2010-12-22 125 views
6

使用Maven程序集插件(版本2.2-beta-5)时,似乎组装的jar将包含来自依赖项的资源,而不是正在汇编的项目(如果它们具有相同的路径)。具体来说,我想弄清楚如何使用项目的log4j配置文件,而不是依赖项中的一个。Maven Assembly插件和资源

PROJECT1

-src

- 主

---资源

----的log4j.xml

如果PROJECT1具有相关性 - 称它为Project2 - 在src/main/resources中也有一个log4j.xml文件,然后在运行程序集插件后,汇编的jar包含Project2的log4j.xml文件而不是Project1的文件。我相信这是因为所有的依赖关系都是首先解压缩的,所以当它试图解压顶级项目时,log4j.xml文件已经存在,所以它不会被覆盖。

是否有使汇编插件使用项目的文件,而不是依赖的?

+0

相关(类似的事实)问题与附加的细节在它的答案:[自定义maven组装](http://stackoverflow.com/q/3493381/320399) – blong 2013-08-08 16:37:17

回答

1

一种可能性是从程序集创建中明确排除相关文件。

<dependencySet> 
     <outputDirectory>/</outputDirectory> 
     <includes> 
      <include>${project.groupId}:Project2</include> 
     </includes> 
     <unpack>true</unpack> 
     <unpackOptions> 
      <excludes> 
       <exclude>**/log4j.xml</exclude> 
      </excludes> 
     </unpackOptions> 
    </dependencySet> 
+1

这必须在组装权?没有办法只有一个maven pom文件的正确的东西在里面。 < – ggb667 2014-04-10 16:42:52

3

像这样的东西应该工作:

<assembly> 
    <id>without-log4j-config</id> 
    <formats> 
     <format>jar</format> 
    </formats> 
    <includeBaseDirectory>false</includeBaseDirectory> 
    <dependencySets> 
     <dependencySet> 
      <unpack>true</unpack> 
      <scope>runtime</scope> 
      <unpackOptions> 
       <excludes> 
        <exclude>**/log4j.xml</exclude> 
        <exclude>**/log4j.properties</exclude> 
       </excludes> 
      </unpackOptions> 
     </dependencySet> 
    </dependencySets> 
    <files> 
     <file> 
      <source>pom.xml</source> 
      <outputDirectory>/</outputDirectory> 
     </file> 
    </files> 
    <fileSets> 
     <fileSet> 
      <useDefaultExcludes>false</useDefaultExcludes> 
      <outputDirectory>/</outputDirectory> 
     </fileSet> 
     <fileSet> 
      <directory>src/main/java</directory> 
      <outputDirectory>/</outputDirectory> 
     </fileSet> 
     <fileSet> 
      <directory>src/main/resources</directory> 
      <outputDirectory>/</outputDirectory> 
     </fileSet> 
    </fileSets> 
    <moduleSets> 
     <moduleSet> 
      <includeSubModules>false</includeSubModules> 
      <sources> 
       <outputDirectoryMapping>/</outputDirectoryMapping> 
       <excludeSubModuleDirectories>false</excludeSubModuleDirectories> 
       <fileSets> 
        <fileSet> 
         <directory>src/main/java</directory> 
         <outputDirectory>/</outputDirectory> 
        </fileSet> 
        <fileSet> 
         <directory>src/main/resources</directory> 
         <outputDirectory>/</outputDirectory> 
        </fileSet> 
       </fileSets> 
      </sources> 
      <binaries> 
       <dependencySets> 
        <dependencySet> 
         <unpack>true</unpack> 
        </dependencySet> 
       </dependencySets> 
      </binaries> 
     </moduleSet> 
    </moduleSets> 
</assembly> 

你还需要这在你的POM:

 <plugin> 
      <artifactId>maven-assembly-plugin</artifactId> 
      <configuration> 
       <appendAssemblyId>true</appendAssemblyId> 
       <descriptors> 
        <descriptor>src/main/resources/whatever-you-name-the-snippet-above.xml</descriptor> 
       </descriptors> 
      </configuration> 
     </plugin> 

这是一个重要的组成部分:

<appendAssemblyId>true</appendAssemblyId> 

没有它,如果你运行您的自定义程序集将被覆盖。