簡體   English   中英

如何使用 maven 創建基於 spring 的可執行文件 jar?

[英]How to create spring-based executable jar with maven?

我有一個基於 Maven 的 Spring-WS 客戶端項目,我希望將 package 作為單個 jar。在 eclipse 中,一切正常運行。 當我嘗試將 package 作為可執行文件 jar 時,我得到 ClassNotFound 異常,因為 Spring jars 不包含在我的應用程序 jar 中。

所以我添加了maven-shade-plugin以將我的所有依賴項包含在我的應用程序 jar 中。當我查看我的應用程序 jar 時,我看到所有包含的依賴項中的所有 class 文件(所有庫 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

我很難過。 我不確定我是否/如何將所有內容作為單個可執行文件 package jar。我不知道這是否是陰影插件配置問題,或者我是否正在嘗試做一些不可能的事情。 我必須手動創建自己的 spring.schemas 文件(其他文件的串聯)似乎不正確。

我可能有點倉促行事。 在挖掘有關 shade 插件的更多信息時,我注意到我之前錯過的AppendingTransformer 但是,我關心的是如何知道哪些其他文件有同樣的問題? 我發現/發現了這個特殊的 Spring 問題。 我不知道任何其他圖書館可能正在做類似的事情......

任何建議,將不勝感激。

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

而不是 maven-shade-plugin 使用onejar-maven-plugin One-JAR允許您將 Java 應用程序與其依賴項 Jar 一起打包到單個可執行 Jar 文件中。

昨天我也遇到了這個問題。

解決方案是通過手動連接和配置程序集插件來准備所需的文件:

  <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 方法還不夠好 - 您不能確定手頭的混合文件,請嘗試按原樣導出所有依賴項...

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>

你試過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>

對於希望為 Spring Boot 應用程序找到答案的人,您只需要:

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

升級到 Spring 6.0.0 后,我遇到了完全相同的問題。 我的項目有一個程序集 xml,我通過將以下處理程序添加到 <assembly> 部分來修復 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM