如何从 ant 构建脚本中查找最新的 git commit hash
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2974106/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to lookup the latest git commit hash from an ant build script
提问by mchr
How can I lookup the latest git commit hash from an ant build script?
如何从 ant 构建脚本中查找最新的 git commit 哈希?
I am currently working on a new open source project which I store on github. I would like to extend my existing ANT build file to allow me to create numbered builds. I am imagining that I would launch the build with something like "ant buildnum -Dnum=12".
我目前正在开发一个存储在 github 上的新开源项目。我想扩展我现有的 ANT 构建文件以允许我创建编号的构建。我想象着我会用“ant buildnum -Dnum=12”之类的东西启动构建。
I would like the resulting jar to have two crucial bits of information in it's manifest file:
我希望生成的 jar 在它的清单文件中有两个关键的信息:
- build.number=12
- build.gitcommit=
- build.number=12
- 构建.gitcommit=
I know how to create the build.number line. However, I am unsure of the best ant plumbing to lookup the latest git commit hash which is the value I want to fill in for .
我知道如何创建 build.number 行。但是,我不确定查找最新的 git commit hash 的最佳蚂蚁管道,这是我想为 .git 填充的值。
回答by jmuc
I wrote the following ant target for a project on github. Usage:
我在 github 上为一个项目编写了以下 ant 目标。用法:
- stores version in property "repository.version"
- works if no git is installed or no .git directory is present (fallback)
- other targets must depend on this target if they need the git version
- only one git command gets executed (--always)
- 将版本存储在属性“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>
It e.g. be used for expanding the token @repository.version@
in a template file:
例如,它用于扩展@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>
回答by Gian Marco Gherardi
This command returns always the working folder's last commit SHA1, useful when you don't always build from HEAD. The command should run both on Windows and *nix systems
此命令始终返回工作文件夹的最后一次提交 SHA1,当您不总是从 HEAD 构建时很有用。该命令应该在 Windows 和 *nix 系统上运行
<exec executable="git" outputproperty="git.revision">
<arg value="log" />
<arg value="-1" />
<arg value="--pretty=format:%H" />
</exec>
回答by Olivier Verdier
Would that be what you are looking for?
那会是你要找的吗?
git rev-parse HEAD
回答by mchr
I actually used both answers. The ant code I wrote was as follows.
我实际上使用了两个答案。我写的蚂蚁代码如下。
<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>
回答by Hunternif
I wrote an Ant task to determine build version without explicitly calling the Git command, so that I don't need to have it installed (on Windows I'd also need to include it in PATH
). The versioning workflow:
我编写了一个 Ant 任务来确定构建版本,而没有显式调用 Git 命令,这样我就不需要安装它(在 Windows 上,我还需要将它包含在 中PATH
)。版本控制工作流程:
- Any "milestone" version changes (i.e. first 2 or 3 numbers) are set manually via tags on branch
master
. - Each commit subsequent to the tag adds to the build number. (Only for tags on
master
.) - If building from a separate branch, its name should be included in the version.
- 任何“里程碑”版本更改(即前 2 或 3 个数字)都是通过 branch 上的标签手动设置的
master
。 - 标签之后的每次提交都会添加到内部版本号。(仅适用于 上的标签
master
。) - 如果从单独的分支构建,其名称应包含在版本中。
Source code and examples: https://github.com/Hunternif/JGitVersion
回答by Dustin
You should tag a version(starting with a 0.1 or similar) and then just use git describe
.
您应该标记一个版本(从 0.1 或类似版本开始),然后只使用git describe
.
This will give you readable unique identifiers as reference points from your tag. When you release, this version number will be whatever you specified it to be.
这将为您提供可读的唯一标识符作为标签的参考点。当您发布时,此版本号将是您指定的任何内容。
回答by Ed Randall
In a large company I found <exec> git command-line
quickly ran into problems, some devs used a GUI, some had different command-line versions installed in different places, some had other problems. I realised that the way to go was a pure Java solution with the dependencies being part of the project build system, much as we had used before with Svnkit for Subversion.
我发现在一家大公司<exec> git command-line
很快就遇到了问题,一些开发人员使用了 GUI,一些在不同的地方安装了不同的命令行版本,一些有其他问题。我意识到要走的路是一个纯 Java 解决方案,将依赖项作为项目构建系统的一部分,就像我们之前在 Svnkit for Subversion 中使用的一样。
One condition was that only 'mainstream' library dependencies were permitted. We could use the JGit library but the many git ant tasksprojects scattered around github and the like were excluded.
一个条件是只允许“主流”库依赖项。我们可以使用 JGit 库,但不包括散布在 github 等的许多git ant 任务项目。
The solution was to use a combination of within build.xml and JGit library.
解决方案是结合使用 build.xml 和 JGit 库。
TODO: paste code...
待办事项:粘贴代码...