I'm using maven and my goal is to include the git commit hash in the version number. Something like : 1.1.{git_hash}.
I'm trying to follow this tutorial.
Q: is it possible to somehow override the version number specified in the version element of the pom file?
If you use this extension, a magic clipboard icon appears in next to commit hash text. You can just click the icon to copy the commit hash to clipboard.
The Maven git commit id plugin is indispensible and should be added to each Maven project. It automatically creates the versioning information during a build in order that you are able to verify the versioning information when an application is deployed.
If you have the hash for a commit, you can use the git show command to display the changes for that single commit. The output is identical to each individual commit when using git log -p .
# open the git config editor $ git config --global --edit # in the alias section, add ... [alias] lastcommit = rev-parse HEAD ... From here on, use git lastcommit to show the last commit's hash.
Even better than a version number is the git hash of the commit that was used to build the software release. In this article I’m going to describe my function that I came up with to do just that. Let’s start with the code to read the git hash from the current source directory.
Here, %H means commit hash. As an alternative, you can use the git-rev-parse command, which will return the hash of the latest git commit: If you want to turn references (branches and tags) into hash, you can use git show-ref and git for-each-ref commands.
Thus, I would like to automatically include Git information, such as commit hash and branch, in the compiled C and/or C++ code. The development environment has developers using both Windows and Linux, and uses Eclipse CDT with a relatively unsophisticated workflow of: check out; compile; download to the hardware. Show activity on this post.
Using the git reflog command is also used if you want to have the history on the head of your branches. With this command, you can find the line referring to the state you want to get back. After getting the hash of the commit you can restore it by using git cherry-pick.
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>
Note, that I've changed the phase to validate
, so the revision number property is already available in 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 produced by the git-commit-id-plugin
. It contains current git revision number (shortened to 7 digits) and an optional dirty
indicator.
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).
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:
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.
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.
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).
The above accepted answer didn't work for me. I found the link https://dzone.com/articles/maven-git-commit-id-plugin, from where I copied the plugin code below. It worked first time for me. I now have the git.properties file 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 build section to also have the version in the target file name
<build>
<finalName>${project.artifactId}-${project.version}-${git.commit.id.describe-short}</finalName>
...
</build>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With