Enable git commit hash in jar version

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

I am trying to follow this tutorial .

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

+15
source share
2 answers

One way to achieve this is to use 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> 

Note that I changed the phase to validate , so the version number property is already available when the artifact is packaged.

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> 

The git.commit.id.describe-short property is created by git-commit-id-plugin . It contains the current git version number (reduced to 7 digits) and an optional dirty indicator.

The examplelib-1.0.2-efae3b9.jar artifact will look like this: examplelib-1.0.2-efae3b9.jar (or examplelib-1.0.2-efae3b9-dirty.jar if there are uncommitted changes in your repository).

In addition, you can also put this information in MANIFEST.MF of your artifact. In that 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 notes:

  • I showed a simple git-commit-id-plugin configuration. On their website you can find additional parameters and properties. In addition to the properties that can be used inside pom.xml, the plugin can also generate a properties file containing revision information.

  • As an alternative to git-commit-id-plugin you may prefer buildnumber-maven-plugin . To get version numbers, this plugin requires the SCM plugin, also configured in your pom.xml.

  • This setting may interfere with other plugins that convert or rename your artifacts (in my case it was maven-shade-plugin - one of the sources that it creates did not contain the correct version number).

+29
source

The above answer does not work for me. I found the link https://dzone.com/articles/maven-git-commit-id-plugin , from where I copied the plugin code below. This worked for the first time for me. Now I have a git.properties file that is automatically included in my target JAR file. Very useful for tracking.

 <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 the build section so that the name of the target file also has a version

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

Source: https://habr.com/ru/post/1259354/


All Articles