Java:如何获取 Mercurial 当前变更集编号以在程序中使用
我最近开始在 Java 项目中使用 Mercurial 进行版本控制。当我运行程序时,它用于产生某些输出的输入参数被写入特定文件。如果我也可以将当前的 Mercurial 变更集编号(指示我的程序的版本)添加到该输出文件中,那就太好了。
在 Windows 上执行此操作的最简单方法是什么? 我可以编写一个简单的 Java 解析器来获取 hg log -l 1 命令第一行的输出,但也许有更简单的方法(即更少的代码行)?
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)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以使用
hg recognize
。hg id
应该在打包步骤中,即提交源代码并生成应用程序的打包 (jar) 版本时。在此步骤中,您可以生成包含此类信息的 version.txt 文件。
(例如,请参阅Python 的“构建标识”)
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.
(see for instance "build identification" for Python)
由于您处于 Java 项目中,因此这可能与您相关。我使用此 Ant 目标在 Tomcat Manager 页面的应用程序列表中显示版本信息(Mercurial 变更集 id)。我只是将变更集 ID 放入 web.xml 的 display-name xml 元素中。
在 web.xml 中,display-name xml 元素中有一个标记,如下所示:
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.
Inside the web.xml, there's a token in the display-name xml element, like this:
以下是 Mercurial 开发人员的观点:关键字替换 - 为什么您不需要它
Here is the view of the Mercurial developers: Keyword Substitution - Why You Don't Need It
HG分行| xargs hg log -l1 --template {rev} -b
这将为您提供当前正在工作的分支的修订号 - 对于来自不同分支的构建非常重要。
在 ant 文件中,这就是您所需要的......
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...
Mercurial 有一个关键字扩展扩展。请参阅 KeywordExtension 了解说明和警告。
Mercurial has an extension for keyword expansion. See KeywordExtension for instructions and warnings.