为什么在 Maven 构建(安装)中默认不执行集成测试?

Why are integration tests not executed by default in a maven build (install)?

据我了解,默认情况下(这意味着没有任何故障安全插件的显式配置),maven 应该 运行 在执行目标 "install" 时进行集成测试。参见 https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html#Lifecycle_Reference

反正好像不是这样的。所以,我可能误会了什么。

我用 src/test/java 中的 JUnit 测试 class org.example.ExampleIT 和以下 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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>mvn-project</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.6.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
    </plugins>
</build>

构建(mvn install)成功,但未执行故障保护。请参阅以下日志摘录:

[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ mvn-project ---
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ mvn-project ---
(...)
[INFO] --- maven-install-plugin:2.4:install (default-install) @ mvn-project
(...)
[INFO] BUILD SUCCESS

你应该实际执行插件

<project>
  [...]
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-failsafe-plugin</artifactId>
        <version>2.19.1</version>
        <executions>
          <execution>
            <goals>
              <goal>integration-test</goal>
              <goal>verify</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  [...]
</project>

您可以在以下方面找到更多信息:

http://maven.apache.org/surefire/maven-failsafe-plugin/usage.html

参见https://maven.apache.org/ref/3.2.2/maven-core/default-bindings.html

插件通过 "Plugin Binding" 绑定到生命周期阶段。这些绑定特定于包装。

默认绑定在 Maven 核心的 META-INF/plexus/default-bindings.xml 中定义。 jar 打包的默认绑定不为 integration-test 阶段(参见 https://maven.apache.org/ref/3.2.2/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging)提供任何插件绑定,因此必须将故障安全插件绑定到 integration-test 和验证阶段手动配置(参见 JF Meier 的回答)。

插件不仅要绑定到 integration-test 阶段,还要绑定到验证阶段。否则会执行测试,但如果某些集成测试失败,构建不会失败。