如何从 ant 构建脚本中查找最新的 git commit hash [英] How to lookup the latest git commit hash from an ant build script
问题描述
如何从 ant 构建脚本中查找最新的 git commit 哈希?
How can I lookup the latest git commit hash from an ant build script?
我目前正在开发一个存储在 github 上的新开源项目.我想扩展我现有的 ANT 构建文件以允许我创建编号的构建.我想象着我会用ant buildnum -Dnum=12"之类的东西启动构建.
I am currently working on a new open source project which I store on github. I would like to extend my existing ANT build file to allow me to create numbered builds. I am imagining that I would launch the build with something like "ant buildnum -Dnum=12".
我希望生成的 jar 在其清单文件中包含两个关键信息:
I would like the resulting jar to have two crucial bits of information in it's manifest file:
- build.number=12
- build.gitcommit=
我知道如何创建 build.number 行.但是,我不确定查找最新的 git commit hash 的最佳蚂蚁管道,这是我想要填写的值.
I know how to create the build.number line. However, I am unsure of the best ant plumbing to lookup the latest git commit hash which is the value I want to fill in for .
推荐答案
我在 github 上为一个项目编写了以下 ant 目标.用法:
I wrote the following ant target for a project on github. Usage:
- 在属性repository.version"中存储版本
- 在没有安装 git 或没有 .git 目录时工作(后备)
- 其他目标如果需要 git 版本,则必须依赖此目标
- 只执行一个 git 命令(--always)
<available file=".git" type="dir" property="git.present"/>
<target name="git.revision" description="Store git revision in ${repository.version}" if="git.present">
<exec executable="git" outputproperty="git.revision" failifexecutionfails="false" errorproperty="">
<arg value="describe"/>
<arg value="--tags"/>
<arg value="--always"/>
<arg value="HEAD"/>
</exec>
<condition property="repository.version" value="${git.revision}" else="unknown">
<and>
<isset property="git.revision"/>
<length string="${git.revision}" trim="yes" length="0" when="greater"/>
</and>
</condition>
</target>
例如用于扩展模板文件中的令牌@repository.version@
:
It e.g. be used for expanding the token @repository.version@
in a template file:
<target name="index.html" depends="git.revision" description="build index.html from template">
<copy file="index.html.template" tofile="index.html" overwrite="yes">
<filterchain>
<replacetokens>
<token key="repository.version" value="${repository.version}" />
</replacetokens>
</filterchain>
</copy>
</target>
这篇关于如何从 ant 构建脚本中查找最新的 git commit hash的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!