方法一:使用git-commit-id-maven-plugin(推荐)
引入git-commit-id-maven-plugin
<build>
<plugins>
<plugin>
<groupId>io.github.git-commit-id</groupId>
<artifactId>git-commit-id-maven-plugin</artifactId>
<version>7.0.0</version>
<executions>
<execution>
<id>get-the-git-infos</id>
<goals>
<goal>revision</goal>
</goals>
<phase>initialize</phase>
</execution>
</executions>
<configuration>
<verbose>true</verbose>
<generateGitPropertiesFile>false</generateGitPropertiesFile> <commitIdGenerationMode>full</commitIdGenerationMode>
<gitDescribe>
<tags>true</tags>
</gitDescribe>
</configuration>
</plugin>
</plugins>
</build>
将tag写到MANIFEST
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<archive>
<manifestEntries>
<Tag>${git.closest.tag.name}</Tag>
<BuildTime>${git.build.time}</BuildTime>
</manifestEntries>
</archive>
</configuration>
</plugin>
</plugins>
</build>
执行打包命令
mvn clean package
查看jar包中的MANIFEST.MF文件
BuildTime: 2024-02-28T15:52:57+0800
Tag: v1.1.0
更多变量如下(需要加上前缀git.):
package pl.project13.core;
/**
* A class that represents all properties that may be generated by the plugin and exposed to maven.
*/
public class GitCommitPropertyConstant {
/**
* Represents the current branch name. Falls back to commit-id for detached HEAD.
*
* Note: When an user uses the {@code evaluateOnCommit} property to gather the
* branch for an arbitrary commit (really anything besides the default {@code HEAD})
* this plugin will perform a {@code git branch --points-at} which might
* return a comma separated list of branch names that points to the specified commit.
*/
public static final String BRANCH = "branch";
/**
* Represents the count of commits that your local branch is ahead in perspective to the remote branch
* (usually the case when your local branch has committed changes that are not pushed yet to the remote branch).
*
* <p>
*
* Note: To obtain the right value for this property this plugin should operate in online mode
* ({@code <offline>false</offline>}) so a {@code git fetch} will be performed before retrieval.
*/
public static final String LOCAL_BRANCH_AHEAD = "local.branch.ahead";
/**
* Represents the count of commits that your local branch is behind in perspective to the remote branch
* (usually the case when there are commits in the remote branch that are not yet integrated into your local branch).
*
* <p>
*
* Note: To obtain the right value for this property this plugin should operate in online mode
* ({@code <offline>false</offline>}) so a {@code git fetch} will be performed before retrieval.
*/
public static final String LOCAL_BRANCH_BEHIND = "local.branch.behind";
/**
* A working tree is said to be "dirty" if it contains modifications
* which have not been committed to the current branch.
*/
public static final String DIRTY = "dirty";
/**
* Represents the commit’s SHA-1 hash. Note this is exchangeable with the git.commit.id.full property
* and might not be exposed. See {@code commitIdGenerationMode}.
*/
public static final String COMMIT_ID_FLAT = "commit.id";
/**
* Represents the commit’s SHA-1 hash. Note this is exchangeable with the git.commit.id property
* and might not be exposed. See {@code commitIdGenerationMode}.
*/
public static final String COMMIT_ID_FULL = "commit.id.full";
/**
* Represents the abbreviated (shorten version) commit hash.
*/
public static final String COMMIT_ID_ABBREV = "commit.id.abbrev";
/**
* Represents an object a human readable name based on a the commit
* (provides {@code git describe} for the given commit).
*/
public static final String COMMIT_DESCRIBE = "commit.id.describe";
/**
* Represents the same value as git.commit.id.describe,
* just with the git hash part removed (the {@code g2414721} part from {@code git describe}).
*/
public static final String COMMIT_SHORT_DESCRIBE = "commit.id.describe-short";
/**
* Represents the git user name that is configured where the properties have been generated.
*/
public static final String BUILD_AUTHOR_NAME = "build.user.name";
/**
* Represents the git user eMail that is configured where the properties have been generated.
*/
public static final String BUILD_AUTHOR_EMAIL = "build.user.email";
/**
* Represents the (formatted) timestamp when the last build was executed.
* If written to the git.properties file represents the latest build time when that file was written / updated.
*/
public static final String BUILD_TIME = "build.time";
/**
* Represents the project version of the current project.
*/
public static final String BUILD_VERSION = "build.version";
/**
* Represents the hostname where the properties have been generated.
*/
public static final String BUILD_HOST = "build.host";
/**
* The git.build.number* variables are available on some hosted CIs and can be used to identify the
* "number" of the build. This represents a project specific build number.
*
* <p>
*
* Currently supported CIs:
* <ul>
* <li>Bamboo</li>
* <li>Hudson/Jenkins</li>
* <li>TeamCity</li>
* <li>Travis</li>
* <li>Gitlab CI (Gitlab >8.10 & Gitlab CI >0.5)</li>
* <li>Azure DevOps</li>
* <li>AWS CodeBuild</li>
* <li>Bitbucket Pipelines</li>
* </ul>
*/
public static final String BUILD_NUMBER = "build.number";
/**
* The git.build.number* variables are available on some hosted CIs and can be used to identify the
* "number" of the build. This represents a system wide unique build number.
*
* <p>
*
* Currently supported CIs:
* <ul>
* <li>TeamCity</li>
* <li>Travis</li>
* <li>Gitlab CI (Gitlab >11.0)</li>
* <li>AWS CodeBuild</li>
* </ul>
*/
public static final String BUILD_NUMBER_UNIQUE = "build.number.unique";
/**
* Represents the user name of the user who performed the commit.
*/
public static final String COMMIT_AUTHOR_NAME = "commit.user.name";
/**
* Represents the user eMail of the user who performed the commit.
*/
public static final String COMMIT_AUTHOR_EMAIL = "commit.user.email";
/**
* Represents the raw body (unwrapped subject and body) of the commit message.
* Similar to running
* <pre>
* git log -1 --pretty=format:%B
* </pre>
*/
public static final String COMMIT_MESSAGE_FULL = "commit.message.full";
/**
* Represents the subject of the commit message - may <b>not</b> be suitable for filenames.
* Similar to running
* <pre>
* git log -1 --pretty=format:%s
* </pre>
*/
public static final String COMMIT_MESSAGE_SHORT = "commit.message.short";
/**
* Represents the (formatted) time stamp when the commit has been performed.
*/
public static final String COMMIT_TIME = "commit.time";
/**
* Represents the (formatted) time stamp when the commit has been originally performed.
*/
public static final String COMMIT_AUTHOR_TIME = "commit.author.time";
/**
* Represents the (formatted) time stamp when the commit has been performed.
*/
public static final String COMMIT_COMMITTER_TIME = "commit.committer.time";
/**
* Represents the URL of the remote repository for the current git project.
*/
public static final String REMOTE_ORIGIN_URL = "remote.origin.url";
/**
* Represents a list of tags which contain the specified commit.
* Similar to running
* <pre>
* git tag --contains
* </pre>
*/
public static final String TAGS = "tags";
/**
* Represents the name of the closest available tag.
* The closest tag may depend on your git describe config that may or may not take lightweight tags into consideration.
*/
public static final String CLOSEST_TAG_NAME = "closest.tag.name";
/**
* Represents the number of commits to the closest available tag.
* The closest tag may depend on your git describe config that may or may not take lightweight tags into consideration.
*/
public static final String CLOSEST_TAG_COMMIT_COUNT = "closest.tag.commit.count";
/**
* Represents the total count of all commits in the current repository.
* Similar to running
* <pre>
* git rev-list HEAD --count
* </pre>
*/
public static final String TOTAL_COMMIT_COUNT = "total.commit.count";
}
官网:
GitHub - git-commit-id/git-commit-id-maven-plugin: Maven plugin which includes build-time git repository information into an POJO / *.properties). Make your apps tell you which version exactly they were built from! Priceless in large distributed deployments... :-)
方法二:使用maven-git-versioning-extension
Maven版本:3.6.3及以上

在.mvn目录下创建extensions.xml文件,内容如下:
<extensions xmlns="http://maven.apache.org/EXTENSIONS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/EXTENSIONS/1.0.0 https://maven.apache.org/xsd/core-extensions-1.0.0.xsd">
<extension>
<groupId>me.qoomon</groupId>
<artifactId>maven-git-versioning-extension</artifactId>
<version>7.3.0</version>
</extension>
</extensions>
在.mvn目录下创建maven-git-versioning-extension.xml文件,内容如下:
<configuration xmlns="https://github.com/qoomon/maven-git-versioning-extension"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://github.com/qoomon/maven-git-versioning-extension https://qoomon.github.io/maven-git-versioning-extension/configuration-9.4.0.xsd">
<refs considerTagsOnBranches="true">
<ref type="tag">
<pattern><![CDATA[.+]]></pattern>
<properties>
<tag>${ref}</tag>
<build.time>${commit.timestamp.datetime}</build.time>
</properties>
</ref>
</refs>
</configuration>
通过以下命令进行测试tag变量是否正确
mvn help:evaluate -Dexpression=tag -q -DforceStdout
将tag写到MANIFEST
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<archive>
<manifestEntries>
<Tag>${tag}</Tag>
<BuildTime>${build.time}</BuildTime>
</manifestEntries>
</archive>
</configuration>
</plugin>
</plugins>
</build>
执行打包命令
mvn clean package
查看jar包中的MANIFEST.MF文件
BuildTime: 2024-02-28T15:52:57+0800
Tag: v1.1.0
官网:
GitHub - qoomon/maven-git-versioning-extension: This extension will set project version, based on current Git branch or tag.