gradle publish为什么运行最后一个任务
下面是相关的build.gradle代码段gradle publish为什么运行最后一个任务,gradle,Gradle,下面是相关的build.gradle代码段 version = '0.0.25-SNAPSHOT' publishing { repositories { mavenLocal() } publications { maven(MavenPublication) { groupId = group artifactId = 'xyz-abc' version = ve
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 build
,incrementSnapshotVersion
任务也正在运行,版本意外更新。
还尝试了
-x incrementSnapshotVersion
,但文件中的版本仍会增加,但是,从build.gradle中删除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)
}
}
你必须把这种行为放在任务的行动中
这样,只有当您将使用/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)
}
}