为什么“failafe:integration-test”不会自动执行mvn verify
?
根据mvn help:describe ...
,maven-failsafe-plugin
任务“集成测试”应该被绑定到“集成测试”阶段:
failsafe:integration-test
Description: Run integration tests using Surefire.
Implementation: org.apache.maven.plugin.failsafe.IntegrationTestMojo
Language: java
Bound to phase: integration-test
然而mvn verify
只执行“surefire:test”但不执行“failsafe:integration-test”。
我知道如何使用<executions>
修复它,因为这甚至在故障安全插件主页上的“用法”示例中,但我想了解为什么这两个插件的行为不同。
有两个<include>
模式匹配的文件:
src/test/java/de/lathspell/test/UselessThingTest.java
src/test/java/de/lathspell/test/UselessThingIT.java
我的pom.xml看起来像这样:
<?xml version="1.0" encoding="UTF-8"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:pom="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<groupId>de.lathspell</groupId>
<artifactId>test1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.22.1</version>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
输出:
[INFO] -------------------------< de.lathspell:test1 >-------------------------
[INFO] Building test1 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ test1 ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /mnt/keller_christian/workspace/test1/src/main/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ test1 ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ test1 ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /mnt/keller_christian/workspace/test1/src/test/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ test1 ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.22.1:test (default-test) @ test1 ---
[INFO]
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO] Running de.lathspell.test.UselessThingTest
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.048 s - in de.lathspell.test.UselessThingTest
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ test1 ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
简单的事情是,maven-failsafe-plugin默认不受任何生命周期的约束。
这意味着您必须在项目中绑定一次。
文件说:
默认情况下绑定到生命周期阶段:集成测试。
这意味着您必须添加到生命周期中,然后插件将绑定到integration-test
生命周期。
<project>
[...]
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>3.0.0-M3</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
[...]
</project>
在此之后,您应该只使用以下命令执行集成测试:
mvn clean verify
更新:如果插件的文档说:
默认情况下绑定到生命周期阶段:x
这意味着如果您添加如上所述的执行但尚未定义<phase>..</phaseY>
,则它仅限于给定生命周期阶段。这是一种默认值,您没有在pom配置中明确指出。