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)?
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)
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>
                        Here is the view of the Mercurial developers: Keyword Substitution - Why You Don't Need It
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With