在jar版本中包括git commit hash [英] Include git commit hash in jar version

查看:120
本文介绍了在jar版本中包括git commit hash的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Maven,我的目标是在版本号中包含git commit哈希.类似于:1.1.{git_hash}.

I'm using maven and my goal is to include the git commit hash in the version number. Something like : 1.1.{git_hash}.

我正在尝试遵循此教程.

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

Q: is it possible to somehow override the version number specified in the version element of the pom file?

推荐答案

一种方法是使用

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:

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

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

Note, that I've changed the phase to validate, so the revision number property is already available in when the artifact is packaged.

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

Then, add the following to the build section:

<build>
    <finalName>${project.artifactId}-${project.version}-${git.commit.id.describe-short}</finalName>
    <!-- your list of plugins -->
</build>

git.commit.id.describe-short属性由git-commit-id-plugin产生.它包含当前的git版本号(缩写为7位)和一个可选的dirty指示器.

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.

产生的工件将如下所示:examplelib-1.0.2-efae3b9.jar(如果存储库中有未提交的更改,则为examplelib-1.0.2-efae3b9-dirty.jar).

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

此外,您可能还希望将此信息放入工件的MANIFEST.MF.在这种情况下,请将其添加到您的插件列表中(本示例假设工件是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):

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

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

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

作为git-commit-id-plugin的替代方案,您可能更喜欢 buildnumber- Maven插件.为了获取修订号,此插件需要 SCM 插件也已在pom.xml中配置.

As an alternative to git-commit-id-plugin, you might prefer buildnumber-maven-plugin. In order to get revision numbers this plugin requires a SCM plugin also configured in your pom.xml.

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

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

这篇关于在jar版本中包括git commit hash的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆