2012-06-22 68 views
28

我有一个基于Maven的Spring-WS客户端项目,我想打包为一个jar。在eclipse中,一切运行正常。当我尝试将它打包为一个可执行的jar时,我得到了ClassNotFound异常,因为Spring jar没有包含在我的应用程序jar中。如何用maven创建基于spring的可执行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文件。因为spring.schemas文件是不完整的(Spring-WS的jar已经覆盖了Spring-core的spring.schemas文件),所以当我尝试运行我的可执行jar时,出现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 

代替弹簧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 

我难倒。我不确定是否/如何将所有内容打包为单个可执行文件。我不知道这是否是阴影插件配置问题,或者我是否尝试做不可能的事情。我不得不手动创建自己的spring.schemas文件(其他连接)。

我可能已经跳了一下枪。在挖阴影插件的更多信息,我注意到我以前错过的AppendingTransformer。但是,我担心如何知道哪些其他文件存在相同的问题?我发现/抓住了这个特殊的春季问题。我不知道任何其他库可能会做类似的事情...

任何意见,将不胜感激。

+0

伟大的工程替代的方法是春季罐子放入单独的lib文件夹,并添加此'lib'夹入清单类路径 - 见安德烈Aronsen的回答http://stackoverflow.com/a/4323501/241986 –

回答

6

代替maven-shade-plugin使用onejar-maven-pluginOne-JAR可让您将Java应用程序及其依赖项Jars打包到单个可执行Jar文件中。

+3

我使用one-jar插件的问题是,它使用one-jar.jar扩展名在目标文件夹中创建第二个jar。因此,当我尝试部署工件时,它会部署原始的jar而不是单个jar。你知道是否有合适的方法来配置pom以避免此问题? –

+0

'one-jar'是一个jar插件添加的默认分类器。请参阅http://onejar-maven-plugin.googlecode.com/svn/mavensite/usage.html配置部署以使用该分类器http://stackoverflow.com/questions/9743574/maven-how-to-install-a- jar-with-a-class-and-not-install-the-default-one – amra

57

您可以添加以下配置,以便所有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> 
+1

谢谢 - 我看到了同样的情况,并编辑了我的问题以包含该信息。我的问题是如何知道是否有其他库可能遇到类似的问题?我不能期望知道我使用的所有库的所有META-INF文件夹的内容...... –

+0

可能没有办法通过分析来搞清楚。唯一可行的选择是执行一些测试,确保它能正常工作并修复任何被破坏的东西。 – gkamal

+0

@gkamal它是什么意思'获得一起附加在一起'是不是具有相同的文件名包装在一个'jar'中的相互覆盖的文件?具有相同名称的不同文件如何在jar中不发生冲突? – Jas

1

您是否试过maven-assembly-plugin

它会创建一个依赖你一个jar和morevover它可以使这个罐子是可执行的:

使用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> 
+3

阴影和装配插件是相同的概念。他们都会爆发依赖关系,并尝试将它们包含在同一个区域中。当多个依赖包含相同路径中的文件时(例如:META-INF/spring.schemas),都会遇到问题。 –

4

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

的解决方案是通过这种准备通过手工拼接和组装插件的配置所需的文件:

<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> 

注:使用一个罐子的方法是不够的 - 你不能在现场混合文件中的某些,尽量保持所有依赖的一样,是出口......

1
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> 
相关问题