如何用maven创建基于spring的可执行jar?

Eri*_* B. 28 java spring spring-ws maven maven-shade-plugin

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

所以我添加了maven-shade-plugin来包含我的应用程序jar中的所有依赖项.当我查看我的app 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>
Run Code Online (Sandbox Code Playgroud)

我的问题是在打包过程中,我的多个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
Run Code Online (Sandbox Code Playgroud)

而不是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
Run Code Online (Sandbox Code Playgroud)

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

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

任何建议,将不胜感激.

gka*_*mal 60

您可以添加以下配置,以便将来自所有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>
Run Code Online (Sandbox Code Playgroud)


amr*_*mra 6

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

  • 我的one-jar插件的问题是它在目标文件夹中创建了第二个jar,扩展名为one-jar.jar.因此,当我尝试部署工件时,它会部署原始jar而不是one-jar.你知道是否有一种正确的方法来配置pom以避免这个问题? (3认同)

Mik*_*lin 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>
Run Code Online (Sandbox Code Playgroud)

注意:使用一个 jar 方法还不够好 - 您不能确定手头的混合文件,请尝试按原样导出所有依赖项...