我正在使用 Mercurial HG 对我的项目进行版本控制。为了使我的构建/编译程序中的版本号保持最新,我正在使用这种方法。我修改了 build.xml 以覆盖预定义的目标。
<target name="-pre-compile">
<!-- getting the version in a fancy format -->
<exec executable="hg" outputproperty="version.mercurial">
<arg value="parent"/>
<arg value="--template"/>
<arg value="{latesttag}.{rev}-{branch}-{node|short}"/>
</exec>
<echo level="warning">Version ${version.mercurial}</echo>
<manifest file="${manifest.file}" mode="update">
<attribute name="Implementation-Version" value="${version.mercurial}"/>
</manifest>
</target>
** 注意hg.exe
在我的类路径中。
要检索版本属性,我在我的主类中使用以下代码片段
String version = Main.class.getPackage().getImplementationVersion();
当我构建我的项目并使用它运行它时java -jar myproject.jar
,版本号是正确的,从那时起一切都很好。
但是当我在我的 IDE 中运行它时,版本字符串 ist null
,因此不会读取清单文件。所以这让我想到这种方法是否有效,或者是否有另一种更简单的方法来保持版本号为最新?
编辑:是否有更复杂的方法将版本号包含到您的程序中?