Pages

Friday, January 13, 2017

Saving current GIT commit hash in manifest

Sometimes I can be quite handy to save commit hash in the manifest of the built jar. If the development is quite intense and many successive versions of jar are installed in many locations, it might be difficult to determine the commit revision corresponding to the code in a particular jar, unless the project version and thereby filename is updated with each commit. The commit hash included in a jar allows easy recovery of the corresponding source code.

Build Number Maven Plugin allows generating GIT-related properties such as git hash or branch. Then those properties can be saved in some file with help of another plugin. The following lines in pom.xml allows saving git hash into manifest.mf:

    <scm>
        <connection>scm:git:https://test@test.git.beanstalkapp.com/test.git</connection>
        <developerConnection>scm:git:https://test@test.git.beanstalkapp.com/test.git</developerConnection>
        <tag>HEAD</tag>
    </scm>
    <build>
        <plugins>             
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>buildnumber-maven-plugin</artifactId>
                <version>1.3</version>
                <executions>
                    <execution>
                        <phase>validate</phase>
                        <goals>
                            <goal>create</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <doCheck>false</doCheck>
                    <doUpdate>false</doUpdate>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.6</version>
                <configuration>                  
                    <archive> 
                        <manifest>
                            <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
                        </manifest>
                        <manifestEntries>
                            <Commit>${buildNumber}</Commit>
                            <Branch>${scmBranch}</Branch>
                            <Build-Time>${maven.build.timestamp}</Build-Time>
                        </manifestEntries> 
                    </archive>                  
                </configuration>
            </plugin>   
        </plugins>
    </build>

The manifests in jar files built with such settings will contain the useful attributes that might facilitate debugging such as commit hash, branch and build time.

No comments:

Post a Comment