Say, I have this project that has 3 levels -- project, group and module. And I have pom.xml on each level: on project level, I have the dependencies across the whole project, and on group level I have the dependencies shared within the group, and module level has dependencies specific for the group. And I organize the pom file in such way: project pom (is the parent of) group pom (is the parent of) module pom.
project pom.xml
<groupId>com.example</groupId>
<artifactId>project</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>project</name>
<packaging>pom</packaging>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.framework1</groupId>
<artifactId>depend1</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.framework2</groupId>
<artifactId>depend2</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.framework3</groupId>
<artifactId>depend3</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
</dependencyManagement>
Say, I have group1 which would need to have dependencies depend1 and depend2 but not depend3.
group1 pom.xml
<parent>
<groupId>com.example</groupId>
<artifactId>project</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.example.project</groupId>
<artifactId>group1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>group1</name>
<packaging>pom</packaging>
<dependencies>
<dependency>
<groupId>org.framework1</groupId>
<artifactId>depend1</artifactId>
</dependency>
<dependency>
<groupId>org.framework2</groupId>
<artifactId>depend2</artifactId>
</dependency>
<dependencies>
And under group1, module1 would need both depend1 and depend2. So it would simply inherit it from group1 pom and no need to specify them.
module1 pom.xml
<parent>
<groupId>com.example.project</groupId>
<artifactId>group1</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.example.project.group1</groupId>
<artifactId>module1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>module1</name>
But for module2, it only needs depend1 but not depend2. How should I specify the exclusion? I have tried a couple of ways but none of them work. For example, I put the group1 dependencies inside a dependencyManagement tag but getting some maven error complaining about missing versions. And I have also made the module1 pom inherit from the project pom rather than the group1 pom, it seems work but that kinda breaks the intended structure. Any idea that I could implement this exclusion while preserve the project structure (in maven's perspective)?
module2 pom
<parent>
<groupId>com.example.project</groupId>
<artifactId>group1</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.example.project.group1</groupId>
<artifactId>module2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>module2</name>
//how to include depend1 but exclude depend2?
CodePudding user response:
Declare your dependencies as follows:
project
- pom.xml ... <dependencyManagement>
- group1
- pom.xml ... <dependency>depend1
- module1
| - pom.xml ... <dependency>depend2; <dependency>depend1 inherited from group1
- module2
- pom.xml ... no <dependency> declared; <dependency>depend1 inherited from group1