Java 如何使用maven创建基于spring的可执行jar?
我有一个基于Maven的SpringWS客户端项目,我想将其打包为一个jar。在eclipse中,一切都正常运行。当我尝试将其打包为可执行jar时,会出现ClassNotFound异常,因为我的应用程序jar中不包含Spring jar 因此,我添加了将所有依赖项都包含在我的应用程序jar中。当我查看我的appjar时,我看到了所有依赖项中的所有类文件(所有库jar都被分解) 代替Spring-beans.jar META-INF/Spring.schemas:Java 如何使用maven创建基于spring的可执行jar?,java,spring,maven,spring-ws,maven-shade-plugin,Java,Spring,Maven,Spring Ws,Maven Shade Plugin,我有一个基于Maven的SpringWS客户端项目,我想将其打包为一个jar。在eclipse中,一切都正常运行。当我尝试将其打包为可执行jar时,会出现ClassNotFound异常,因为我的应用程序jar中不包含Spring jar 因此,我添加了将所有依赖项都包含在我的应用程序jar中。当我查看我的appjar时,我看到了所有依赖项中的所有类文件(所有库jar都被分解) 代替Spring-beans.jar META-INF/Spring.schemas: http\://www.spri
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
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插件的信息时,我注意到了我之前错过的一些信息。但是,我关心的是如何知道其他哪些文件也有相同的问题?我发现/捕捉到了这个春季特刊。我不知道其他任何图书馆可能也在做类似的事情
如果您有任何建议,我们将不胜感激。您可以添加以下配置,以便将来自所有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>
META-INF/spring.handlers
META-INF/spring.schemas
代替maven shade插件使用。允许您将Java应用程序及其依赖项Jar打包到单个可执行Jar文件中。您是否尝试过该方法
它将为您创建一个带有依赖项的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>
maven汇编插件
2.3
带有依赖项的jar
org.sample.App
组装
包裹
单一的
昨天我也遇到了这个问题
解决方案是通过手动连接和通过以下方式配置组装插件来准备所需文件:
<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>
src/META-INF/spring.schemas
META-INF
src/META-INF/spring.handlers
META-INF
/
真的
真的
运行时
META-INF/spring.handlers
META-INF/spring.schemas
注意:使用一个jar方法是不够好的-您不能确定手头上的混合文件,请尝试保持所有依赖项的原样导出 汇编插件有问题,请使用下面的插件
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>
org.apache.maven.plugins
maven阴影插件
包裹
阴凉处
at.seresunit.讲师管理器_connector.App
META-INF/spring.handlers
META-INF/spring.schemas
您可能会遇到安全异常,请使用以下配置解决它
*:*
META-INF/*.SF
META-INF/*.DSA
META-INF/*.RSA
对于希望为Spring Boot应用程序找到答案的人,您所需要的只是:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
org.springframework.boot
springbootmaven插件
谢谢-我也看到了同样的情况,刚刚编辑了我的问题以包含这些信息。我的问题是如何知道是否还有其他库可能遇到类似的问题?我不能期望知道我使用的所有库的所有META-INF文件夹的内容…可能没有办法通过分析来确定。唯一可行的选择是执行一些测试,确保它工作并修复任何损坏的东西。@gkamal这意味着什么附加在一起
当打包在jar
中时,具有相同文件名的文件不是相互覆盖吗?在jar中,同名的不同文件怎么可能不冲突?@Jas-如果添加了上述配置,maven shade插件会将这些文件附加在一起。@gkamal谢谢!终于成功了+1 one-jar插件的问题是,它在目标文件夹中创建了第二个jar,扩展名为one-jar.jar。因此,当我尝试部署工件时,它会部署原始jar,而不是一个jar。您知道是否有适当的方法来配置pom以避免此问题吗?one-jar
是由一个jar插件添加的默认分类器。请参阅配置部署以使用分类器阴影和程序集插件是相同的概念。它们都将分解依赖项,并尝试将它们包含在同一区域中。当多个依赖项的文件位于同一路径(例如:META-INF/spring.schemas)时,两者都会遇到问题。另一种有效的方法是将spring JAR放在单独的lib文件夹中,并将这个lib
文件夹添加到清单中的类路径中——请参见AndréAronsen的回答
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>