2015-05-04 219 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中显然也会这样做。