Last active
August 29, 2015 14:03
-
-
Save Tzrlk/cce6290a439a8f0ef3a5 to your computer and use it in GitHub Desktop.
Generates a version number from your maven version, adding on commits since branching, and the hash of the commit. Good for continuous integration.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
export args="$*" | |
# Check whether in quiet mode. | |
export quiet=false | |
if grep -q '\-q' <<<$args; then | |
export quiet=true | |
fi | |
function log { | |
if ! $1; then | |
echo $2 | |
fi | |
} | |
if [ ! -f pom.xml ]; then | |
log $quiet "Script needs to be run in project root." | |
exit 1 | |
fi | |
# Get current maven version. | |
export VER_MVN=`mvn help:evaluate \ | |
-Dexpression=project.version \ | |
| grep '^[0-9][0-9A-Za-z\.\-]*$'` | |
log $quiet "-> Maven version extracted: $VER_MVN." | |
if ! grep -q "SNAPSHOT$" <<<$VER_MVN; then | |
log $quiet "Maven version must be SNAPSHOT" | |
exit 2 | |
fi | |
if [ -d ".git" ]; then | |
# Get master revision. | |
export GIT_MASTER_REV=`git rev-list --count origin/master` | |
log $quiet "-> Git master revision extracted: $GIT_REV." | |
# Get branch revision. If master, should be 0. | |
export GIT_BRANCH_REV=`git rev-list --count HEAD ^origin/master` | |
log $quiet "-> Git branch revision extracted: $GIT_REV." | |
# Get current git SHA | |
export GIT_HASH=`git rev-parse --short HEAD` | |
log $quiet "-> Git hash extracted: $GIT_HASH." | |
# Construct build version. | |
export BUILD_VERSION="$GIT_MASTER_REV.$GIT_BRANCH_REV.$GIT_HASH" | |
log $quiet "-> Build version generated as: $BUILD_VERSION." | |
elif [ -d ".svn" ]; then | |
# Get current svn branch | |
export SVN_BRANCH=`svn info \ | |
| grep '^Relative URL:' \ | |
| sed 's/.*\///'` | |
log $quiet "-> SVN branch extracted: $SVN_BRANCH." | |
# Get current svn rev | |
export SVN_VERSION=`svn info \ | |
| grep '^Revision:' \ | |
| sed 's/Revision: //'` | |
log $quiet "SVN revision extracted: $SVN_VERSION." | |
# Construct build version | |
export BUILD_VERSION="$SVN_BRANCH-$SVN_VERSION" | |
log $quiet "-> Build version generated as: $BUILD_VERSION." | |
else | |
log $quiet "Directory not under source control." | |
exit 3 | |
fi | |
# Replace "SNAPSHOT" with generated version number. | |
export FINAL_VERSION=`log $quiet $VER_MVN | sed "s/SNAPSHOT/$BUILD_VERSION/"` | |
log $quiet "-> Creating final build version: $FINAL_VERSION." | |
if grep -q "\-\-exec" <<<$args; then | |
# Set maven version appropriately. | |
mvn versions:set versions:commit -DnewVersion="${FINAL_VERSION}" | |
fi | |
echo $FINAL_VERSION |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment