如何从ant构建脚本中查找最新的git commit hash?
我目前正在开发一个新的开源项目,我存储在github上。我想扩展我现有的ANT构建文件,以允许我创建编号的构建。我想我会用“ant buildnum -Dnum = 12”之类的东西启动构建。
我希望生成的jar在其清单文件中包含两个关键信息:
我知道如何创建build.number行。但是,我不确定最好的ant管道来查找最新的git commit hash,这是我想填写的值。
答案 0 :(得分:81)
我在github上为一个项目编写了以下ant目标。用法:
<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@
:
<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>
答案 1 :(得分:21)
此命令始终返回工作文件夹的最后一次提交SHA1,当您不总是从HEAD构建时非常有用。该命令应该在Windows和* nix系统上运行
<exec executable="git" outputproperty="git.revision">
<arg value="log" />
<arg value="-1" />
<arg value="--pretty=format:%H" />
</exec>
答案 2 :(得分:8)
这会是你想要的吗?
git rev-parse HEAD
答案 3 :(得分:4)
我实际上使用了两个答案。我写的蚂蚁代码如下。
<target name="getgitdetails" >
<exec executable="git" outputproperty="git.tagstring">
<arg value="describe"/>
</exec>
<exec executable="git" outputproperty="git.revision">
<arg value="rev-parse"/>
<arg value="HEAD"/>
</exec>
<if>
<contains string="${git.tagstring}" substring="cannot"/>
<then>
<property name="git.tag" value="none"/>
</then>
<else>
<property name="git.tag" value="${git.tagstring}"/>
</else>
</if>
</target>
答案 4 :(得分:2)
我编写了一个Ant任务来确定构建版本而不显式调用Git命令,因此我不需要安装它(在Windows上我还需要将它包含在PATH
中)。版本控制工作流程:
master
上的标记手动设置任何“里程碑”版本更改(即前2或3个数字)。master
上的代码。)答案 5 :(得分:1)
您应tag a version(以0.1或类似值开头)然后只使用git describe
。
这将为您提供可读的唯一标识符作为标记的参考点。当您发布时,此版本号将是您指定的版本号。
答案 6 :(得分:0)
在一家大公司,我发现<exec> git command-line
很快就遇到了问题,一些开发人员使用了GUI,一些人在不同的地方安装了不同的命令行版本,有些还有其他问题。我意识到要走的路是纯Java解决方案,其中依赖项是项目构建系统的一部分,就像我们之前使用Svnkit for Subversion一样。
一个条件是只允许“主流”库依赖项。我们可以使用JGit库,但是排除了分散在github周围的许多 git ant任务项目。
解决方案是使用build.xml和JGit库中的组合。
TODO:粘贴代码......