简体   繁体   English

来自具有相同名称的不同maven-modules的文件不能在使用maven assembly-plugin创建的jar文件中共存

[英]files from different maven-modules with the same name can not co-exist in a jar-file created with the maven assembly-plugin

If there are two files with different content but with the same name in two different maven-modules , wich are both put together in one jar-file with the maven assembly-plugin , only one file ends up being part of the .jar file. 如果有两个文件具有不同的内容但在两个不同的maven-modules中具有相同的名称,那么它们都与maven assembly-plugin放在一个jar文件中,只有一个文件最终成为.jar文件的一部分。

Question : Is there a way to ensure that the content of the files is assembled into one file when building the jar-file? 问题 :有没有办法确保在构建jar文件时将文件的内容组合到一个文件中?

I obviously do not want to put the information together manually, since this is what I am trying to avoid by splitting the project in different modules. 我显然不想手动将信息放在一起,因为这是我试图通过在不同模块中拆分项目来避免的。

EDIT: I have a custom Assembly-Descriptor that i would like to keep, even if I start using another plugin. 编辑:我有一个自定义的Assembly-Descriptor,我想保留,即使我开始使用另一个插件。 This Descriptor basically excludes every language but the english one for resources and error-texts. 这个描述符基本上排除了所有语言,但英语排除了资源和错误文本。

<id>jar-with-dependencies</id>
<formats>
    <format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
    <dependencySet>
        <outputDirectory>/</outputDirectory>
        <useProjectArtifact>true</useProjectArtifact>
        <unpack>true</unpack>
        <scope>runtime</scope>
        <unpackOptions>
            <excludes>
                <exclude>**/*Resources_*</exclude>
                <exclude>**/*ErrorsText_*</exclude>
            </excludes>
        </unpackOptions>
    </dependencySet>
</dependencySets>

As specified by the maven-assembly-plugin documentation: maven-assembly-plugin文档指定:

If your project wants to package your artifact in an uber-jar, the assembly plugin provides only basic support. 如果您的项目想要将工件打包在超级jar中,则程序集插件仅提供基本支持。 For more control, use the Maven Shade Plugin . 要获得更多控制,请使用Maven Shade插件


Using the maven-shade-plugin you can have a fat jar (like using the assembly plugin) and solve similar issues of merging file using Resources transformers . 使用maven-shade-plugin你可以有一个胖jar(比如使用程序集插件)并解决使用Resources变换器合并文件的类似问题。 In your case, the AppendingTransformer would merge files with the same name but with different content. 在您的情况下, AppendingTransformer将合并具有相同名称但具有不同内容的文件。

Some jars contain additional resources (such as properties files) that have the same file name. 某些jar包含具有相同文件名的其他资源(例如属性文件)。 To avoid overwriting, you can opt to merge them by appending their content into one file. 为避免覆盖,您可以选择通过将其内容附加到一个文件来合并它们。

A simple configuration would look like: 一个简单的配置看起来像:

<build>
    <plugins>
        <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.AppendingTransformer">
                                <resource>path/to/file/file-name-here</resource>
                            </transformer>
                        </transformers>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

Update 更新
You don't need an external assembly descriptor for the shade plugin, you can configure your requirements directly as plugin configuration. 您不需要为shade插件提供外部程序集描述符,您可以直接将您的需求配置为插件配置。
In your case, to exclude resources from assembled jars, you can use shade filters . 在您的情况下,要从已组装的jar中排除资源,您可以使用阴影过滤器

A simple configuration (to be merged with the one above) would look like: 一个简单的配置(与上面的一个合并)看起来像:

<configuration>
    <filters>
        <filter>
            <artifact>*:*</artifact>
            <excludes>
                <exclude>**/*Resources_*</exclude>
                <exclude>**/*ErrorsText_*</exclude>
            </excludes>
        </filter>
    </filters>
</configuration>

Also encountered this issue, my need was to filter out resource files of the same name from dependency modules, solution like below: 还遇到了这个问题,我需要从依赖模块中筛选出同名的资源文件,解决方法如下:

<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>
                        <filters>
                            <filter>
                                <artifact>artifact1</artifact>
                                <excludes>
                                    <exclude>application.yml</exclude>
                                    <exclude>logging.yml</exclude>
                                </excludes>
                            </filter>
                            <filter>
                                <artifact>artifact2</artifact>
                                <excludes>
                                    <exclude>application.yml</exclude>
                                    <exclude>logging.yml</exclude>
                                </excludes>
                            </filter>
                            <filter>
                                <artifact>*:*</artifact>
                                <excludes>
                                    <exclude>META-INF/*.SF</exclude>
                                    <exclude>META-INF/*.DSA</exclude>
                                    <exclude>META-INF/*.RSA</exclude>
                                </excludes>
                            </filter>
                        </filters>
                    </configuration>
                </execution>
            </executions>
</plugin>

note the lines below is to avoid possible exception: 请注意以下行是为了避免可能的异常:

Exception in thread "main" java.lang.SecurityException: Invalid signature file digest for Manifest main attributes 线程“main”中的异常java.lang.SecurityException:Manifest主要属性的签名文件摘要无效

<filter>
          <artifact>*:*</artifact>
          <excludes>
              <exclude>META-INF/*.SF</exclude>
              <exclude>META-INF/*.DSA</exclude>
              <exclude>META-INF/*.RSA</exclude>
          </excludes>
</filter>

In case you want more details, refer to Selecting Contents for Uber JAR 如果您需要更多详细信息,请参阅为Uber JAR选择内容

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

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