java 在 jar 版本中包含 git commit hash
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40446275/
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
Include git commit hash in jar version
提问by Herr Kater
I'm using maven and my goal is to include the git commit hash in the version number. Something like : 1.1.{git_hash}.
我正在使用 maven,我的目标是在版本号中包含 git commit 哈希。类似于:1.1.{git_hash}。
I'm trying to follow this tutorial.
我正在尝试按照本教程进行操作。
Q: is it possible to somehow override the version number specified in the version element of the pom file?
问:是否可以以某种方式覆盖在 pom 文件的 version 元素中指定的版本号?
回答by user3078523
One way to achieve this is to use the git-commit-id-plugin. Add this to the list of plugins in the build
section of your pom.xml:
实现此目的的一种方法是使用git-commit-id-plugin。将此添加到build
pom.xml 部分的插件列表中:
<plugin>
<groupId>pl.project13.maven</groupId>
<artifactId>git-commit-id-plugin</artifactId>
<version>${git-commit-id-plugin.version}</version>
<executions>
<execution>
<id>get-the-git-infos</id>
<goals>
<goal>revision</goal>
</goals>
<phase>validate</phase>
</execution>
</executions>
<configuration>
<dotGitDirectory>${project.basedir}/.git</dotGitDirectory>
</configuration>
</plugin>
Note, that I've changed the phase to validate
, so the revision number property is already available in when the artifact is packaged.
请注意,我已将阶段更改为validate
,因此在打包工件时修订号属性已经可用。
Then, add the following to the build
section:
然后,将以下内容添加到该build
部分:
<build>
<finalName>${project.artifactId}-${project.version}-${git.commit.id.describe-short}</finalName>
<!-- your list of plugins -->
</build>
The git.commit.id.describe-short
property is produced by the git-commit-id-plugin
. It contains current git revision number (shortened to 7 digits) and an optional dirty
indicator.
该git.commit.id.describe-short
物业由git-commit-id-plugin
. 它包含当前的 git 修订号(缩短为 7 位数字)和一个可选的dirty
指示符。
The produced artifact will look like this: examplelib-1.0.2-efae3b9.jar
(or examplelib-1.0.2-efae3b9-dirty.jar
in case there are uncommitted changes on your repository).
生成的工件将如下所示:(examplelib-1.0.2-efae3b9.jar
或者examplelib-1.0.2-efae3b9-dirty.jar
如果您的存储库中有未提交的更改)。
Additionally, you might also want to put this information to the MANIFEST.MF of your artifact. In such case add this to your list of plugins (the example assumes the artifact is a jar
):
此外,您可能还想将此信息放入工件的 MANIFEST.MF。在这种情况下,将此添加到您的插件列表中(该示例假定工件是 a jar
):
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifestEntries>
<SCM-Revision>${git.commit.id.describe-short}</SCM-Revision>
</manifestEntries>
</archive>
</configuration>
</plugin>
Additional remarks:
补充说明:
I've shown a simple configuration of the
git-commit-id-plugin
. On their site you may find more options and properties. In addition to properties, that can be used inside pom.xml, the plugin can also generate a properties file containing information about revision.As an alternative to
git-commit-id-plugin
, you might prefer buildnumber-maven-plugin. In order to get revision numbers this plugin requires a SCMplugin also configured in your pom.xml.This setup may interfere with other plugins that transform or rename your artifacts (in my case it was the maven-shade-plugin - one of the sources jar it produces did not contain proper revision number).
我已经展示了
git-commit-id-plugin
. 在他们的网站上,您可以找到更多选项和属性。除了可以在 pom.xml 中使用的属性之外,该插件还可以生成一个包含修订信息的属性文件。作为 的替代方案
git-commit-id-plugin
,您可能更喜欢buildnumber-maven-plugin。为了获得修订号,这个插件需要一个也在你的 pom.xml 中配置的SCM插件。此设置可能会干扰转换或重命名工件的其他插件(在我的情况下,它是 maven-shade-plugin - 它生成的源 jar 之一不包含正确的修订号)。
回答by DAB
The above accepted answer didn't work for me. I found the link https://dzone.com/articles/maven-git-commit-id-plugin, from where I copied the plugin code below. It worked first time for me. I now have the git.properties file automatically included in my target JAR file. Very useful for tracking.
上面接受的答案对我不起作用。我找到了链接https://dzone.com/articles/maven-git-commit-id-plugin,从那里我复制了下面的插件代码。它第一次对我有用。我现在有 git.properties 文件自动包含在我的目标 JAR 文件中。对跟踪非常有用。
<plugin>
<groupId>pl.project13.maven</groupId>
<artifactId>git-commit-id-plugin</artifactId>
<version>2.2.4</version>
<executions>
<execution>
<id>get-the-git-infos</id>
<goals>
<goal>revision</goal>
</goals>
</execution>
</executions>
<configuration>
<dotGitDirectory>${project.basedir}/.git</dotGitDirectory>
<prefix>git</prefix>
<verbose>false</verbose>
<generateGitPropertiesFile>true</generateGitPropertiesFile>
<generateGitPropertiesFilename>${project.build.outputDirectory}/git.properties</generateGitPropertiesFilename>
<format>json</format>
<gitDescribe>
<skip>false</skip>
<always>false</always>
<dirty>-dirty</dirty>
</gitDescribe>
</configuration>
Add finalName to build section to also have the version in the target file name
将 finalName 添加到构建部分以在目标文件名中也包含版本
<build>
<finalName>${project.artifactId}-${project.version}-${git.commit.id.describe-short}</finalName>
...
</build>