mavenpom.xml

Simplest way to generate version string for Maven package from Git branch


I want to automate the version string generation for release builds of my Java project. My pom.xml has a <version>${my-version-property}</version tag and the Git branch is called e.g. release/1.0.2. How do I extract "1.0.2" from the branch name, so that I can use it as a property in downstream project dependency tags and in the <finalName> for the maven-war-plugin?

I found the buildnumber-maven-plugin that allows me so set a property to a generated string through Java's MessageFormat mechanism. Its ${scmBranch} property contains the current Git branch, but MessageFormat syntax doesn't support substrings (correct me if I'm wrong).

I found this question where one answer suggests gmaven-plugin. But I would rather not use two different plugins for this otherwise relatively simple thing. In fact, I wouldn't say I'd be reinventing the wheel if I had to write a line of source code somewhere to achieve this. However, I don't intend to make my Java project aware of the build process. What's the most straightforward approach here?


Solution

  • In Gitlab CI, use something like

    - mvn versions:set -DnewVersion=$RELEASE_VERSION

    where your first set $RELEASE_VERSION to the right value.