Search code examples
javagradleantivy

How to get the next build number in Gradle


Is there any way to get the next version when publishing to a repository in gradle?

For e.g. if I have the version 3.0.1 in my repository I want the published version to be 3.0.2.

ivy has a task for ant named buildnumber which does exactly that:

<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>

Is there a way to do so in gradle? if not how can I access ivy tasks from gradle's ant?

In my build.gradle I calling to the ant

ant.importBuild 'build.xml'

Solution

  • After a long work, I managed to do that.

    In my build.gradle I added this following code

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

    I imported my ant build file, and created a task that calls the ivy buildnumber task.

    There is my 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>
    

    Because my IDE (Intellij), didn't have ivy.jar in the content,
    I imported the ivy.jar from my root dir (lib/ivy.jar)