Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
gradle publish为什么运行最后一个任务_Gradle - Fatal编程技术网

gradle publish为什么运行最后一个任务

gradle publish为什么运行最后一个任务,gradle,Gradle,下面是相关的build.gradle代码段 version = '0.0.25-SNAPSHOT' publishing { repositories { mavenLocal() } publications { maven(MavenPublication) { groupId = group artifactId = 'xyz-abc' version = ve

下面是相关的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 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)
    }
}