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

yk9xbfzb  于 2022-11-28  发布在  Spring
关注(0)|答案(7)|浏览(145)

我有一个基于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。我不知道这是一个shade-plugin配置问题,还是我正在尝试做一些不可能的事情。如果我必须手动创建自己的spring.schemas文件(其他文件的串联),这似乎是不正确的。
我可能有点操之过急了。在挖掘更多关于shade插件的信息时,我注意到了我之前错过的AppendingTransformer。然而,我关心的是如何知道其他文件有同样的问题?我发现/发现了这个特殊的Spring问题。我不知道其他库可能会做类似的事情...
如有任何建议,我们将不胜感激。

kpbwa7wx

kpbwa7wx1#

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

yb3bgrhw2#

使用x1e0 f1 a代替maven-shade-plugin。One-JAR允许您将Java应用程序及其依赖Jar打包到单个可执行Jar文件中。

edqdpe6u

edqdpe6u3#

昨天我也遇到了这个问题。
解决方案是通过手动连接和配置汇编插件来准备所需的文件:

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

注意:使用一个jar方法是不够的-你不能确定手头的混合文件,尽量保持所有依赖的导出...

vzgqcmou

vzgqcmou4#

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

eoigrqb65#

你试过maven-assembly-plugin吗?
它将为您创建一个带有依赖项的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>
6ojccjat

6ojccjat6#

对于那些希望为Sping Boot 应用程序找到答案的人来说,您所需要的只是:

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>
2skhul33

2skhul337#

在升级到Spring 6.0.0之后,我遇到了完全相同的问题。我的项目有一个程序集xml,我通过向该部分添加以下处理程序修复了spring.schemas和spring.handlers问题:

<containerDescriptorHandlers>
    <containerDescriptorHandler>
        <handlerName>metaInf-spring</handlerName>
    </containerDescriptorHandler>
</containerDescriptorHandlers>

这记录在https://maven.apache.org/plugins/maven-assembly-plugin/examples/single/using-container-descriptor-handlers.html

相关问题