views:

265

answers:

3

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)?

+3  A: 

You can rather use hg identity.

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)

VonC
That trailing plus sign means VonC has local, uncommitted changes, which can be nice to note too.
Ry4an
Speaking of which, how is that migration coming along?
Santa
+1  A: 

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

Devon_C_Miller
Hmm, I don't get the point. What is meant by keyword expansion? The only thing I can think of is e.g. that you can write 'hg id' instead of 'hg identity', but that concept doesn't seem to apply here.
Rabarberski
Other VCS allow you to add special keywords such as $Id$, $Rev$, $Author$ that are expanded when the file is checked out. This allowed you to write things like const char * version = "$Rev$"; which would become something like const char * version = "$Rev: 1.2$" when the file was checked out.
Devon_C_Miller
+3  A: 

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>
Paul