1

We want to insert the current git commit hash in our MANIFEST.MF. Unfortunately, we aren´t allowed to use maven-git-commit-id-plugin, so we took the following approach:

  • create a class that reads the commit hash and sets it in a system property
  • run this class at package time with the exec-maven-plugin
  • read the property while modifying the MANIFEST.MF in the maven-jar-plugin

This, however, doesn't work as expected. Since the tag where the hash appears is blank at the end. It doesn´t even has the default value we set at the beginning.

  • The plugin execution order is correct.
  • We are able to access other System Properties.

The Class in question:

public class GetGitHash {

    public static void main(String... args) {
        String gitHash = "ZUFALL";
        System.out.println("1. Git Hash: " + System.getProperty("githash"));
        try{
            ProcessBuilder pb = new ProcessBuilder("git", "rev-parse", "--short", "HEAD");
            pb.redirectInput(ProcessBuilder.Redirect.INHERIT);
            Process p = pb.start();

            gitHash = new BufferedReader(new InputStreamReader(p.getInputStream()))
                    .lines().collect(Collectors.joining(" "));
        }
        catch (Exception e){
            e.printStackTrace();
        }
        finally {
            System.setProperty("githash", gitHash);
        }
        System.out.println("2. Git Hash: " + System.getProperty("githash"));
    }

}

The maven-exec configuration in the pom file:

<plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.6.0</version>
                <executions>
                    <execution>
                        <id>git-hash</id>
                        <configuration>
                            <mainClass>PACKAGE.GetGitHash</mainClass>
                            <systemProperties>
                                <systemProperty>
                                    <key>githash</key>
                                    <value>ZUFALL</value>
                                </systemProperty>
                            </systemProperties>
                        </configuration>
                        <phase>package</phase>
                        <goals>
                            <goal>java</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

The jar plugin configuration in the pom file:

<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.1.0</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>test-jar</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <archive>
                        <addMavenDescriptor>false</addMavenDescriptor>
                        <manifestEntries>
                            <Library-Version>${project.version}</Library-Version>
                            <Build-Timestamp>${maven.build.timestamp}</Build-Timestamp>
                            <Build-ID>${githash}</Build-ID>
                        </manifestEntries>
                    </archive>
                </configuration>
            </plugin>

In the console output, it can be seen that the property is set:

[INFO] --- exec-maven-plugin:1.6.0:java (git-hash) @ PROJECT ---
1. Git Hash: ZUFALL
2. Git Hash: 0003dfa9

However the MANIFEST looks like this:

Manifest-Version: 1.0
Created-By: Apache Maven 3.3.9
Built-By: USER
Build-Jdk: 11
Build-ID: 
Build-Timestamp: 2019-07-19T09:49:36Z
Library-Version: 0.9.2
Dmitriy Popov
  • 2,150
  • 3
  • 25
  • 34
  • Use buildnumber-maven-plugin or other existing plugins any other approach is error prone and costs more effort than worth... – khmarbaise Jul 19 '19 at 10:50
  • @khmarbaise In the project page is not clear which license they use, and due to enterprise policies, we are not allowed to use GPL, Apache is fine, however. Nonetheless, I think that the idea of the maven-git-commit-id-plugin was to offer the git capabilities that buildnumber-maven-plugin didn't offer. I might be wrong however – Enrique Castaneda Jul 19 '19 at 17:17
  • buildnumber-maven-plugin is MIT Licensed which is not relevant cause only things which will bepart of your application is a problem..The https://github.com/git-commit-id/maven-git-commit-id-plugin is GNU Lesser General Public License v3.0 which means you are not allowed to create a new plugin of it and sell it..but you are allowed to use it (But I'm not a lawyer)...The licenses can be looked in the repositories (LICENSE file)...but the license of the code is something different than using the plugin ... – khmarbaise Jul 19 '19 at 18:21
  • Licenses can be looked here: https://github.com/git-commit-id/maven-git-commit-id-plugin/blob/master/LICENSE and here: https://github.com/mojohaus/buildnumber-maven-plugin/blob/master/LICENSE.txt – khmarbaise Jul 19 '19 at 18:23
  • I will check that out on Monday, for this specific task. However, I found odd, that the properties in the pom are not working as advertised. Either I am doing something wrong or there is a bug. I think the first option is more likely. – Enrique Castaneda Jul 19 '19 at 21:42
  • I can confirm that the buildnumber-maven-plugin is the solution we are taking for this specific task. However I´d love to know why you can´t set variables to be read in the pom file. My only reasoning is that maven-exec-plugin its own JVM starts appart from that of the building proccess. – Enrique Castaneda Jul 24 '19 at 10:35

0 Answers0