您需要将<exclusions> 部分添加到您的maven 依赖项中,并列出您不想继承的传递依赖项。
例如,如果我的 pom.xml 有一个如下所示的 <dependencies> 部分:
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
...然后当我执行mvn dependency:tree 时,我得到以下输出:
[INFO] ------------------------------------------------------------------------
[INFO] Building scratch 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ scratch ---
[INFO] com.nppc.mes:scratch:jar:0.0.1-SNAPSHOT
[INFO] \- junit:junit:jar:4.11:test
[INFO] \- org.hamcrest:hamcrest-core:jar:1.3:test
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
如果我决定我真的不想在我的项目中使用 hamcrest-core 依赖项,我可以修改我的 pom.xml 以排除它,如下所示:
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
...这给了我mvn dependency:tree 的输出:
[INFO] ------------------------------------------------------------------------
[INFO] Building scratch 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ scratch ---
[INFO] com.nppc.mes:scratch:jar:0.0.1-SNAPSHOT
[INFO] \- junit:junit:jar:4.11:test
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------