我有一个基于Maven的Spring-WS客户端项目,我希望将其打包为一个jar。在日食中,一切正常运行。当我尝试将其打包为可执行jar时,由于Spring jar不包含在我的应用程序jar中,因此出现ClassNotFound异常。
因此,我添加了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.schemas文件不完整(Spring-WS的jar已覆盖Spring-core的spring.schemas文件),我收到了无法找到文件的Spring错误消息。
我的可执行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文件(其他文件的串联)似乎并不正确。
我可能稍微跳了一下枪。在挖掘有关shade插件的更多信息时,我注意到我之前错过的AppendingTransformer。但是,我关心的是如何知道哪些其他文件存在相同的问题?我发现/抓住了这个Spring特刊。我不知道任何其他图书馆可能正在做类似的事情…
任何建议,将不胜感激。
代替使用maven-shade-plugin,请使用onejar-maven-plugin。通过One-JAR,您可以将Java应用程序及其依赖项Jar打包到一个可执行的Jar文件中。
你可以添加以下配置,以便将所有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>