如何在 Gradle 中获取下一个内部版本号

How to get the next build number in Gradle

发布到 gradle 中的存储库时,是否有任何方法可以获取下一个版本?

例如如果我的存储库中有 3.0.1 版本,我希望发布的版本为 3.0.2.

ivy 有一个名为 buildnumberant 任务,它确实是这样做的:

<project xmlns:ivy="antlib:org.apache.ivy.ant"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<target name="ivyBuildNumber" description="Use ivy get the next build number">
    <ivy:buildnumber
        resolver="url-chain"
        organisation="${ivy.organisation}"
        module="${ivy.module}"
        revision="${version.base}"/>

    <echoproperties prefix="ivy.new."/>
</target>

gradle有没有办法做到这一点?如果不是,我如何从 gradle 的 ant 访问 ivy 任务?

在我的 build.gradle 我打电话给 ant

ant.importBuild 'build.xml'

我认为 Gradle 中没有支持,但您可以尝试使用 Ant 任务。 https://docs.gradle.org/current/userguide/ant.html#sec:import_ant_build

另一种方法是使用某种插件或自定义任务来管理版本。

是的,您可以通过将 ant 的 build.xml 文件导入 gradle 的 build.gradle 文件来从 ant 脚本访问 ivy 任务。以下是这样做的语法。

ant.importBuild 'build.xml'

请参考:https://docs.gradle.org/current/userguide/ant.html#sec:import_ant_build

我推荐你使用 ResearchGate 发布插件 https://github.com/researchgate/gradle-release 它有一个漂亮的文档。易于阅读。 另外,看看我是如何在我的个人项目中使用它的。 https://github.com/vatolinrp/bitcoin-esb/blob/master/build.gradle 这对你来说是一个很好的例子。

经过长时间的努力,我终于做到了。

在我的 build.gradle 中,我添加了以下代码

ant.importBuild 'build.xml'

task getNextBuild(dependsOn : ivyBuildNumber) {
    doLast{
        def nextVersion = ant.properties['ivy.new.revision']
        println nextVersion
    }
}

我导入了 ant 构建文件,并创建了一个调用 ivy buildnumber 任务的任务。

还有我的build.xml

<project xmlns:ivy="antlib:org.apache.ivy.ant">

    <target name="ivyBuildNumber">
        <path id="ivy.classpath" path="lib/ivy.jar" />
        <typedef resource="org/apache/ivy/ant/antlib.xml" uri="antlib:org.apache.ivy.ant" classpathref="ivy.classpath" />
        <ivy:buildnumber
            organisation="daniel"
            module="hello"/>
        <echoproperties prefix="ivy.new."/>
    </target>
</project>

因为我的IDE(Intellij),内容里没有ivy.jar
我从根目录 (lib/ivy.jar)

导入了 ivy.jar
  • 对于这个确切的行为,可以使用纯 Gradle 调用 Ivy buildnumber 任务,而无需导入 Ant 构建:
configurations {
    antTasks // define a new configuration
}

repositories {
    mavenCentral()
}

dependencies {
    antTasks("org.apache.ivy:ivy:2.4.0") // add Ivy library to it
}

ext {
    // define the Ivy task, using the extra configuration as classpath extension
    ant.taskdef(name: "ivyBuildNumber", 
                classname: "org.apache.ivy.ant.IvyBuildNumber", 
                classpath: configurations.antTasks.asPath) 

    ant.ivyBuildNumber(organisation: "daniel", module: "hello")
    nextVersion = ant.properties["ivy.new.revision"]
}

task demo {
    doLast {
        println nextVersion
    }
}
  • 一般来说,Gradle没有任何捆绑的等价于Maven Release Plugin,所以必须依赖插件。一个可靠的插件是 Allegro Tech 的 gradle-release by ResearchGate, the other is axion。前者是经典的 Maven 风格的版本控制,后者将 SCM 本身作为唯一的真实来源,消除了构建文件中的版本控制。但是这些插件都没有提供确切的请求行为。

  • 我个人对版本控制问题的看法最初是使用一些插件。因为我在工作中使用 Bamboo 作为 CI 服务器,所以我使用 Gradle 使用发布插件所做的一切迟早会在 CI 服务器上崩溃。它可能已经工作了几个星期,但每次服务器更新都会带来一些问题。我最终使用了一个简单约定的无 SCM 方法:使用分支名称作为基本版本,将其与内部版本号连接(两个值均由 CI 服务器提供):

ext {
    branch = System.getProperty("branch", "develop")
    buildNumber = System.getProperty("buildNumber", "latest")
    isRelease = System.getProperty("isRelease", "false").toBoolean()
    artifactVersion = "${branch}${(isRelease ? ".$buildNumber" : "-SNAPSHOT")}"
}

CI 服务器可以设置执行以下命令

./gradlew -DisRelease=true -Dbranch=${git.branch} -DbuildNumber=${build.number} mavenPublish

当按下 'Release' 按钮时。例如,3.0 分支的构建 12 将在二进制存储库中生成版本 3.0.12。

优点是:
+ 该版本是免费的,假设分支相应地命名
+ 自动递增的版本号也是免费的
+ 可以轻松发布自定义修订
+ 没有插件意味着 Gradle 版本更新没有问题
+ 这种方法非常简单并且总是有效

缺点是:
- 标签需要额外的脚本任务
- 显然会跳过一些内部版本号(例如 3.5.76 之后的下一个版本可以是 3.5.84)