2015-05-04 210 views
1

我有一個Maven多模塊項目與A作爲父項目。 B和C是模塊。如果我不想讓B和C繼承A,我怎麼能在B和C之間分享一些依賴關係? (所以我不能把那些依賴於一個從B和C繼承他們)Maven多模塊依賴關係共享

如果我有這樣的:

<dependency> 
     <groupId>groupCommon</groupId> 
     <artifactId>IdCommon</artifactId> 
     <version>1.0</version> 
     <scope>compile</scope> 
    </dependency> 

我想通過每一個模塊使用這種依賴關係,但我不知道把它放在每個pom.xml中。所以基本上,我怎麼能在模塊B和C之間共享這種依賴關係,而無需在項目A中放置這種依賴關係,並使B和C繼承自A?

+0

[導入依賴項](https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#Importing_Dependencies) – SpaceTrucker

回答

3

如果有多個共享依賴你有一個選擇是定義單獨的依賴性POM在其中定義的所有共享的依賴關係(在<dependencies>部,<dependencyManagement>部),然後定義該POM作爲一個依賴你的模塊。通過定義這個共享依賴關係pom作爲一個正常的依賴關係,它的所有依賴關係都被包含爲你的模塊的傳遞依賴關係。

您顯然仍然需要在每個模塊poms中定義對這個pom的依賴關係,但現在它是一個依賴項而不是多個。

因此,例如:

依賴性POM

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 
<modelVersion>4.0.0</modelVersion> 
<groupId>shared-dependencies-group</groupId> 
<artifactId>shared-dependencies</artifactId> 
<version>1.0</version> 
<name>Shared Dependencies</name> 
<dependencies> 
    <dependency> 
     <groupId>groupCommon</groupId> 
     <artifactId>IdCommon1</artifactId> 
     <version>1.0</version> 
     <scope>compile</scope> 
    </dependency> 
    <dependency> 
     <groupId>groupCommon</groupId> 
     <artifactId>IdCommon2</artifactId> 
     <version>1.0</version> 
     <scope>compile</scope> 
    </dependency> 
    <!-- more dependencies --> 
</dependencies> 

模塊B POM

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 
<modelVersion>4.0.0</modelVersion> 
<groupId>groupModules<groupId> 
<artifactId>module-b</artifactId> 
<name>Module B</name> 
<dependencies> 
    <!-- single dependency to the shared-dependencies pom instead of multiple dependencies --> 
    <dependency> 
     <groupId>shared-dependencies-group</groupId> 
     <artifactId>shared-dependencies</artifactId> 
     <version>1.0</version> 
     <scope>compile</scope> 
     <type>pom</type> 
    </dependency> 
</dependencies> 

模塊C的pom中顯然也會這樣做。