在使用Maven Shade插件时如何指定自定义MANIFEST.MF文件? [英] How can I specify a custom MANIFEST.MF file while using the Maven Shade plugin?

查看:160
本文介绍了在使用Maven Shade插件时如何指定自定义MANIFEST.MF文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当一个项目使用Maven-jar-plugin时,很容易在jar中包含一个自定义清单文件,但是我找不到使用Maven shade做相同事情的方法.在使用"Maven-shade-plugin"时如何使用自己的清单文件?

When a project uses the Maven-jar-plugin, it's easy to include a custom manifest file in the jar, but I can't find a way to do the same thing with Maven shade. How can I use my own manifest file while using the "Maven-shade-plugin"?

其他详细信息:

我的自定义清单文件位于"src/main/resources/META-INF/MANIFEST.MF"中.Maven不包括我的文件,而是在最终的jar中将其替换为默认的Maven清单文件.

My custom manifest file is located in "src/main/resources/META-INF/MANIFEST.MF". Maven is not including my file, instead it is being replaced in the final jar with a default Maven manifest file.

我需要一个自定义清单文件的原因是为清单组件库在清单中指定一些JavaBeans类.应该在清单文件中以以下格式指定多个JavaBeans类,例如

The reason I need a custom manifest file is to specify some JavaBeans classes in my manifest, for a swing component library. Multiple JavaBeans classes should be specified in the manifest file in the following format, as described here. Note that the empty lines (and the line grouping) are important for marking JavaBeans classes in the manifest.

Name: SomeBean1.class
Java-Bean: True

Name: SomeBean2.class
Java-Bean: True

Name: SomeBean3.class
Java-Bean: True

尝试的解决方案列表(这些方法无效):

  1. 此代码仅在使用Maven jar插件(而非阴影)时有效.

  1. This code only works when using the Maven jar plugin (not shade).

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
      <archive>
        <manifestFile>src/main/resources/META-INF/MANIFEST.MF</manifestFile>
      </archive>
    </configuration>
  </plugin>

  • 此链接表示这里的所有示例中,都可以在所有使用Maven Archiver的插件中使用此配置,而不仅仅是本示例中的Maven-jar-plugin."根据该建议,我尝试了以下代码,但这也不起作用.(Maven仍用默认清单文件替换了清单文件.)

  • This link says "As with all the examples here, this configuration can be used in all plugins that use Maven Archiver, not just Maven-jar-plugin as in this example." Based on that advice, I tried the following code, but this did not work either. (Maven still replaced my manifest file with the default manifest file.)

        <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> 
                        <archive>
                            <manifestFile>src/main/resources/META-INF/MANIFEST.MF</manifestFile>
                        </archive>
                        <shadedArtifactAttached>true</shadedArtifactAttached>
                        <shadedClassifierName>core</shadedClassifierName>
                        <createDependencyReducedPom>false</createDependencyReducedPom>
                        <minimizeJar>true</minimizeJar>      
                    </configuration>
                </execution>
            </executions>
        </plugin>
    

  • 我不能将阴影"ManifestResourceTransformer"用作

  • I cannot use the shade "ManifestResourceTransformer" as described here to do the job, for the following reason. I need to add JavaBeans classes to my manifest file as described above under "additional details". However, if I add manifest entries using the shade ManifestResourceTransformer, those entries are inserted into the manifest file in an unpredictable (random-seeming) ordering. For specifying JavaBeans classes, the ordering of the manifest entries (the line order) is important.

    我尝试使用IncludeResourceTransformer,但是以下代码产生以下错误:创建阴影jar时出错:重复的条目:META-INF/MANIFEST.MF".

    I attempted to use IncludeResourceTransformer, but the below code produces the following error: "Error creating shaded jar: duplicate entry: META-INF/MANIFEST.MF".

                    <configuration> <shadedArtifactAttached>true</shadedArtifactAttached>
                        <shadedClassifierName>core</shadedClassifierName><createDependencyReducedPom>false</createDependencyReducedPom>
                        <minimizeJar>true</minimizeJar>    
                        <transformers>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.IncludeResourceTransformer">
                                <resource>META-INF/MANIFEST.MF</resource>
                                <file>src/main/resources/META-INF/MANIFEST.MF</file>
                            </transformer>
                        </transformers>  
                    </configuration>
    

  • 推荐答案

    以下pom配置允许程序员使用自定义清单文件替换由Apache Maven Shade插件创建的清单文件.自定义清单文件应放在Maven项目的以下目录中:"src/main/resources/META-INF/MANIFEST.MF"

    The following pom configuration allows the programmer to replace the manifest file created by Apache Maven Shade plugin, with a custom manifest file. The custom manifest file should be placed in this directory in the maven project: "src/main/resources/META-INF/MANIFEST.MF"

            <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>
    
                                <!-- Don't do this: Avoid adding anything that makes shade create or modify a manifest file.
                                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>com.mypackage.MyMainClass</mainClass>
                                </transformer>
                                -->
    
                                <!-- Add a transformer to exclude any other manifest files (possibly from dependencies). -->
                                <transformer implementation="org.apache.maven.plugins.shade.resource.DontIncludeResourceTransformer">
                                    <resource>MANIFEST.MF</resource>
                                </transformer>
    
                                <!-- Add a transformer to include your custom manifest file. -->
                                <transformer implementation="org.apache.maven.plugins.shade.resource.IncludeResourceTransformer">
                                    <resource>META-INF/MANIFEST.MF</resource>
                                    <file>src/main/resources/META-INF/MANIFEST.MF</file>
                                </transformer>
    
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
    

    这篇关于在使用Maven Shade插件时如何指定自定义MANIFEST.MF文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆