I have a build.gradle file that uses the maven plugin. I am attempting to build a project and upload the artifacts to a nexus repo. The build and upload portion is working fine, however I am running into issues trying to set the version name and code dynamically. Here's are the important tasks in my build.gradle file:
uploadArchives {
repositories {
mavenDeployer {
snapshotRepository(url: "https://artifact.example.net/content/repositories/snapshots") {
authentication(userName: artifactUsername, password: artifactPassword)
}
pom.version = rootProject.ext.name.toString() + "-" + rootProject.ext.code.toString() + "-SNAPSHOT"
pom.artifactId = appId
pom.groupId = "com.example.mobile.android"
}
}
}
task getVersionCode(type: Exec) {
commandLine './getVersionCode.sh'
workingDir scriptDir
standardOutput = new ByteArrayOutputStream()
doLast {
rootProject.ext.code = standardOutput.toString() as Integer
println(code)
}
}
task getVersionName(type: Exec) {
commandLine './getVersionName.sh'
workingDir scriptDir
standardOutput = new ByteArrayOutputStream()
doLast {
rootProject.ext.name = standardOutput.toString()
println(rootProject.ext.name)
}
}
preBuild.dependsOn getVersionCode
preBuild.dependsOn getVersionName
uploadArchives.dependsOn getVersionCode
uploadArchives.dependsOn getVersionName
What I'm finding is that even though the getVersionCode and getVersionName tasks run prior to the uploadArchives task running, the variables rootProject.ext.name and rootproject.ext.code have their default values and not the values set dynamically by the tasks. I have verified that if I set a variable like:
rootProject.ext.name = "new Name"
that I can then print out the value like so:
println(rootProject.ext.name)
That all works, but then when I try to use that same variable from my mavenDeployer task the variables reset to their default values. What am I doing wrong here?
Try adding 'mustRunAfter', e.g.
preBuild.mustRunAfter getVersionCode
.