2009-09-22 111 views
20

我想用Maven程序集插件来构建一个jar -with-dependencies,,除了已经提供了作用域。从Maven程序集中排除“提供”的依赖关系

我已经将jar-with-dependencies复制到了一个assembly.xml文件中,并在我的pom中配置了它的使用。这是供参考:

<?xml version="1.0" encoding="UTF-8"?> 
<assembly> 
    <id>injectable-jar</id> 
    <formats> 
    <format>jar</format> 
    </formats> 
    <includeBaseDirectory>false</includeBaseDirectory> 
    <dependencySets> 
    <dependencySet> 
     <unpack>true</unpack> 
     <scope>runtime</scope> 
    </dependencySet> 
    </dependencySets> 
    <fileSets> 
    <fileSet> 
     <directory>${project.build.outputDirectory}</directory> 
    </fileSet> 
    </fileSets> 
</assembly> 

我发现,如果我将范围设置为provided,那么我可以构建一个包含正是我希望有一个罐子,但我想不出如何获得反向行为。

+0

包含什么用,你显示为例大会的JAR?它是否仅包含运行时依赖关系? – romaintaz 2009-09-22 09:27:29

+0

它似乎包含除'test'作用域依赖关系之外的所有内容。 – 2009-09-22 09:28:49

+0

不,它也具有'测试'范围的依赖关系。我想知道,这可能是一种理智的默认方式? – 2009-09-22 09:33:33

回答

17

这有点笨重,但您可以使用maven-dependency-plugin将所有依赖项复制/解压到您的项目中,然后使用assembly插件进行打包。

copy-dependenciesunpack-dependencies目标都有一个可选的excludeScope属性,您可以设置为忽略provided依赖关系。下面的配置将所有依赖关系复制到target/lib中,您的程序集插件描述符可以修改为使用fileSet来包含这些jar。

更新:刚刚测试过,以确认它的工作原理。添加了将程序集插件绑定到程序包阶段的配置以及对程序集描述符的相关修改。

<plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-dependency-plugin</artifactId> 
    <executions> 
    <execution> 
     <id>copy-dependencies</id> 
     <phase>process-resources</phase> 
     <goals> 
     <goal>copy-dependencies</goal> 
     </goals> 
     <configuration> 
     <excludeScope>provided</excludeScope> 
     <outputDirectory>${project.build.directory}/lib</outputDirectory> 
     </configuration> 
    </execution> 
    </executions> 
</plugin> 
<plugin> 
    <artifactId>maven-assembly-plugin</artifactId> 
    <version>2.2-beta-4</version> 
    <executions> 
    <execution> 
     <id>jar-with-deps</id> 
     <phase>package</phase> 
     <goals> 
     <goal>single</goal> 
     </goals> 
    </execution> 
    </executions> 
    <configuration> 
    <descriptors> 
     <descriptor>src/main/assembly/my-assembly.xml</descriptor> 
    </descriptors> 
    </configuration> 
</plugin> 

my-assembly描述符的文件集部分是这样的:

<assembly> 
    <fileSets> 
    <fileSet> 
     <directory>${project.build.directory}/lib</directory> 
     <outputDirectory>/</outputDirectory> 
     <includes> 
     <include>*.*</include> 
     </includes> 
    </fileSet> 
    </fileSets> 
... 

</assembly> 
+1

我无法弄清楚如何让它排除我所拥有的'test'依赖项,但除此之外它完美地工作。 :) – 2009-09-22 12:53:11

+0

这更加笨拙,但如果您需要排除测试范围,您可以定义多次执行依赖项插件,并且在每次执行时指定一个不同的作用域(即省略测试并提供,定义两个执行,每个用于 compile runtime) – 2009-09-22 13:03:35

+14

事实证明,一次执行就足够了。 ' runtime'是我所需要的 - 它隐含地排除了'test','provided'和'system',这是完美的。 – 2009-09-24 15:53:51

0

随着最新的Maven(我是在Maven的3.0测试),这似乎按预期方式工作,与一些注意事项:

请求的范围(在dependencySet中)可能包含基于以下定义的其他范围:http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#Dependency_Scope

因此,如果你要求编译范围,你将得到编译和提供。但是,如果您请求运行时范围,则应该获得编译和运行时(但未提供)。

+0

如果此功能恰好同时属于运行时传递依赖关系,该功能是否能正确排除提供的依赖关系? – Vadzim 2013-10-09 15:09:19

4

从理论上讲,标签“ignoreNonCompile”和“excludeScope”应该有所帮助,但要警告他们不一定能正常工作。

随着maven3和Maven的依赖,插件2.4,一种解决方案是:

<configuration> 
<excludeArtifactIds>junit,mockito-all</excludeArtifactIds> 
<excludeTransitive>true</excludeTransitive> 
</configuration> 
0

这是一个老的文章,但Maven的依赖,插件现在有,你可以设置一个“excludeScope”选项到“提供”或您需要的任何范围。

http://maven.apache.org/plugins/maven-dependency-plugin/copy-dependencies-mojo.html#excludeScope

例如,

<plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-dependency-plugin</artifactId> 
    <version>2.10</version> 
    <executions> 
     <execution> 
      <id>copy-dependencies</id> 
      <phase>prepare-package</phase> 
      <goals> 
       <goal>copy-dependencies</goal> 
      </goals> 
      <configuration> 
       <outputDirectory>${project.build.directory}/lib</outputDirectory> 
       <overWriteReleases>false</overWriteReleases> 
       <overWriteSnapshots>false</overWriteSnapshots> 
       <overWriteIfNewer>true</overWriteIfNewer> 
       <excludeScope>provided</excludeScope> 
      </configuration> 
     </execution> 
    </executions> 
</plugin>