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> </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).
source share