2009-06-09 45 views
8

我正在使用Maven 2程序集插件來構建一個jar-with-dependencies並創建一個可執行的JAR文件。我的程序集包括Spring和CXF庫。Maven 2程序集插件破壞了一些META-INF文件

CXF包含META-INF文件spring.schemas和spring.handlers的副本,這些副本最終會從spring-2.5.4 jar中敲除相似的文件。

手動,我可以更新jar-with-dependencies中的這兩個文件。

我正在尋找的是Maven POM中的一些方式來指導程序集插件來獲取這兩個文件的正確版本。

該彙編插件文檔討論了文件過濾,但似乎沒有配置或參數,沒有去創建自定義彙編描述符的麻煩。

是否在這種情況下製作自定義組裝描述符?

+0

同樣的事情發生與「spring.tooling」的文件,但是從他們的名字我猜他們不是在運行時使用。 – 2010-10-12 05:12:08

回答

6

由於某種原因,Mojo和其他人提出的解決方案對我來說仍然不起作用。我創建了我的自定義spring.handlersspring.schemas文件並將它們放在src/main/resources/META-INF之下。但是,使用unpackOptions時,我的文件也不包括在內。當我不使用unpackOptions時,我的文件不是jar中的文件。

我最終做的是直接引用文件。這最終把我的文件放入JAR中。

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd"> 
    <!-- TODO: a jarjar format would be better --> 
    <id>jar-with-dependencies</id> 
    <formats> 
     <format>jar</format> 
    </formats> 
    <includeBaseDirectory>false</includeBaseDirectory> 
    <dependencySets> 
     <dependencySet> 
      <outputDirectory>/</outputDirectory> 
      <unpack>true</unpack> 
      <unpackOptions> 
       <excludes> 
        <exclude>META-INF/spring.handlers</exclude> 
        <exclude>META-INF/spring.schemas</exclude> 
       </excludes> 
      </unpackOptions> 
      <scope>runtime</scope> 
     </dependencySet> 
    </dependencySets> 
    <files> 
     <file> 
      <source>${project.basedir}/src/main/resources/META-INF/spring.handlers</source> 
      <outputDirectory>META-INF</outputDirectory> 
     </file> 
     <file> 
      <source>${project.basedir}/src/main/resources/META-INF/spring.schemas</source> 
      <outputDirectory>META-INF</outputDirectory> 
     </file> 
    </files> 
</assembly> 
+0

確實,這個解決方案更好,更通用。由於某些原因,早先的排除情況因爲我而停止工作時,我不得不自行迴避。 – Mojo 2011-02-17 17:32:27

+0

我不得不添加 true來將類包含在同一個項目中。 – jontro 2012-06-25 15:36:46

1

我的工作了,和這裏的細節:

首先,有沒有辦法來指定文件包括或排除如果使用內置的裝配描述JAR-與依賴關係。

裝配插件文檔給出樣本jar-with-dependencies descriptor here

我將該描述符複製並粘貼到名爲exec-jar.xml的項目目錄中的文件中。然後在pom中,我改變了程序集插件以引用該描述符。下面是摘錄:

<build> 
    <plugins> 
    <plugin> 
     <groupId>org.apache.maven.plugins</groupId> 
     <artifactId>maven-assembly-plugin</artifactId> 
     <version>2.2-beta-3</version> 
     <configuration> 
      <descriptors> 
       <descriptor>exec-jar.xml</descriptor> 
      </descriptors> 
      <archive> 
       <manifest> 
        <mainClass>com.package.MyMainClass</mainClass> 
       </manifest> 
      </archive> 
     </configuration> 
     <executions> 
      <execution> 
       <id>make-assembly</id> 
       <phase>package</phase> 
       <goals> 
        <goal>single</goal> 
       </goals> 
      </execution> 
     </executions> 
    </plugin> 
    </plugins> 
</build> 

描述的該位結合的組件生命週期的包裝階段,並引用EXEC-jar.xml描述符。做這個包確認了jar是和預定義的描述符一起構建的。

然後,它成爲修改exec-jar.xml來排除與Spring文件衝突的CXF文件的問題。這是我的裝配描述符,它完成了:

<assembly> 
    <id>jar-with-dependencies</id> 
    <formats> 
    <format>jar</format> 
    </formats> 
    <includeBaseDirectory>false</includeBaseDirectory> 
    <dependencySets> 
    <dependencySet> 
     <unpack>true</unpack> 
     <unpackOptions> 
     <excludes> 
      <exclude>cxf*/META-INF/spring.handlers</exclude> 
      <exclude>cxf*/META-INF/spring.schemas</exclude> 
     </excludes> 
     </unpackOptions> 
     <scope>runtime</scope> 
    </dependencySet> 
    </dependencySets> 
    <fileSets> 
    <fileSet> 
     <directory>${project.build.outputDirectory}</directory> 
    </fileSet> 
    </fileSets> 
</assembly> 

現在,這裏是蹭。如果使用當前發佈的彙編插件2.1版進行此操作,它將在標籤上以「意外」方式失敗。標籤在插件的未發佈版本2.2中受支持。請注意,在我上面的pom文件摘錄中,我指定了maven-assembly-plugin版本2.2-beta-3,這是本文寫作時的最新版本。

這成功地構建了一個可執行的jar,並且Spring擁有了初始化我的應用程序所需的所有處理程序和模式。

1

你也可以通過從你想要的spring發行版中獲取spring.schemas和spring.handlers文件並將它們放到你的項目src/main/resources/META-INF目錄中來解決這個問題。由於這些是最後打包的,你會得到你想要的版本。我發現這個想法here

5

我試過陰影插件的方法,它的工作非常好。這裏是所有你需要把你的POM(不需要組裝插件)。

 <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-shade-plugin</artifactId> 
      <version>1.4</version> 
      <executions> 
       <execution> 
        <phase>package</phase> 
        <goals> 
         <goal>shade</goal> 
        </goals> 
        <configuration> 
         <transformers> 
          <transformer 
           implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> 
           <mainClass>org.example.Runner</mainClass> 
          </transformer> 
          <transformer 
           implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"> 
           <resource>META-INF/spring.handlers</resource> 
          </transformer> 
         </transformers> 
        </configuration> 
       </execution> 
      </executions> 
     </plugin>