我有一个基于 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 问题。我不知道任何其他图书馆可能正在做类似的事情......
任何建议将不胜感激。
最佳答案
您可以添加以下配置,以便将所有 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>
关于java - 如何使用 maven 创建基于 spring 的可执行 jar?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11160534/