2009-09-22 83 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>