简体   繁体   English

maven-shade-plugin 将应用程序版本添加到清单

[英]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.我不知道如何让 maven-shade-plugin 将应用程序版本从 POM 文件包含到清单文件中。 I found some examples for maven-jar-plugin which suggests including我找到了一些 maven-jar-plugin 的例子,其中建议包括

<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/ ).在插件配置部分( 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.我为 maven-shade-plugin 尝试了这个,但它不起作用。 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.如果 org.apache.maven.plugins.shade.resource.ManifestResourceTransformer 可以做到这一点,我也试图找到一些信息,但我在文档中找不到任何内容。

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.但是 Maven Shade Plugin 不支持Apache Maven Archiver ,所以<archive>元素在这里不起作用。 You have to use ManifestResourceTransformer , provided by Maven Shade Plugin:您必须使用 Maven Shade Plugin 提供的ManifestResourceTransformer

<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.这个配置也用在我的spring项目中,我可以在java中通过Application.class.getPackage().getImplementationVersion()直接获取版本号。

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.然而,简单地替换到 maven shade 会带来副作用,因为它不包含额外的资源文件,因为<archive>插件在默认情况下会这样做,这会使具有属性资源的项目无法正常工作,例如 spring 项目。 In most cases therefore, maintaining dependencies by hand would be required along with maven shade, as the frequent usage example below:因此,在大多数情况下,需要手动维护依赖项以及 maven shade,如下面的频繁使用示例:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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