Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从ant构建脚本中查找最新的git提交哈希_Git_Ant - Fatal编程技术网

如何从ant构建脚本中查找最新的git提交哈希

如何从ant构建脚本中查找最新的git提交哈希,git,ant,Git,Ant,如何从ant构建脚本中查找最新的git提交哈希 我目前正在从事一个新的开源项目,该项目存储在github上。我想扩展我现有的ANT构建文件,以允许我创建编号的构建。我想象着我会用类似“antbuildnum-Dnum=12”的东西启动构建 我希望生成的jar在其清单文件中包含两个关键信息: build.number=12 build.gitcommit= 我知道如何创建build.number行。但是,我不确定查找最新git提交哈希的最佳ant管道,这是我要填写的值。这就是您要查找的吗 gi

如何从ant构建脚本中查找最新的git提交哈希

我目前正在从事一个新的开源项目,该项目存储在github上。我想扩展我现有的ANT构建文件,以允许我创建编号的构建。我想象着我会用类似“antbuildnum-Dnum=12”的东西启动构建

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

  • build.number=12
  • build.gitcommit=

我知道如何创建build.number行。但是,我不确定查找最新git提交哈希的最佳ant管道,这是我要填写的值。

这就是您要查找的吗

git rev-parse HEAD
您应该(从0.1或类似版本开始),然后只使用
git descripe


这将为您提供可读的唯一标识符作为标记的参考点。当您发布时,这个版本号将是您指定的任何版本号。

我实际上使用了这两个答案。我编写的ant代码如下

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


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

<exec executable="git" outputproperty="git.revision">
    <arg value="log" />
    <arg value="-1" />
    <arg value="--pretty=format:%H" />
</exec>

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

  • 将版本存储在属性“repository.version”中
  • 如果未安装git或不存在.git目录(回退),则此功能有效
  • 如果其他目标需要git版本,则必须依赖此目标
  • 只执行一个git命令(--始终)


我编写了一个Ant任务来确定构建版本,而无需显式调用Git命令,这样我就不需要安装它(在Windows上,我还需要将它包含在
路径
中)。版本控制工作流:

  • 任何“里程碑”版本更改(即前2或3个数字)都是通过branch
    master
    上的标签手动设置的
  • 标记后面的每个提交都会添加到内部版本号。(仅适用于
    master
    上的标签)
  • 如果从单独的分支进行构建,则其名称应包含在版本中

源代码和示例:

在一家大公司,我发现git命令行很快就遇到了问题,一些开发人员使用GUI,一些人在不同的地方安装了不同的命令行版本,一些人还有其他问题。我意识到,接下来要做的是一个纯Java解决方案,依赖项是项目构建系统的一部分,就像我们以前在Svnkit for Subversion中使用的一样

一个条件是只允许“主流”库依赖项。我们可以使用JGit库,但许多分散在github周围的git-ant任务项目和类似项目被排除在外

解决方案是结合使用inbuild.xml和JGit库


TODO:粘贴代码…

Ii将此目标集成到我的build.xml中,但是如何将此目标的输出获取到文件中?@MartinSchlagnitweit您可以使用
echo
,例如
${repository.version}
@MartinSchlagnitweit您还可以在模板文件中使用令牌
@repository.version@
,并扩展git版本(参见上文原始答案的补充)。此外,如果工作副本包含本地修改,您可以添加
,它将在
${svn.revision}
中附加“
-dirty
”。@StevenVascellaro:“应存储命令标准错误的属性名称”我必须将其用作命令:
“C:\Program Files\Git\Git cmd.exe”Git log-1--pretty=格式:%H
Git version 2.5.1.windows.1
)当前代码段在具有
Git version 2.5.3.windows.1
<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>
<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>