I can't figure out how to get the maven-shade-plugin to include the application version from POM file into the Manifest file. I found some examples for maven-jar-plugin which suggests including
<archive>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
<addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
</manifest>
</archive>
within the plugis configuration section (http://blog.soebes.de/blog/2014/01/02/version-information-into-your-appas-with-maven/). I tried this for the maven-shade-plugin, but it doesn't work. I also tried to find some information if the org.apache.maven.plugins.shade.resource.ManifestResourceTransformer can do this but I could not find anything in the docs.
Has anybody an idea how to do this?
Thanks!
Apache Maven Shade Plugin/ Usage. | Last Published: 2022-09-11. Version: 3.4.0.
In Java, to “shade” a dependency is to include all its classes and the classes from its transitive dependencies in your project, often renaming the packages and rewriting all affected bytecode.
maven-shade-plugin : It packages all dependencies into one uber-jar. It can also be used to build an executable jar by specifying the main class. This plugin is particularly useful as it merges content of specific files instead of overwriting them by Relocating Classes.
Just as described in official instruction page about adding entries into manifest file, Version and Title of Implementation and specification could also be supported because they are entries of the manifest file.
However Apache Maven Archiver is not supported in Maven Shade Plugin, so <archive>
element is not work here. You have to use ManifestResourceTransformer
, provided by Maven Shade Plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Main-Class>${project.build.mainClass}</Main-Class>
<Specification-Title>${project.artifactId}</Specification-Title>
<Specification-Version>${project.version}</Specification-Version>
<Implementation-Title>${project.artifactId}</Implementation-Title>
<Implementation-Version>${project.version}</Implementation-Version>
<Implementation-Vendor-Id>${project.groupId}</Implementation-Vendor-Id>
</manifestEntries>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
This configuration is also used in my spring project, where I am able to directly retrieve version number via Application.class.getPackage().getImplementationVersion()
in java.
However simple replace to maven shade brings side effect for not including additional resource files as how <archive>
plugin does in default, which would make projects that have properties resources not work such as spring projects. In most cases therefore, maintaining dependencies by hand would be required along with maven shade, as the frequent usage example below:
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.handlers</resource>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.schemas</resource>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.tooling</resource>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Main-Class>${project.build.mainClass}</Main-Class>
<Specification-Title>${project.artifactId}</Specification-Title>
<Specification-Version>${project.version}</Specification-Version>
<Implementation-Title>${project.artifactId}</Implementation-Title>
<Implementation-Version>${project.version}</Implementation-Version>
<Implementation-Vendor-Id>${project.groupId}</Implementation-Vendor-Id>
</manifestEntries>
</transformer>
</transformers>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With