【问题标题】:Maven building jar: merge duplicate resources from dependenciesMaven构建jar:合并依赖项中的重复资源
【发布时间】:2015-09-27 11:07:13
【问题描述】:

我遇到了这个问题:

我有一个带有 spring-boot 的项目。它在eclipse上完美运行,但我需要生成一个带有依赖项的jar,所以我将maven-assembly-plugin配置添加到pom中。

一些 spring 依赖项在 META-INF 中有一个名为 spring.schemas 的文件,我需要将所有 spring.schemas 合并为一个(spring-context、spring-beans 等)

我尝试使用 maven-shade-pluggin 和 AppendingTransformer this solution,它完美地合并了所有 spring.schemas...但是它有一个问题,当我执行 jar 时,它失败了:

java.lang.IllegalStateException: Unable to open nested entry 'lib/spring-boot-starter-batch-1.2.4.RELEASE.jar'. 
It has been compressed and nested jar files must be stored without compression.
Please check the mechanism used to create your executable jar file

所以,shade插件压缩jar,spring-boot不喜欢,也没有办法关闭shade压缩。我手动复制了 shade 生成的 shade spring.schemas 并将其放入 maven-assembly-pluggin 生成和未压缩的具有依赖关系的 jar 中。它有效。

然后我尝试将生成的 spring.schemas 包含到我的资源文件夹中,但它总是被 spring-context 中的 spring.schemas 覆盖。

我还尝试this other solution 使用程序集中的描述符 XML 从依赖 jar 中排除 spring.schemas,但它不起作用:

<assembly
    xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
    <id>distribution</id>
    <formats>
        <format>jar</format>
    </formats>
    <dependencySets>
        <dependencySet>
            <outputDirectory>/</outputDirectory>
            <includes>
                <include>org.springframework:spring-context</include>
            </includes>
            <unpack>true</unpack>
            <unpackOptions>
                <excludes>
                    <exclude>**/spring.schemas</exclude>
                </excludes>
            </unpackOptions>
        </dependencySet>
        <dependencySet>
            <outputDirectory>/</outputDirectory>
            <excludes>
                <exclude>org.springframework:spring-context</exclude>
            </excludes>
            <unpack>true</unpack>
        </dependencySet> 
    </dependencySets>
</assembly>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <archive>
                        <manifest>
                            <mainClass>org.springframework.batch.core.launch.support.CommandLineJobRunner</mainClass>
                        </manifest>
                        <compress>false</compress>
                    </archive>
                    <descriptors>
                        <descriptor>src/main/assembly/distribution.xml</descriptor>
                    </descriptors>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

这是我的依赖项:

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-batch</artifactId>
        </dependency>
        <dependency>
            <groupId>org.hsqldb</groupId>
            <artifactId>hsqldb</artifactId>
        </dependency>
        <!-- MySql 5.5 Connector -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.35</version>
        </dependency>
    </dependencies>

有什么想法吗?提前致谢

【问题讨论】:

    标签: maven spring-boot maven-assembly-plugin maven-bundle-plugin


    【解决方案1】:

    您可以使用maven-assembly-plugin

    将插件添加到您的pom.xml:

    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-assembly-plugin</artifactId>
      <version>3.1.1</version>
      <executions>
        <execution>
          <id>make-assembly</id>
          <phase>package</phase>
          <goals>
            <goal>single</goal>
          </goals>
        </execution>
      </executions>
      <configuration>
        <descriptors>
          <descriptor>fat-jar.xml</descriptor>
        </descriptors>
      </configuration>
    </plugin>
    

    pom.xml 旁边添加一个fat-jar.xml 描述符:

    <assembly xmlns="http://maven.apache.org/ASSEMBLY/2.1.0"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.1.0 http://maven.apache.org/xsd/assembly-2.1.0.xsd">
      <id>fat</id>
      <formats>
        <format>jar</format>
      </formats>
      <includeBaseDirectory>false</includeBaseDirectory>
      <dependencySets>
        <dependencySet>
          <outputDirectory>/</outputDirectory>
          <useProjectArtifact>true</useProjectArtifact>
          <unpack>true</unpack>
          <scope>runtime</scope>
        </dependencySet>
      </dependencySets>
      <containerDescriptorHandlers>
        <containerDescriptorHandler>
          <handlerName>metaInf-spring</handlerName>
        </containerDescriptorHandler>
      </containerDescriptorHandlers>
    </assembly>
    

    注意metaInf-spring 处理程序。它将合并任何名称以spring 开头的 META-INF 重复文件。

    参考文档:https://maven.apache.org/plugins/maven-assembly-plugin/examples/single/using-container-descriptor-handlers.html

    【讨论】:

      【解决方案2】:

      我不会使用 maven-assembly-plugin 和 maven-shade-pluggin,而是尝试使用 Spring Boot 自己的 spring-boot-maven-plugin 可用于创建可执行的“胖”JAR。请参阅this reference documentation page,“73.2 使用 Maven 创建可执行 JAR”部分。

      <build>
      <plugins>
          <plugin>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-maven-plugin</artifactId>
          </plugin>
      </plugins>
      

      或者,如果你不使用 spring-boot-starter-parent POM:

      <build>
      <plugins>
          <plugin>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-maven-plugin</artifactId>
              <version>1.2.5.RELEASE</version>
              <executions>
                  <execution>
                      <goals>
                          <goal>repackage</goal>
                      </goals>
                  </execution>
              </executions>
          </plugin>
      </plugins>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-01-20
        • 2012-01-15
        • 1970-01-01
        • 1970-01-01
        • 2020-05-08
        • 1970-01-01
        • 2015-02-19
        • 1970-01-01
        相关资源
        最近更新 更多