我定义了一个读取属性文件并更新特定字段的任务。我希望仅在执行“发布”时运行,而不在“构建”时运行。
我正在使用此gradle-release插件进行发布:https://github.com/researchgate/gradle-release
在每个发行版上,此插件都会将gradle.properties文件中的版本更新为下一个版本。我还需要保留当前的版本号,因此我编写了此方法。
但是,无论何时执行构建,此任务都会被执行。我试图将其更改为方法,并在“uploadArchives”中调用该方法,我认为该方法仅在“release”期间运行。仍然没有结果。它会在每个构建中继续执行!
如何将其从“build”中排除并仅在发布时调用它?
这是任务和一些代码片段:
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"
最佳答案
<<
或doLast
块。否则它将在配置阶段运行,几乎每次您运行任何其他任务时都会运行。看到这里:Why is my Gradle task always running? uploadArchives
中那样直接从另一个任务中调用/调用另一个任务的gradle方法,而是使用dependsOn
或finalizedBy
设置任务依赖项。如果uploadArchives取决于restoreCurrentVersion,则每次调用uploadArchives时将首先调用restoreCurrentVersion。 关于gradle - 如何在构建期间排除gradle任务或方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35782186/