Tuesday, January 24, 2017

Git Commit Maven Plugin

I'm not sure how I survived so long without this. As a developer, almost everyone has run into a situation where a bug is reported but the question is what version. For releases there's generally a tag that identifies the release and makes it easy to find the source code to begin the investigation. But, often, especially with snapshot builds, it may be less clear. This simple addition to the Maven pom.xml (below) creates a git.properties file in your build artifact. There are many properties you can include and I've excluded some that were not particularly useful for my purposes. The most important, in my opinion include:

git.dirty=true
git.commit.time=24.01.2017 @ 07\:08\:22 MST
git.branch=master
git.commit.id=79b91f49b53eab5fe96efd2a2d9438ea3e3a0d00

git.tags=

The git.dirty indicates there are uncommitted changes in git. This build should never be dirty since if it is the commit hash does not reflect what was built. I believe you can fail the build if dirty. With web apps it's easy to expose this information via an endpoint. Also a good practice is to print the contents of the file to a log on start-up. Big tip of the hat goes to Konrad Malawski (akka core developer) for developing this plugin.


<plugin>
   <groupId>pl.project13.maven</groupId>
   <artifactId>git-commit-id-plugin</artifactId>
   <version>2.2.1</version>
   <executions>
      <execution>
         <id>get-the-git-infos</id>
         <goals>
            <goal>revision</goal>
         </goals>
      </execution>
      <execution>
         <id>validate-the-git-infos</id>
         <goals>
            <goal>revision</goal>
         </goals>
         <phase>package</phase>
      </execution>
   </executions>

   <configuration>
      <generateGitPropertiesFile>true</generateGitPropertiesFile>
      <generateGitPropertiesFilename>${project.build.outputDirectory}/git.properties</generateGitPropertiesFilename>
      <excludeProperties>
         <excludeProperty>git.build.*</excludeProperty>
         <excludeProperty>git.commit.user.email</excludeProperty>
         <excludeProperty>git.commit.message.*</excludeProperty>
         <excludeProperty>git.commit.id.describe</excludeProperty>
         <excludeProperty>git.commit.user.name</excludeProperty>
         <excludeProperty>git.remote.origin.url</excludeProperty>
         <excludeProperty>git.commit.id.abbrev</excludeProperty>
         <excludeProperty>git.closest.tag.name</excludeProperty>
         <excludeProperty>git.closest.tag.commit.count</excludeProperty>
         <excludeProperty>git.remote.origin.url</excludeProperty>
      </excludeProperties>
   </configuration>
</plugin>

No comments:

Post a Comment