简体   繁体   中英

maven-shade-plugin add application version to manifest

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!

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM