Maven pom 文件继承范围

jcm*_*jcm 5 java maven

我在父 pom 中有以下定义:

<dependency>
  <groupId>net.sourceforge.htmlunit</groupId> 
  <artifactId>htmlunit</artifactId> 
  <version>2.11</version> 
  <scope>test</scope>      
</dependency>
Run Code Online (Sandbox Code Playgroud)

然后在我的孩子 pom 中:

<dependency>
    <groupId>net.sourceforge.htmlunit</groupId>
    <artifactId>htmlunit</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)

我发现当我的 WAR 文件(我的子 pom 中的打包类型)构建时,来自 htmlunit 的传递依赖项包含在 WEB-INF/lib 目录中(特别是 commons-codec)。

我本以为,由于依赖项具有“测试”范围,因此不应打包它及其任何传递依赖项。我的假设不正确吗?

Ash*_*wal 4

你的假设是不正确的。由于您已经提供了范围test,因此该 jar 将在测试和执行阶段出现。

范围应该是provided相反的。该范围provided将使 jar 在编译和测试类路径上可用,并且不可传递。

有关更多详细信息,请参阅http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html 依赖范围部分。