Maven + Mercurial for Build Numbers
I can't figure out how to get a Mercurial revision id put into my Maven build (ideally I would like it in the MANIFEST of my jars and war).
The clos开发者_开发问答est solution I could find is:
mvn -DbuildNumber=`hg id -i`
Which won't really work for Windows or my Hudson server. Luckily Hudson tags my builds but I would like some more assurance if the builds were also tagged with the Mercurial changset id.
Have a look at this previous question and the link from the accepted answer. Basically, you want to do the same thing except that you'll want to use the buildnumber:hgchangeset
goal with Mercurial to get a changeset
property with the content of hg id -i
.
Unfortunately, hg id -i
is too long for use. I created a script that will calculate an accurate build number. However, there are two exceptions. If there was not previous release on the branch, then it cannot be valid. If there are changes in the local repo, then it cannot be valid. In my build script I mark the build as "x.x.UNSTABLE" whenever that happens.
I use a REL_PATTERN to pick up the last tag in the current branch that was marked as an actual release. Then I calculate the build number by tracking the commit log count from that release + all commits to the branch since that release.
#!/bin/bash
REL_PATTERN="release-[0-9]*\.[0-9]*\.[0-9]*"
BRANCH=$( hg branch )
CURR_REV=$( hg id -n )
if [ "${CURR_REV: -1}" = "+" ] ; then
echo "ERROR: This workspace contains uncommitted code. Cannot calculate build number" >&2
echo "UNSTABLE"
exit 1
fi
RELEASE=$( hg log --rev="branch($BRANCH) and tag() and 1:$CURR_REV" -T "{tags} {rev}\n"|grep "${REL_PATTERN} "|tail -1 )
if [ "$RELEASE" = "" ] ; then
echo "ERROR: Unable to locate version tag" >&2
echo "UNSTABLE"
exit 1
fi
RELEASE_REV=$( echo $RELEASE|cut -f 2 -d ' ' )
RELEASE_TAG=$( echo $RELEASE|cut -f 1 -d ' ' )
REVS=$( hg log -P $RELEASE_REV -b $BRANCH -T "{rev}\n"|wc -l )
BUILD=$( hg log -r1:$CURR_REV -P $RELEASE_REV -b $BRANCH -T "{rev}\n"|wc -l )
echo "BRANCH=$BRANCH" >&2
echo "CURR_REV=$CURR_REV" >&2
echo "RELEASE_REV=$RELEASE_REV" >&2
echo "RELEASE_TAG=$RELEASE_TAG" >&2
echo "BUILD=$BUILD" >&2
echo $BUILD
精彩评论