来自不同 maven 模块的同名文件不能共存于使用 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

如果在两个不同的maven-modules中有两个内容不同但同名的文件,它们都放在一起在一个带有 maven assembly-plugin 的 jar 文件中,只有 一个文件 最终成为 .jar 文件的一部分。

问题:在构建jar文件的时候,有没有办法保证将文件的内容汇集到一个文件中?

我显然不想手动将信息放在一起,因为这是我试图通过将项目拆分为不同模块来避免的。

编辑:我有一个我想保留的自定义程序集描述符,即使我开始使用另一个插件也是如此。此描述符基本上排除了所有语言,但资源和错误文本的英语除外。

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

maven-assembly-plugin 文档所述:

If your project wants to package your artifact in an uber-jar, the assembly plugin provides only basic support. For more control, use the Maven Shade Plugin.


使用 maven-shade-plugin you can have a fat jar (like using the assembly plugin) and solve similar issues of merging file using Resources transformers. In your case, the AppendingTransformer 将合并名称相同但内容不同的文件。

Some jars contain additional resources (such as properties files) that have the same file name. To avoid overwriting, you can opt to merge them by appending their content into one file.

一个简单的配置如下:

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

更新
您不需要 shade 插件的外部程序集描述符,您可以直接将您的要求配置为插件配置。
在您的情况下,要从组装的罐子中排除资源,您可以使用 shade filters.

一个简单的配置(与上面的配置合并)如下所示:

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

也遇到了这个问题,我的需求是从依赖模块中过滤掉同名资源文件,解决方法如下:

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

注意 下面几行是为了避免可能的异常:

Exception in thread "main" java.lang.SecurityException: Invalid signature file digest for Manifest main attributes

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

如果您需要更多详细信息,请参阅为 Uber JAR 选择内容