Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

maven-shade-plugin add application version to manifest

Tags:

java

maven

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!

like image 499
Ralf Avatar asked Aug 22 '17 08:08

Ralf


People also ask

What is Maven Shade plugin version?

Apache Maven Shade Plugin/ Usage. | Last Published: 2022-09-11. Version: 3.4.0.

How do you shade a dependency?

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.

How does maven shade plugin work?

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.


1 Answers

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>
like image 121
千木郷 Avatar answered Oct 14 '22 23:10

千木郷