为什么gradle发布正在运行最后一个任务



以下是相关的build.gradle片段

version = '0.0.25-SNAPSHOT'
publishing {
repositories {
mavenLocal()
}
publications {
maven(MavenPublication) {
groupId = group
artifactId = 'xyz-abc'
version = version
from components.java
}
}
}
task incrementSnapshotVersion {
String jVersion = version
int snapshotSuffixBegin = jVersion.lastIndexOf('-')
String currentMinor = jVersion.substring(jVersion.lastIndexOf('.') + 1, snapshotSuffixBegin)
String updatedMinor = (Integer.parseInt(currentMinor) + 1).toString()
String major = jVersion.substring(0, jVersion.lastIndexOf(currentMinor))
String newVersion = major + updatedMinor + "-SNAPSHOT"
String s = buildFile.getText().replaceFirst("version = '$jVersion'", "version = '" + newVersion + "'")
buildFile.setText(s)
}

在低于命令运行时,/home/user/gradle-5.1.1/bin/gradle clean buildincrementSnapshotVersion任务也在运行,版本意外更新。也尝试了-x incrementSnapshotVersion,但文件中的版本仍然会增加,然而,在从build.gradle中删除incrementSnapshotVersion时,版本保持原样。

现在,版本将在配置阶段增加,这就是为什么每次运行命令时,版本都会增加。

你必须把这种行为放在任务的行动中。

这样,只有当您将使用./gradlew incrementSnapshotVersion执行任务,或者您将执行依赖的任务,或者您的任务完成时,版本才会增加。

task incrementSnapshotVersion {
doFirst {
String jVersion = version
int snapshotSuffixBegin = jVersion.lastIndexOf('-')
String currentMinor = jVersion.substring(jVersion.lastIndexOf('.') + 1, snapshotSuffixBegin)
String updatedMinor = (Integer.parseInt(currentMinor) + 1).toString()
String major = jVersion.substring(0, jVersion.lastIndexOf(currentMinor))
String newVersion = major + updatedMinor + "-SNAPSHOT"
String s = buildFile.getText().replaceFirst("version = '$jVersion'", "version = '" + newVersion + "'")
buildFile.setText(s)
}
}

最新更新