Select Page

Update Maven pom version on GIT checkout in TeamCity

Update Maven pom version on GIT checkout in TeamCity

Here is a solution to the following problems

  • Deriving #Maven artifact version from #GIT branch,
  • Update pom version on #GIT checkout automatically,
  • Add the ability to use Pull request with Apache #Maven.

You have a workflow requirement that require you to have the artifact version of a module externally defined from the current branch in #GIT.

For example

You want to start working on a new feature branch “feature-memory-improvement”, so you branch from master a new branch named feature/feature-memory-improvement

Having unique snapshot is a something you need to share your code using a #Maven repository, so you may want to have into the branch all pom.xml version changed to

<version>FEATURE-MEMORY-IMPROVEMENT-SNAPHOTS</version>

changing all your pom.xml and doing a technical commit  will create merge conflicts when using pull request!

One solution, while not perfect is to do the following:  You can add a separate execution to run a goal which will change the version of the POM automatically in the #Maven reactor. This small script will do it¨

Create a TeamCity Command line Build Step with a custom script (Linux Bash)

echo 'Change the version in pom.xml files...' branch=$(git rev-parse --abbrev-ref HEAD)  
version="$branch-SNAPSHOT"

# OPTIONAL: filter out any eventual Branch prefixes
# e.g. /bugfix /feature /release  version="$(echo $version | sed 's/bugfix\///g')" version="$(echo $version | sed 's/feature \///g')" 
version="$(echo $version | sed 's/release \///g')"

# OPTIONAL: make pom version all uppercase
version="${version^^}"  mvn versions:set -DgenerateBackupPoms=false -DnewVersion="$version" echo 'Changed version in pom.xml files to ${version}' exit 0

What is changing?

You do not have anymore to make pom version unique in your feature branch and commit pom version as they will change on the fly according to the branch name. You can now enjoy pull request in Atlassian Stash or GitHub Enterprise.

pom.xml all stay to MASTER-SNAPSHOT in all branches

Soon I will show you hot to do this using a server side GIT hook in Atlassian Stash and with a client side GIT hook.

Note about Branch prefixes

Use the prefixes below as part of your branch names to categorize them and take advantage of automatic branching workflows

These are the conventions I am using, Atlassian Bitbucket propose them as default

  • bugfix/ Typically used for fixing bugs against a release branch
  • feature/ Used for specific feature work. Typically, this branches from and merges back into the development branch
  • hotfix/ Typically used to quickly fix the production branch
  • release/ Used for release tasks and long-term maintenance. Typically, this branches from the development branch and changes are merged back into the development branch

Thanks to these articles for the idea.

About The Author

I worked with various Insurances companies across Switzerland on online applications handling billion premium volumes. I love to continuously spark my creativity in many different and challenging open-source projects fueled by my great passion for innovation and blockchain technology.In my technical role as a senior software engineer and Blockchain consultant, I help to define and implement innovative solutions in the scope of both blockchain and traditional products, solutions, and services. I can support the full spectrum of software development activities, starting from analyzing ideas and business cases and up to the production deployment of the solutions.I'm the Founder and CEO of Disruptr GmbH.

Categories