【问题标题】:files from different maven-modules with the same name can not co-exist in a jar-file created with the maven assembly-plugin来自不同 maven 模块的具有相同名称的文件不能共存于使用 maven 程序集插件创建的 jar 文件中
【发布时间】:2016-07-26 21:16:32
【问题描述】:

如果两个文件具有不同的内容但在两个不同的maven模块中具有相同的名称,则它们都放在一个带有ma​​ven 程序集插件,只有 一个文件 最终成为 .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>

【问题讨论】:

    标签: java maven jar maven-assembly-plugin multi-module


    【解决方案1】:

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

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

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

    线程“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>
    

    如果您想了解更多详情,请参阅Selecting Contents for Uber JAR

    【讨论】:

      【解决方案2】:

      maven-assembly-plugin 文档指定:

      如果您的项目想要将您的工件打包到 uber-jar 中,则程序集插件仅提供基本支持。如需更多控制,请使用 Maven Shade 插件


      使用maven-shade-plugin,您可以拥有一个fat jar(如使用程序集插件)并使用Resources transformers 解决合并文件的类似问题。在您的情况下,AppendingTransformer 将合并具有相同名称但具有不同内容的文件。

      某些 jar 包含具有相同文件名的其他资源(例如属性文件)。为避免覆盖,您可以选择通过将其内容附加到一个文件中来合并它们。

      一个简单的配置如下所示:

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

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

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

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

      【讨论】:

      • 我会失去使用 maven-shade-plugin 编写自定义程序集描述符的能力吗?
      • @replayleif 你的程序集描述符自定义了什么?您可以将其添加到您的问题或至少其相关部分吗?
      • @replayleif 检查我的编辑,我添加了所需的指针和一个示例来说明如何实现相同
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-05-18
      • 2010-12-12
      • 1970-01-01
      • 2011-02-15
      • 1970-01-01
      • 2014-01-27
      • 1970-01-01
      相关资源
      最近更新 更多