32

我有一个基于 Maven 的 Spring-WS 客户端项目,我想将其打包为单个 jar。在eclipse中,一切运行正常。当我尝试将其打包为可执行 jar 时,我得到 ClassNotFound 异常,因为 Spring jar 不包含在我的应用程序 jar 中。

所以我添加了maven-shade-plugin以在我的应用程序 jar 中包含我的所有依赖项。当我查看我的应用程序 jar 时,我看到了所有依赖项中的所有类文件(所有库 jar 都被分解了)。

<build>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <mainClass>com.cws.cs.Client</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>1.7</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                </execution>
            </executions>

        </plugin>
    </plugins>
</build>

我的问题是,在打包过程中,我的多个 spring 依赖项有不同的 META-INF/spring.schemas文件,它们相互覆盖。因此,我的最终 jar 有一个不完整的 spring.schemas 文件。

因此,当我尝试运行我的可执行 jar 时,我收到 Spring 错误消息,指出由于 spring.schemas 文件不完整(Spring-WS 的 jar 覆盖了 Spring-core 的 spring.schemas 文件),因此找不到文件。

我的可执行 jar 的 META-INF/spring.schemas:

http\://www.springframework.org/schema/web-services/web-services-1.5.xsd=/org/springframework/ws/config/web-services-1.5.xsd
http\://www.springframework.org/schema/web-services/web-services-2.0.xsd=/org/springframework/ws/config/web-services-2.0.xsd
http\://www.springframework.org/schema/web-services/web-services.xsd=/org/springframework/ws/config/web-services-2.0.xsd

而不是 Spring-beans.jar META-INF/spring.schemas:

http\://www.springframework.org/schema/beans/spring-beans-2.0.xsd=org/springframework/beans/factory/xml/spring-beans-2.0.xsd
http\://www.springframework.org/schema/beans/spring-beans-2.5.xsd=org/springframework/beans/factory/xml/spring-beans-2.5.xsd
http\://www.springframework.org/schema/beans/spring-beans-3.0.xsd=org/springframework/beans/factory/xml/spring-beans-3.0.xsd
http\://www.springframework.org/schema/beans/spring-beans-3.1.xsd=org/springframework/beans/factory/xml/spring-beans-3.1.xsd
http\://www.springframework.org/schema/beans/spring-beans.xsd=org/springframework/beans/factory/xml/spring-beans-3.1.xsd
http\://www.springframework.org/schema/tool/spring-tool-2.0.xsd=org/springframework/beans/factory/xml/spring-tool-2.0.xsd
http\://www.springframework.org/schema/tool/spring-tool-2.5.xsd=org/springframework/beans/factory/xml/spring-tool-2.5.xsd
http\://www.springframework.org/schema/tool/spring-tool-3.0.xsd=org/springframework/beans/factory/xml/spring-tool-3.0.xsd
http\://www.springframework.org/schema/tool/spring-tool-3.1.xsd=org/springframework/beans/factory/xml/spring-tool-3.1.xsd
http\://www.springframework.org/schema/tool/spring-tool.xsd=org/springframework/beans/factory/xml/spring-tool-3.1.xsd
http\://www.springframework.org/schema/util/spring-util-2.0.xsd=org/springframework/beans/factory/xml/spring-util-2.0.xsd
http\://www.springframework.org/schema/util/spring-util-2.5.xsd=org/springframework/beans/factory/xml/spring-util-2.5.xsd
http\://www.springframework.org/schema/util/spring-util-3.0.xsd=org/springframework/beans/factory/xml/spring-util-3.0.xsd
http\://www.springframework.org/schema/util/spring-util-3.1.xsd=org/springframework/beans/factory/xml/spring-util-3.1.xsd
http\://www.springframework.org/schema/util/spring-util.xsd=org/springframework/beans/factory/xml/spring-util-3.1.xsd

我难住了。我不确定是否/如何将所有内容打包为单个可执行 jar。我不知道这是否是一个阴影插件配置问题,或者我是否正在尝试做一些不可能的事情。我必须手动创建自己的 spring.schemas 文件(其他文件的串联)似乎不正确。

我可能有点过激了。在挖掘有关阴影插件的更多信息时,我注意到了我之前错过的AppendingTransformer 。但是,我关心的是如何知道哪些其他文件有同样的问题?我发现/发现了这个特殊的 Spring 问题。我不知道任何其他可能正在做类似事情的图书馆......

任何建议,将不胜感激。

4

6 回答 6

63

您可以添加以下配置,以便将所有 jar 中的 .schema 文件的内容附加在一起。

<configuration>
  <transformers>
    <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
      <resource>META-INF/spring.handlers</resource>
    </transformer>
    <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
      <resource>META-INF/spring.schemas</resource>
    </transformer>
  </transformers>
</configuration>
于 2012-06-22T16:45:21.280 回答
7

而不是 maven-shade-plugin 使用onejar-maven-pluginOne-JAR允许您将 Java 应用程序及其依赖项 Jar 打包到单个可执行 Jar 文件中。

于 2012-06-22T16:46:10.733 回答
5

昨天我也遇到了这个问题。

解决方案是通过手动连接和配置程序集插件来准备所需的文件:

  <files>
    <file>
        <source>src/META-INF/spring.schemas</source>
        <outputDirectory>META-INF</outputDirectory>
    </file>
    <file>
        <source>src/META-INF/spring.handlers</source>
        <outputDirectory>META-INF</outputDirectory>
    </file>
  </files>
  <dependencySets>
    <dependencySet>
      <outputDirectory>/</outputDirectory>
      <useProjectArtifact>true</useProjectArtifact>
      <unpack>true</unpack>
      <scope>runtime</scope>
      <unpackOptions>
        <excludes>
            <exclude>META-INF/spring.handlers</exclude>
            <exclude>META-INF/spring.schemas</exclude>
        </excludes>
      </unpackOptions>  
    </dependencySet>
  </dependencySets>

注意:使用一种 jar 方法还不够好 - 你不能确定手头的混合文件,尽量保持原样导出所有依赖项......

于 2012-08-02T03:51:14.360 回答
3
assembly plugin have issues, use below plugin

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <transformers>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                        <mainClass>at.seresunit.lecturemanager_connector.App</mainClass>
                    </transformer>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                        <resource>META-INF/spring.handlers</resource>
                    </transformer>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                        <resource>META-INF/spring.schemas</resource>
                    </transformer>
                </transformers>
            </configuration>
        </execution>
    </executions>
</plugin>

you may get security exception resolve it using below configuration

 <configuration>
    <filters>
        <filter>
            <artifact>*:*</artifact>
            <excludes>
                <exclude>META-INF/*.SF</exclude>
                <exclude>META-INF/*.DSA</exclude>
                <exclude>META-INF/*.RSA</exclude>
            </excludes>
        </filter>
    </filters>
</configuration>
于 2015-07-15T18:32:01.523 回答
2

您是否尝试过maven-assembly-plugin

它将为您创建一个带有依赖项的 jar,而且它可以使这个 jar 可执行:

使用 mainClass 指定要执行的类。

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.3</version>
    <configuration>
      <descriptorRefs>
        <descriptorRef>jar-with-dependencies</descriptorRef>
      </descriptorRefs>
      <archive>
        <manifest>
          <mainClass>org.sample.App</mainClass>
        </manifest>
      </archive>
    </configuration>
    <executions>
      <execution>
        <id>make-assembly</id> <!-- this is used for inheritance merges -->
        <phase>package</phase> <!-- bind to the packaging phase -->
        <goals>
          <goal>single</goal>
        </goals>
      </execution>
    </executions>
  </plugin>
于 2012-06-26T15:28:53.180 回答
1

对于希望为 Spring Boot 应用程序找到答案的人,您只需要:

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>
于 2020-06-25T01:43:18.223 回答