问题
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 closest 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.
回答1:
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
.
回答2:
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
来源:https://stackoverflow.com/questions/3934603/maven-mercurial-for-build-numbers