2009-06-09 54 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>