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

六月ゝ 毕业季﹏ 提交于 2019-12-09 04:23:09

问题


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


回答1:


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)




回答2:


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>



回答3:


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




回答4:


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>



回答5:


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



来源:https://stackoverflow.com/questions/2805976/java-how-to-get-mercurial-current-changeset-number-for-use-in-program

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!