Java Ninja Chronicles By Norris Shelton

Things I learned in the pursuit of code

We had a need to include information about the current Git version of the files in the build for the purposes of troubleshooting the deploy process.

I stumbled upon the maven Git Commit Id plugin that provides that functionality.

Here is a list of the values that it makes available.

git.build.user.email
git.build.host
git.dirty
git.remote.origin.url
git.closest.tag.name
git.commit.id.describe-short
git.commit.user.email
git.commit.time
git.commit.message.full
git.build.version
git.commit.message.short
git.commit.id.abbrev
git.branch
git.build.user.name
git.closest.tag.commit.count
git.commit.id.describe
git.commit.id
git.tags
git.build.time
git.commit.user.name

These values are made available to replace elements in your specified directory that have the proper key and are surrounded with ${}. An example is.

<constructor-arg index="0" value="${git.commit.id.abbrev}"/>

Here are the steps to using it.

Step 1 – Set the resources directory as modifiable

<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
            </includes>
        </resource>
    </resources>
</build>

Step 2 – Include the Git Commit Id plugin

<plugins>
    <plugin>
        <groupId>pl.project13.maven</groupId>
        <artifactId>git-commit-id-plugin</artifactId>
        <version>2.2.0</version>
        <executions>
            <execution>
                <goals>
                    <goal>revision</goal>
                </goals>
            </execution>
        </executions>
        <configuration>
            <!--
                If you'd like to tell the plugin where your .git directory is,
                use this setting, otherwise we'll perform a search trying to
                figure out the right directory. It's better to add it explicitly IMHO.
            -->
            <dotGitDirectory>${project.basedir}/.git</dotGitDirectory>
        </configuration>
    </plugin>
</plugins>

Additional configuration

To have the plugin generate a file with all of it’s values, instead of replacing values in your source, use the generateGitPropertiesFile configuration. The file will be created in whatever directory you specified in the resources element in Step 1.

<configuration>
    <!-- this is false by default, forces the plugin to generate the git.properties file -->
    <generateGitPropertiesFile>true</generateGitPropertiesFile>
</configuration>

An example of a generated git.properties is below.

#Generated by Git-Commit-Id-Plugin
#Thu Jan 14 13:32:07 EST 2016
git.build.user.email=norris.shelton@java.ninja.com
git.build.host=cdimac0001.java.ninja.com
git.dirty=true
git.remote.origin.url=https\://github.java.ninja.com/my_project.git
git.closest.tag.name=
git.commit.id.describe-short=4b3fea6-dirty
git.commit.user.email=norris.shelton@java.ninja.com
git.commit.time=14.01.2016 @ 11\:19\:21 EST
git.commit.message.full=r18.i1 - US12193 - Fixing package scan in Spring Config files
git.build.version=3.17.0
git.commit.message.short=r18.i1 - US12193 - Fixing package scan in Spring Config files
git.commit.id.abbrev=4b3fea6
git.branch=R18tc
git.build.user.name=Norris Shelton
git.closest.tag.commit.count=
git.commit.id.describe=4b3fea6-dirty
git.commit.id=4b3fea6863da76d37980527b0cc28310f88d7540
git.tags=
git.build.time=14.01.2016 @ 13\:32\:07 EST
git.commit.user.name=Norris.Shelton

By default, the plugin will search for your .git directory. The plugin documentation recommends explicitly setting it. If you are using a multi-module project, you will need to point your dotGitDirectory to a directory above what it expects.

<configuration>
    <!--
        If you'd like to tell the plugin where your .git directory is,
        use this setting, otherwise we'll perform a search trying to
        figure out the right directory. It's better to add it explicitly IMHO.
    -->
    <dotGitDirectory>${project.basedir}/../.git</dotGitDirectory>
</configuration>

To instruct the plugin to work in verbose mode, specify the following configuration.

<configuration>
    <!-- false is default here, it prints some more information during the build -->
    <verbose>true</verbose>
</configuration>

January 15th, 2016

Posted In: Git, GitHub, Java, java ninja, Javaninja, Maven

Leave a Reply

Your email address will not be published. Required fields are marked *

LinkedIn Auto Publish Powered By : XYZScripts.com