Java 在jar版本中包含git提交哈希
我正在使用maven,我的目标是在版本号中包含git提交哈希。类似于:1.1.{git_hash} 我正试着跟着这个Java 在jar版本中包含git提交哈希,java,maven,maven-versions-plugin,Java,Maven,Maven Versions Plugin,我正在使用maven,我的目标是在版本号中包含git提交哈希。类似于:1.1.{git_hash} 我正试着跟着这个 问:是否可能以某种方式覆盖pom文件的version元素中指定的版本号?实现这一点的一种方法是使用。将此添加到pom.xml的build部分的插件列表中: <plugin> <groupId>pl.project13.maven</groupId> <artifactId>git-commit-id-plugin&l
问:是否可能以某种方式覆盖pom文件的version元素中指定的版本号?实现这一点的一种方法是使用。将此添加到pom.xml的
build
部分的插件列表中:
<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>
git.commit.id.descripe-short
属性由git commit id插件生成。它包含当前的git修订号(缩短为7位)和可选的dirty
指示器
生成的工件如下所示:examplelib-1.0.2-efae3b9.jar
(或者examplelib-1.0.2-efae3b9-dirty.jar
,以防存储库中有未提交的更改)
此外,您可能还希望将此信息放入工件的MANIFEST.MF中。在这种情况下,将其添加到插件列表中(示例假设工件是jar
):
org.apache.maven.plugins
. 为了获得版本号,这个插件需要在pom.xml中配置一个插件
此设置可能会干扰其他转换或重命名您的工件的插件(在我的例子中,它是maven shade插件-它生成的一个源jar没有包含正确的修订号)
上述被接受的答案对我来说并不适用。我找到了链接,从那里我复制了下面的插件代码。这对我来说是第一次。现在,我的目标JAR文件中自动包含了git.properties文件。非常有用的跟踪
<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>
pl.project13.maven
git提交id插件
2.2.4
获取git信息
修订
${project.basedir}/.git
吉特
假的
真的
${project.build.outputDirectory}/git.properties
json
假的
假的
-肮脏的
将finalName添加到build部分,以便在目标文件名中也包含该版本
<build>
<finalName>${project.artifactId}-${project.version}-${git.commit.id.describe-short}</finalName>
...
</build>
${project.artifactId}-${project.version}-${git.commit.id.descripe-short}
...
您是如何做到这一点的?变量未暴露于pom
中,我得到消息:模板变量'git.commit.id.descripe-short'在'git commit id plugin'定义中没有值我已将执行阶段更改为'validate',因此,当工件打包时,revision number属性已经可用。请注意,资源筛选对于该插件的工作非常重要,不要忽略它!(true
)“尽管此插件试图与每个Maven版本兼容,但特定版本存在一些已知的限制。”请检查此项。我遇到的问题是,我使用shade库创建uber JAR。ID插件只标记普通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>
<build>
<finalName>${project.artifactId}-${project.version}-${git.commit.id.describe-short}</finalName>
...
</build>