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

标签 git ant

如何从 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/

相关文章:

git - 如何在 git diff 命令中显示相对路径?

git - 下载 Azure DevOps Git Monorepo 下特定目录的源文件

带有附加信息的 Git 日志装饰图

git - 使用 git 克隆 https url 时出现证书问题

ant - 为什么我不能在 ant <dirset> 上使用 <date> 选择器?

java - Ivy :publish work?怎么办

git - 停止 mvn release 触发重复 Jenkins 构建

java - 如何使用 Ant 检查已签名的 jar 文件?

php - 在 Phing(和 Ant)构建脚本中选择属性

java - 如何在 Mac OS X 上使用 Ant 复制 .​​app 包?