Wednesday, September 2, 2015

Injection GIT branch / tag name into file

Prerequisite: In your project you need the file src/main/resources/

Of course you can adapt to using whatever file you want.

Note that this assumes you're using a "version=" property in your file

Step 1: Add the maven plugin to run an executable

1:  #!/usr/bin/env bash  
3:  # Get the git branch / tag name  
5:  # first, see of we have branch information (this will not be available if we checked out a tag)  
6:  TAG_OR_BRANCH="$(git rev-parse --abbrev-ref HEAD | egrep -o '([0-9]{1,}\.)+[0-9]{1,}')"  
8:  if [ "$TAG_OR_BRANCH" == "" ]; then  
10:    # if we can't get branch info, then we must be in a tag, so use that  
11:    TAG_OR_BRANCH="$(git describe | egrep -o '([0-9]{1,}\.)+[0-9]{1,}+(-b[0-9]{1,})')"  
12:  fi  
14:  # Remove the version from the properties file  
15:  sed -i '/^version=/ d' src/main/resources/static/version  
17:  # Add the version to the properties file  
18:  echo "Writing version $TAG_OR_BRANCH"  
19:  echo "version=$TAG_OR_BRANCH" >> src/main/resources/static/version  


  1. This comment has been removed by the author.

  2. We end beta tagged versions with "-b" and a beta ID .. eg. 1.0.0-b3 ... so in my case, when I am reading the version info for an annotated tag for a beta build, I require "+(-b[0-9]{1,})" as part of my regex. ... you may not ... so feel free to change it accordingly.