I have 2 tasks in my build.gradle - release and releasePatch. Depends on which task is running I want to set different versions to maven artifacts.
version = new Version(project: this, major: 1, minor: 1, release: false, patch: false)
gradle.taskGraph.whenReady { taskGraph ->
if (taskGraph.hasTask(release) || taskGraph.hasTask(releasePatch)) {
version.release = true;
version.patch = taskGraph.hasTask(releasePatch)
taskGraph.getAllTasks().each { task ->
if (task.name.endsWith('PublicationToMobileRepository')) {
task.repository.url = releaseRepository
task.publication.version = version.toString()
}
}
}
}
publishing {
publications {
model(MavenPublication) {
artifactId "model"
artifact tasks.modelJar
artifact tasks.sourceModelJar {
classifier 'sources'
}
}
}
repositories {
maven { name 'mobile'; auth 'dx'; url snapshotRepository }
}
}
class Version {
def project
int major
int minor
boolean patch
boolean release
String toString() { patch ? "$major.$minor${release ? '' : '-SNAPSHOT'}" : "$major${release ? '' : '-SNAPSHOT'}" }
Version increaseMajor() { major++; minor = 0;
this }
Version increaseMinor() { minor++; this }
}
The problem is that the line “task.publication.version = version.toString()” doesnt affect artifact version. So when releasePatch is called expected version is 1.1 but actual is 1-SNAPSHOT (because patch and release are still false)
How can I fix it?