开发者

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

开发者 https://www.devze.com 2022-12-30 00:34 出处:网络
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, ar开发者_如何转开发e written to a spe

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, ar开发者_如何转开发e 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


hg branch | xargs hg log -l1 --template {rev} -b

This will give you the revision number for the current branch that you are working in - very important for builds from different branches.

In an ant file this is what you need...

    <exec dir="${basedir}"
        executable="/usr/local/bin/hg"
        outputproperty="branch">

          <arg value="branch"/>
    </exec>

    <exec dir="${basedir}"
          executable="/usr/local/bin/hg"
          outputproperty="version">

          <arg value="log"/>
          <arg line="-l1 --template {rev} -b${branch}" />
    </exec>


Mercurial has an extension for keyword expansion. See KeywordExtension for instructions and warnings.

0

精彩评论

暂无评论...
验证码 换一张
取 消