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 themaven-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