如何从ant构建脚本中查找最新的git commit hash

mch*_*chr 55 git ant

如何从ant构建脚本中查找最新的git commit hash?

我目前正在开发一个新的开源项目,我存储在github上.我想扩展我现有的ANT构建文件,以允许我创建编号的构建.我想我会用"ant buildnum -Dnum = 12"之类的东西启动构建.

我希望生成的jar在它的清单文件中有两个关键信息:

  • build.number = 12
  • build.gitcommit =

我知道如何创建build.number行.但是,我不确定最好的ant管道来查找最新的git commit hash,这是我想填写的值.

jmu*_*muc 82

我在github上为一个项目编写了以下ant目标.用法:

  • 将属性存储在属性"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>
Run Code Online (Sandbox Code Playgroud)

它例如用于@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>
Run Code Online (Sandbox Code Playgroud)


Gia*_*rdi 22

此命令始终返回工作文件夹的最后一次提交SHA1,当您不总是从HEAD构建时非常有用.该命令应该在Windows和*nix系统上运行

<exec executable="git" outputproperty="git.revision">
    <arg value="log" />
    <arg value="-1" />
    <arg value="--pretty=format:%H" />
</exec>
Run Code Online (Sandbox Code Playgroud)


Oli*_*ier 8

那会是你想要的吗?

git rev-parse HEAD
Run Code Online (Sandbox Code Playgroud)


mch*_*chr 5

我实际上使用了两个答案。我写的蚂蚁代码如下。

  <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>