Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include git commit hash in jar version

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?

like image 351
Herr Kater Avatar asked Nov 06 '16 05:11

Herr Kater


People also ask

How do I copy a commit hash?

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.

What does git commit ID plugin do?

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.

How do I see commit hash?

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 .

How do I get the latest commit hash?

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

What is the git hash of the commit used to build?

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.

What does%h mean in Git?

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.

Can I include Git information in compiled C and/or C++ code?

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.

How do I get the history of a commit in Git?

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.


2 Answers

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:

  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.

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

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

like image 129
user3078523 Avatar answered Oct 19 '22 07:10

user3078523


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>
like image 4
DAB Avatar answered Oct 19 '22 07:10

DAB