如何从 ant 构建脚本中查找最新的 git 提交散列?
我目前正在开发一个存储在 github 上的新开源项目。我想扩展我现有的 ANT 构建文件以允许我创建编号的构建。我想象我会用类似“ant buildnum -Dnum=12”的东西启动构建。
我希望生成的 jar 在其 list 文件中包含两个关键信息:
- build.number=12
- build.gitcommit=
我知道如何创建 build.number 行。但是,我不确定查找最新 git commit hash 的最佳 ant 管道,这是我想为 . 填写的值。
最佳答案
我为 github 上的一个项目写了下面的 ant 目标。用法:
- 在属性“repository.version”中存储版本
- 在没有安装 git 或没有 .git 目录的情况下工作(回退)
- 其他target如果需要git版本就必须依赖这个target
- 只执行一个 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>
例如用于在模板文件中扩展 token @repository.version@
:
<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>
关于git - 如何从 ant 构建脚本中查找最新的 git commit hash,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2974106/