I defined a task that reads the property file and updates a certain field. I want this to run only when I execute 'release' and not during 'build'.
I am using this gradle-release plugin for release : https://github.com/researchgate/gradle-release
This plugin updates the version in the gradle.properties file to the next version, on every release. I need to keep the current version number as well, and hence I wrote this method.
However, this task is getting executed whenever I do a build. I tried to change it to a method, and call the method within 'uploadArchives' which I presume runs only during 'release'. Yet no result. It keeps on executing on every build!
How do I exclude it from 'build' and invoke it only in case of release ?
Here is the task and some code snippets :
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"
You need to add a <<
or a doLast
block to your task definition. Otherwise it will run during the configuration phase, which is pretty much every time you run any other task. see here: Why is my Gradle task always running?
There is no gradle way to invoke/call a task from another task directly like you attempted to do in uploadArchives
instead you use dependsOn
or finalizedBy
to setup task dependencies. If uploadArchives depends on restoreCurrentVersion , restoreCurrentVersion will be called first everytime uploadArchives is called.