2010-02-03 15 views
12

我想在Maven 2.2.1的子項目中繼承(父)pom.xml的依賴關係;即使用項目繼承。在這種情況下,似乎有必要將默認包裝類型從jar更改爲pom未使用項目聚合(multimodule)時是否需要包裝類型'pom'?

但是,the Maven2 documentation指出包裝類型pom是項目集合所必需的,即使用子模塊的多模塊項目,但不適用於項目繼承?

<project> 
<modelVersion>4.0.0</modelVersion> 
<groupId>example</groupId> 
<artifactId>example-parent</artifactId> 
<version>1</version> 

<dependencies> 
    <dependency> 
    <groupId>log4j</groupId> 
    <artifactId>log4j</artifactId> 
    <version>1.2.14</version> 
    </dependency> 
</dependencies> 
</project> 

<project>  
<parent> 
    <groupId>example</groupId> 
    <artifactId>example-parent</artifactId> 
    <version>1</version> 
</parent> 

<modelVersion>4.0.0</modelVersion> 
<groupId>example</groupId> 
<artifactId>example-child</artifactId> 
</project> 

但是如果你用上面的配置調用Maven的(如mvn clean),你會得到一個錯誤:

Project ID: example:example-child 

Reason: Parent: example:example-parent:jar:1 
of project: example:example-child has wrong packaging: jar. 
Must be 'pom'. for project example:example-child 

在其他另一方面,輸入以下內容:

<project> 
... 
<packaging>pom</packaging> 
... 
</project> 

在父親pom.xml中,Maven可以在沒有任何錯誤的情況下執行。

Maven的這種行爲是否正確?

回答

10

正如POM ReferenceInheritance節記載:

The packaging type required to be pom for parent and aggregation (multi-module) projects.

所以Maven的行爲,似乎是正確的,我(和錯誤消息是很好的自我解釋)。

0

正如帕斯卡所言,這種行爲是正確的。

如果您仍然在尋找共享模塊之間依賴關係的方法,可以考慮將問題中的依賴關係捆綁到一個pom中,然後讓您的模塊都依賴於新的「依賴」pom。請參閱Maven Book Section 3.6.1瞭解更多詳情。

2

如果你只是想繼承依賴關係,那麼我不認爲它需要類型'POM'。 您可以將它設置爲jar,並將其指定爲您作爲子項目的依賴項。然而,你不會有父/子關係,這是阻止你的父項目是'pom'以外的類型。

要清楚,您繼承了所有依賴項(傳遞依賴項)的依賴關係。

相關問題