Austen's Blog

moon indicating dark mode
sun indicating light mode

Gradle on steroids

November 17, 2019

I’ve been experimenting with the Gradle build tool and the final hurdle, while converting one of my Maven projects, was to produce a tagged release.

For simple projects I’m a big fan of Axel Fontaine’s “Maven on Steroids” pattern. Whilst Gradle has a number of release and version plugins (https://github.com/researchgate/gradle-release, https://github.com/ru-fix/gradle-release-plugin, https://github.com/allegro/axion-release-plugin, https://github.com/nebula-plugins/nebula-release-plugin), they are all quite heavyweight and I couldn’t find one that enabled this simple versioning approach. After lots of Googling and some experimentation, the following seems to work quite well.

Configure your gradle build

Step 1: Create a version.gradle file

In the project’s root directory create a version.gradle file and populate with 0-SNAPSHOT. 0-SNAPSHOT will be the default version number and is used for local snapshot builds. We’ll override this during the CI build process.

// ${rootDir}/version.gradle
project.version = "0-SNAPSHOT"

Then plug the new version file into your Gradle build like so:

// ${rootDir}/build.gradle
allprojects {
apply from: "${rootDir}/version.gradle"
}

Step 2: Setup your CI build to update your projects version

I’m using CircleCI for this particular project, so I’ve added a task to re-version the build with the appropriate version number - in this case, taken from the tag.

- run:
name: If we're building a tag, set the version to that of the tag
command: |
if [ -n "$CIRCLE_TAG" ]; \
then \
sed -i -e "s/0-SNAPSHOT/$CIRCLE_TAG/g" version.gradle; \
fi

And we’re done

Using this approach we have a really lightweight way to version releases which doesn’t resort to the lengthy workflow of the Maven release plugin.

References