Maven Release Plugin — A simple example of package management

A simple way of managing package version of your artifact with CI / CD

Thiago Silva - @thihenos
3 min readNov 16, 2020

In this article we will talk about the version management process within a Java project using maven. For this process, we will consider the version model <MAJOR.MINOR.PATCH>, where we will consider Major as an official release, Minor the bugfix fix and Patch any implementation or new feature that should be tested.

What will we use?

Settings

First, inside your project’s pom.xml file, we will configure the maven-release-plugin that will be responsible for managing the PATCH versions (SNAPSHOT).

https://gist.githubusercontent.com/thihenos/87ade6889c1b8d0afbfb135fdeb75f58/raw/edc23fa31fb79da458092275bd17776f171660e0/mavenReleasePlugin.xml

  • Mandatory, we will have to keep the version of the project ending with -SNAPSHOT
  • Inside the scm tag we will configure the address of the Git repository, so that the process creates a git-tag of the versioning done. We will also configure that the generated tag will be the same value as the version attribute of pom, through the annotation @{project.version}
  • Inside the <build></build> tag we will add the release plugin. Within it, we will mark that the repository tag format will be the same as the standard version of the pom and also that our version generation process will not verify that our pom file is unchanged by checking checkModificationExcludes

SNAPSHOT

For this flow, any new implementation or new feature that should be tested and that was merged into the master branch will create a new SNAPSHOT version, which will not yet correspond to the final release version, just a stable version for testing and validations.

To create the first SNAPSHOT version, we must execute the commands release:prepare and release:perform.Briefly, the prepare command is responsible for:

  • Create the git-tag with the same version number for the configured repository
  • Update the POM -SNAPSHOT version from 0.1.-SNAPSHOT to 0.1.1-SNAPSHOT
  • Test (equivalent to the mvn package or mvn test) the project with the new updated version of POM
  • Commit the POM

The performcommand:

  • Will checkout the corresponding tag with the POM version
  • Performed the same as the mvn deploycommand
mvn --batch-mode release:clean release:prepare release:perform -Darguments="-Dmaven.javadoc.skip=true -Dmaven.test.skipTests=true -Dmaven.test.skip=true" -Dusername=<SEU_USER_GIT> -Dpassword=<SUA_SENHA_GIT>

The --batch-modeargument is responsible for executing the entire process without presenting any prompt for entering values, if you do not use it, the terminal will always ask for the next SNAPSHOT version and will ask for the user and password to commit to the git repository.

If you encounter any problems in preparing your release, use the option -Dargumnts=”-Dmaven.javadoc.skip=true -Dmaven.test.skipTests=true -Dmaven.test.skip=true”

MAJOR e MINOR

Considering a complete flow with an official release or a bugfix fix, we will need to change the MAJOR and MINOR versions of the project and for that, we will use the Build Helper Maven Plugin dependency to assist us in managing the versions of the packages.
For that, we must insert the following excerpt in our POM to use the lib.

<dependencies>
<dependency>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.2.0</version>
<type>maven-plugin</type>
</dependency>
</dependencies>

Our final POM will look like this:

Now, to update the MINOR version, we will run

mvn --batch-mode build-helper:parse-version versions:set -DnewVersion=${parsedVersion.majorVersion}.${parsedVersion.nextMinorVersion}.0-SNAPSHOT versions:commit

This command will transform our version from 0.1.0-SNAPSHOT to 0.2.0-SNAPSHOT

mvn --batch-mode build-helper:parse-version versions:set -DnewVersion=${parsedVersion.nextMajorVersion}.${parsedVersion.minorVersion}.0-SNAPSHOT versions:commit

This command will transform our version from 0.1.0-SNAPSHOT to 1.1.0-SNAPSHOT
We will always use the versions: commit argument so that maven will always commit POM changes. Then, we will execute the prepare and perform command again so that these changes are inserted into our repository.

mvn --batch-mode release:clean release:prepare release:perform -Darguments="-Dmaven.javadoc.skip=true -Dmaven.test.skipTests=true -Dmaven.test.skip=true" -Dusername=<USER_GIT> -Dpassword=<PASSWORD_GIT>

--

--

Thiago Silva - @thihenos
Thiago Silva - @thihenos

Written by Thiago Silva - @thihenos

DevOps, SRE e Engenheiro de Computação em núvem. Trabalhei como gerente por +2 anos pela AmBev.

No responses yet