依赖 "test" 范围是否适用于 Maven 中的 "integration-test" 阶段?

Does the dependency "test" scope apply to the "integration-test" phase in Maven?

当我使用 Maven 构建项目时,范围 test 的依赖项似乎在 integration-test 阶段超出范围。这是设计使然,还是我可以做些什么来在 integration-test 阶段包含范围为 test 的依赖项?

One answer here on SO 建议测试依赖项在集成测试阶段的范围内,但答案只是一个没有引用的语句。但是,这似乎不是 Maven 为我工作的方式。

当我将给定依赖项的范围从 test 更改为 compile 时,给定依赖项(如预期)在 integration-test 阶段可用。

test 范围应该应用于 integration-test 阶段,还是我必须将依赖范围设置为 compile 以便它们在 integration-test 期间可用]相?

这是 POM 文件的相关部分。我正在做的是尝试在集成测试阶段启动 MockServer 的实例。但是,它失败了,因为 com.company.msd 依赖项未包含在内。

<dependencies>                      
    <dependency>
        <groupId>com.company.msd</groupId>
        <artifactId>MockServerDemo</artifactId>
        <version>0.0.4</version>
        <scope>test</scope>
    </dependency>         
</dependencies>

[...]

<plugin>
  <groupId>org.mock-server</groupId>
  <artifactId>mockserver-maven-plugin</artifactId>
  <version>3.9.17</version>
  <configuration>
    <serverPort>1080</serverPort>
    <proxyPort>1081</proxyPort>
    <logLevel>DEBUG</logLevel
    <initializationClass>com.company.msd.server.DefaultExpectationInitializer</initializationClass>
  </configuration>
  <executions>
    <execution>
      <id>pre-integration-test</id>
        <phase>pre-integration-test</phase>
        <goals>
          <goal>start</goal>
        </goals>
        </execution>
      <execution>
      <id>post-integration-test</id>
      <phase>post-integration-test</phase>
      <goals>
        <goal>stop</goal>
      </goals>
    </execution>
  </executions>
</plugin>

注意!如果我将依赖项添加为插件依赖项,它就可以工作。但是,我仍然很好奇 Maven 是否在设计上没有在 integration-test 阶段包含测试范围的依赖项。

根据 maven-failsafe-pluginintegration-test mojo 文档 (link) and source code (link),它确实需要在 test 范围内解决依赖关系。换句话说,决定使用哪个依赖范围的不是阶段,而是实际使用的 mojo。

在你的情况下 mockserver-maven-pluginstart mojo 确实只需要解决 compile+runtime 范围内的依赖关系 (link), whereas stop mojo requires the default runtime resolution scope (link). See requiresDependencyResolution descriptor in Maven Mojo API documentation (link) 以获取更多详细信息。

如果你以后遇到类似的问题,我建议你打印出 effective pom 来了解 Maven 是如何解释你的配置的,否则 运行 Maven with -X flag 以便您可以看到每个 mojo 的实际 class 路径。 Maven 文档有时可能会模棱两可,但这样您至少可以确定 Maven 在实践中是如何工作的。