Gradle equivalent of maven-versions-plugin
Asked Answered
R

4

6

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?

Rootlet answered 4/2, 2017 at 18:45 Comment(0)
R
3

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.

Rootlet answered 15/2, 2017 at 12:3 Comment(1)
I like the Maven on Steroids pattern which is close to what you’re describing here. 1. Set the checked-in version to 0-SNAPSHOT 2. Update the version dynamically on release. See: austenconstable.com/2019-11-17-grade-on-steroids and the original Maven concept from Axel Fontaine here: axelfontaine.com/blog/final-nail.htmlSuffer
B
3

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.

Biannulate answered 5/2, 2017 at 1:45 Comment(0)
R
3

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.

Rootlet answered 15/2, 2017 at 12:3 Comment(1)
I like the Maven on Steroids pattern which is close to what you’re describing here. 1. Set the checked-in version to 0-SNAPSHOT 2. Update the version dynamically on release. See: austenconstable.com/2019-11-17-grade-on-steroids and the original Maven concept from Axel Fontaine here: axelfontaine.com/blog/final-nail.htmlSuffer
A
0

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

Alunite answered 5/6, 2019 at 8:57 Comment(0)
P
0

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.

Pierre answered 6/9, 2023 at 2:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.