Gradle equivalent of maven-versions-plugin

3.1k Views Asked by At

This is my build.gradle:

group 'whatever'
version '1.0.0-SNAPSHOT'

...

dependencies {
    compile 'whatever:2.2.1-SNAPSHOT'
}

I want to automate releasing process, which includes the need to set both versions to particular values, e.g. 1.1.0 or 2.2.0 using command line only. Any kind of autoincrement is not an option.

With Maven, I'd do this using maven-versions-plugin:

mvn versions:set -DnewVersion=${WHATEVER_NEW_VERSION}

How can I do the same with Gradle? I only found this unanswered question. There must be some simple way to do that?

4

There are 4 best solutions below

1
On BEST ANSWER

I ended up extracting version numbers to gradle.properties and updating them as part of the automated build script using sed:

sed -i -e \"/someVersionNumber=/ s/=.*/=${SOME_NEW_VERSION_NUMBER}/\" gradle.properties

It's what I want. Although for me, coming from the Maven background, this doesn't seem natural. I may research another alternative later.

0
On

Though not related to publishing, one way to pass command-line properties is as follows:

gradle -PWHATEVER_NEW_VERSION=2.0.0

Consider the following build.gradle snippet:

def newVersion = project."WHATEVER_NEW_VERSION"
println newVersion

See ~/utils/gradle/version.gradle in this project for another approach. It uses separate environment variables for major, minor, and incremental versions and then builds the string automatically. Because it resides in the gradle directory, it can simply be imported into build.gradle, which hides some boilerplate.

0
On

I've added this line on build.gradle.kts

version = System.getenv("NEW_VERSION") ?: "0.0.1-SNAPSHOT"

Not completely equivalent but works fine inside CI/CD scripts.

0
On

I successfully used Axion Release Plugin a couple of times and was very satisfied. Functionality-wise it comes the closest to what Maven's