Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: how to get mercurial current changeset number for use in program

I've recently started using mercurial for version control in a Java project. When I run my program, the input parameters it has used to produce certain a output, are written to a specific file. It would be nice if I could add the current mercurial changeset number (indicating the version of my program) to that output file as well.

What would be the easiest way to do so on Windows? I could write a simple Java parser to fetch the output of the first line of the hg log -l 1 command, but perhaps there is an easier way (i.e., less code lines)?

like image 603
Rabarberski Avatar asked May 10 '10 19:05

Rabarberski


3 Answers

You can rather use hg identify.

hg id should be during the packaging step, when the sources have been committed and you generate the packaged (jar) version of your application.
During that step, you can generate a version.txt file with that kind of information.

$ MY_VERSION=$(hg id)
$ echo $MY_VERSION
53efa13dec6f+ tip

(see for instance "build identification" for Python)

like image 138
VonC Avatar answered Nov 15 '22 13:11

VonC


Since you're in a Java project, this might be relevant to you. I use this Ant target to display the version info (Mercurial changeset id) in the application list in the Tomcat Manager page. I simply put the changeset id inside the display-name xml element in my web.xml.

<target name="build.release">
    <exec executable="/usr/local/bin/hg" outputproperty="scm.version.tag.id">
        <arg value="id"/>
        <arg value="-i"/>
    </exec>
    <filter token="build.version.tag" value="${scm.version.tag.id}" />
    <copy file="${web.home}/WEB-INF/web.xml" todir="${build.home}" filtering="true" />
</target>

Inside the web.xml, there's a token in the display-name xml element, like this:

<display-name>My Webapp @build.version.tag@</display-name>
like image 24
Paul Avatar answered Nov 15 '22 14:11

Paul


Here is the view of the Mercurial developers: Keyword Substitution - Why You Don't Need It

like image 22
Devon_C_Miller Avatar answered Nov 15 '22 14:11

Devon_C_Miller