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

Rabarberski picture Rabarberski · May 10, 2010 · Viewed 7.7k times · Source

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

Answer

VonC picture VonC · May 10, 2010

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)