Gradle 任务不应自动执行 [英] Gradle task should not execute automatically

查看:76
本文介绍了Gradle 任务不应自动执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 gradle 中定义一个任务:

I'm defining a task in gradle:

task releaseCandidate(type: Exec) {
    commandLine 'git', 'checkout', 'develop'

    // Increment version code in Manifest
    String manifest = new File('AndroidManifest.xml').getText('UTF-8')
    Pattern pattern = Pattern.compile('android:versionCode="([0-9]+)"')
    Matcher matcher = pattern.matcher(manifest)
    matcher.find()
    int newVersionCode = Integer.parseInt(matcher.group(1)) + 1
    manifest = manifest.replaceAll(
        "android:versionCode="([0-9]+)"", "android:versionCode="$newVersionCode""
    )
    new File('AndroidManifest.xml').write(manifest, 'UTF-8')

    commandLine 'git', 'diff'
}

仅当我明确将其称为 gradle releaseCandidate 时才想执行.但是,当我运行任何其他任务时,例如 gradle assembleDebug,它也会运行任务 releaseCandidate.我不希望这种行为发生.没有依赖于 releaseCandidate 的任务,反之亦然.

Which I want to execute only when I explicitly call it as gradle releaseCandidate. However, when I run any other task, such as gradle assembleDebug, it also runs task releaseCandidate. I don't want that behaviour to happen. There is no task depending on releaseCandidate or vice-versa.

我的项目是一个 Android 应用,所以我使用的是 android gradle 插件.

My project is an Android app, so I am using android gradle plugin.

推荐答案

一个常见的陷阱.向任务添加操作,否则代码将在配置阶段运行.带操作的示例任务:

A common pitfall. Add an action to the task otherwise code will run at configuration phase. Sample task with action:

task sample << {
}

正如我所见,您宁愿需要编写自定义任务而不是使用 Exec 类型.我认为将 commandLine 定义两次是无效的.

As I see You'd rather need to write a custom task than using Exec type. I suppose it's not valid to define commandLine twice.

编辑

您可以阅读这个发布以了解其工作原理.

You can read this post to get the general idea how it all works.

这篇关于Gradle 任务不应自动执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆