【问题标题】:How to exclude a gradle task or method during build如何在构建期间排除 gradle 任务或方法
【发布时间】:2016-06-17 08:52:36
【问题描述】:

我定义了一个读取属性文件并更新某个字段的任务。我希望它仅在我执行“发布”时运行,而不是在“构建”期间运行。

我正在使用这个 gradle-release 插件进行发布:https://github.com/researchgate/gradle-release

此插件会在每个版本中将 gradle.properties 文件中的版本更新到下一个版本。我还需要保留当前版本号,因此我编写了这个方法。

但是,每当我进行构建时,都会执行此任务。我试图将其更改为一种方法,并在“uploadArchives”中调用该方法,我认为该方法仅在“发布”期间运行。然而没有结果。它会在每次构建时继续执行!

如何将其从“构建”中排除并仅在发布时调用它?

这是任务和一些代码sn-ps:

task restoreCurrentVersion {
    try {
        String key = 'currentVersion'
        File propertiesFile = project(':commons').file("gradle.properties")
        String currentVersion = project(':commons').version
        this.ant.replaceregexp(file: propertiesFile, byline: true) {
            regexp(pattern: "^(\\s*)$key(\\s*)=(\\s*).+")
            substitution(expression: "\\1$key\\2=\\3$currentVersion")
        }
    } catch (BuildException be) {
        throw new GradleException('Unable to write version property.', be)
    }
}

uploadArchives {
    repositories.mavenDeployer {
        repository(url: 'file://Users/my.home/.m2/repository/')
    }
 //   restoreCurrentVersion()   //Uncommenting this makes the method (when converted the above task to a method) to execute always
}

createReleaseTag.dependsOn uploadArchives    
ext.'release.useAutomaticVersion' = "true"

【问题讨论】:

    标签: gradle build.gradle gradle-release-plugin gradle-task


    【解决方案1】:
    1. 您需要在任务定义中添加 <<doLast 块。否则它将在配置阶段运行,这几乎是您每次运行任何其他任务的时候。看这里:Why is my Gradle task always running?

    2. 没有像您在uploadArchives 中尝试那样直接从另一个任务调用/调用任务的gradle 方法,而是使用dependsOnfinalizedBy 来设置任务依赖关系。如果 uploadArchives 依赖于 restoreCurrentVersion ,每次调用 uploadArchives 时都会先调用 restoreCurrentVersion 。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多