在jar版本中包含git commit hash

Her*_*ter 15 java maven maven-versions-plugin

我正在使用maven,我的目标是在版本号中包含git commit hash.类似的东西:1.1.{git_hash}.

我正在尝试按照本教程.

问:是否有可能以某种方式覆盖pom文件的version元素中指定的版本号?

use*_*523 31

实现此目的的一种方法是使用git-commit-id-plugin.将其添加到buildpom.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>
Run Code Online (Sandbox Code Playgroud)

请注意,我已将阶段更改为validate,因此在打包工件时,版本号属性已可用.

然后,将以下内容添加到该build部分:

<build>
    <finalName>${project.artifactId}-${project.version}-${git.commit.id.describe-short}</finalName>
    <!-- your list of plugins -->
</build>
Run Code Online (Sandbox Code Playgroud)

git.commit.id.describe-short物业由该生产git-commit-id-plugin.它包含当前git版本号(缩写为7位数)和可选dirty指示符.

生成的工件将如下所示:( examplelib-1.0.2-efae3b9.jar或者examplelib-1.0.2-efae3b9-dirty.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>
Run Code Online (Sandbox Code Playgroud)

补充说明:

  1. 我已经展示了一个简单的配置git-commit-id-plugin.在他们的网站上,您可以找到更多选项和属性.除了可以在pom.xml中使用的属性之外,插件还可以生成包含有关修订信息的属性文件.

  2. 作为替代方案git-commit-id-plugin,您可能更喜欢buildnumber-maven-plugin.为了获得修订号,这个插件需要在pom.xml中配置一个SCM插件.

  3. 此设置可能会干扰转换或重命名工件的其他插件(在我的情况下,它是maven-shade-plugin - 它生成的jar源之一不包含正确的修订版号).

  • _“尽管此插件尝试与每个 Maven 版本兼容,但特定版本仍存在一些已知限制。”_ 检查此[兼容性表](https://github.com/ktoso/maven-git-commit-id-插件#plugin-compatibility-with-maven)。 (2认同)

DAB*_*DAB 8

上面接受的答案对我不起作用。我找到了链接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>
Run Code Online (Sandbox Code Playgroud)

将 FinalName 添加到构建部分,以便目标文件名中也包含版本

<build>

<finalName>${project.artifactId}-${project.version}-${git.commit.id.describe-short}</finalName>

...

</build>
Run Code Online (Sandbox Code Playgroud)