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

时间:2017-02-15 07:28:09

标签: java maven-3 maven-failsafe-plugin

根据我的理解,默认情况下(这意味着没有任何明确的故障安全插件配置),maven应该在目标"安装"被执行。请参阅https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html#Lifecycle_Reference

无论如何,似乎并非如此。所以,我可能误解了一些东西。

我在src / test / java和以下POM中使用JUnit测试类org.example.ExampleIT编写了一个最小测试项目:

<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安装)是成功的,但不执行failsafe。请参阅以下日志摘录:

[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

2 个答案:

答案 0 :(得分:1)

你应该通过

实际执行插件
<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

答案 1 :(得分:1)

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

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

默认绑定在maven核心的META-INF / plexus / default-bindings.xml中定义。 jar包装的默认绑定不为集成测试阶段提供任何插件绑定(请参阅https://maven.apache.org/ref/3.2.2/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging),因此必须手动配置failafe插件与集成测试和验证阶段的绑定(请参阅JF Meier回答。

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