Maven 集成测试阶段未绑定到 Failsafe

Maven integration-test phase not binding to Failsafe

主要目标

使用命令行在 Maven 的 Standard Directory Layout:

之后的多模块存储库的两个对等模块中进行编译和 运行 集成测试

module
    |_src
    |____it          integration-test
    |____main
    |____test      unit-test

update: 事实证明,将 IT int 测试放在“src/it”文件夹中并不是 Maven 的惯例。 “src / it”用于 maven-plugin 特定的集成测试。虽然,“src / it”当然可以为 IT int 测试配置 - 如果您需要配置而不是约定。

简介

我有一个多模块存储库,其模块继承自父 POM(项目和 Spring 相关)。我无法从命令行从“新鲜”mvn clean 开始编译或 运行 集成测试,但我可以让 IntelliJ 编译所有源代码和 运行 所有测试(作为 Maven 模块) - 之后,int 测试也将在命令行 运行,尽管没有将 Failsafe 绑定到阶段(虽然有点有意义)。我无法确定导致命令行冲突的原因(如果有的话)。已经尽我所能地搜索了这个问题,而且——是的——我已经尝试了几乎所有我能做的事情google,但无济于事。我的 POM 在此处定义为所有先前尝试更改后的当前状态。

在核心模块上发布 mvn help:describe -Dcmd=install 时,它显示 Failsafe 插件(在我的 POM 中定义)未绑定到相应的阶段。这也许可以解释为什么我不能 运行 集成测试,但不能解释为什么它无法绑定,因为它是在 POM 中定义的。此外,它没有解释为什么 int-test 源代码没有编译,因为我目前理解 int-test 编译由编译器插件在 test-compile 阶段完成,因为没有 int-test-compile 阶段在 Maven 生命周期中。这是正确的还是也在 integration-test 阶段完成?

Maven 帮助输出

假设我 mvn clean install 模块父。那么,

~$: pwd
/repo/module-core
~$: mvn help:describe -Dcmd=install
[INFO] 'install' is a phase corresponding to this plugin:
org.apache.maven.plugins:maven-install-plugin:2.4:install
It is a part of the lifecycle for the POM packaging 'jar'. This lifecycle includes the following phases:
* validate: Not defined
* initialize: Not defined
* generate-sources: Not defined
* process-sources: Not defined
* generate-resources: Not defined
* process-resources: org.apache.maven.plugins:maven-resources-plugin:2.6:resources
* compile: org.apache.maven.plugins:maven-compiler-plugin:3.1:compile
* process-classes: Not defined
* generate-test-sources: Not defined
* process-test-sources: Not defined
* generate-test-resources: Not defined
* process-test-resources: org.apache.maven.plugins:maven-resources-plugin:2.6:testResources
* test-compile: org.apache.maven.plugins:maven-compiler-plugin:3.1:testCompile
* process-test-classes: Not defined
* test: org.apache.maven.plugins:maven-surefire-plugin:2.12.4:test
* prepare-package: Not defined
* package: org.apache.maven.plugins:maven-jar-plugin:2.4:jar
* pre-integration-test: Not defined
* integration-test: Not defined
* post-integration-test: Not defined
* verify: Not defined
* install: org.apache.maven.plugins:maven-install-plugin:2.4:install
* deploy: org.apache.maven.plugins:maven-deploy-plugin:2.7:deploy

项目结构概览

MainRepo
    |_ module-parent provided below
    |____ pom.xml
    |
    |_ module-core provided below
    |____ pom.xml
    |
    |_ module-backend (spring)
    |____ pom.xml
    |
    |_ module-frontend (angular2)
    |____ pom.xml

模块父/pom.xml

<project>
    <!-- ... -->
    <groupId>my.apps.module</groupId>
    <artifactId>module-parent</artifactId>
    <version>0.1.0</version>
    <packaging>pom</packaging>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.3.RELEASE</version>
    </parent>
    <modules>
        <module>../module-core</module>
        <module>../module-backend</module>
    </modules>
    <!-- ... -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-failsafe-plugin</artifactId>
                <version>2.20</version>
                <executions>
                    <execution>
                        <id>module-parent-failsafe-it</id>
                        <phase>integration-test</phase>
                        <goals>
                            <goal>integration-test</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>module-parent-failsafe-verify</id>
                        <phase>verify</phase>
                        <goals>
                            <goal>verify</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

模块核心/pom.xml

<project>
    <!-- ... -->
    <groupId>my.apps.module</groupId>
    <artifactId>module-core</artifactId>
    <version>0.1.0</version>

    <parent>
        <groupId>my.apps.module</groupId>
        <artifactId>module-parent</artifactId>
        <version>0.1.0</version>
    </parent>
    <!-- ... -->
</project>

其他详细信息/编辑

也意味着我审查了有效的 POM。看起来不错,但我不是 Maven 专家。 Spring 父 POM || Springs parent 的 parent POM 正确设置了 Failsafe 插件,所以核心模块——我相信——应该继承自那个。

我假设 Failsafe 没有出现在 ..help:describe.. 中各个阶段旁边是我的问题的根源,但事实证明它不是(参见 failsafe not binding to X or Y 的答案)。在两个不同的源位置(src/it 和 src/test)之间设置两种测试类型(int/unit)是问题所在,这似乎是使用 Maven 时常见的配置难题。这是因为它违背了 Maven 假设项目将被设置的惯例。

为了实现使用两个不同的源文件夹进行测试,我找到[Kainulainen-2012]谁演示了[(org.codehaus.mojo:build-helper- maven-plugin)] 它使用额外的源配置执行,以便在编译期间使用。这解决了上面定义的主要目标,尽管是以非常规的方式,同时也引入了其他问题。或者,使用 Maven 的约定只需要将集成测试移动到每个模块的 "src/test" 位置并可能更新测试名称。我没有遇到其他问题,我发现它是更简单的解决方案。

解决方案 1:常规

  1. 将集成测试移动到每个模块的 "src/test" 类路径。
  2. 为集成测试命名,其中包括 "IT" [Failsafe default name convention]
  3. mvn install 父级 -> 核心 -> 后端模块

解决方案 2:非常规

  1. build-helper-maven-plugin 添加到 module-parent 下的父级 pom.xml。
  2. mvn install 父级 -> 核心 -> 后端模块

模块父级/pom.xml:

<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <version>3.0.0</version>
            <executions>
                <execution>
                    <id>add-integration-test-sources</id>
                    <phase>generate-test-sources</phase>
                    <goals>
                        <goal>add-test-source</goal>
                    </goals>
                    <configuration>
                        <sources>
                            <source>src/it/java</source>
                        </sources>
                    </configuration>
                </execution>
                <execution>
                    <id>add-integration-test-resources</id>
                    <phase>generate-test-resources</phase>
                    <goals>
                        <goal>add-test-resource</goal>
                    </goals>
                    <configuration>
                        <resources>
                            <resource>
                                <filtering>true</filtering>
                                <directory>src/it/resources</directory>
                            </resource>
                        </resources>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <!-- ... -->
    </plugins>
</build>

故障保护未绑定到 X 或 Y 相。

其中 x = 集成测试,y = 验证。

使用 mvn help:describe -Dcmd=install 的输出来确定绑定似乎更像是转移注意力。仅仅因为插件没有在输出中的阶段旁边列出,并不意味着插件在 mvn install

期间无法执行

假设您有一个严格基于 Maven 预期约定的简单单模块项目:

lone-module
    |_ src
        |_ main
            |_ java
                |_ Service.java
    |_ src
        |_ test
            |_ java
                |_ ServiceIT.java         int-test
                |_ ServiceTest.java     unit-test
    |_ pom.xml

并让pom.xml定义如下:

<project>
    <!-- ... -->
    <groupId>single.module.apps</groupId>
    <artifactId>lone-module</artifactId>
    <version>0.1.0</version>

    <dependencies>
        <!-- ... -->
    </dependencies>
</project>

那么, mvn install 编译单元测试和集成测试(如在 target / test-class / * 中观察到的),但它仅 运行s 单元测试(如在 cmd 行中观察到的)。接下来,运行mvn help-describe -Dcmd=install

通知"Not defined"

* integration-test: Not defined
* ...
* verify: Not defined

这可能是因为我们没有在 pom.xml 中定义 maven-failsafe-plugin。因此,让 pom.xml 包括所述插件:

<project>
    <!-- ... -->
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-failsafe-plugin</artifactId>
            <version>2.20</version>
            <executions>
                <execution>
                    <goals>
                        <goal>integration-test</goal>
                        <goal>verify</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</project>

那么, mvn help-describe -Dcmd=install 仍将显示 X 和 Y 阶段的 "Not defined" 的输出。

但是, mvn install 现在还将 运行 使用 maven-failsafe-plugin 的集成测试。

[INFO] --- maven-failsafe-plugin:2.20:integration-test (default) @ lone-module ---
[INFO]
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running ServiceIT
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.388 s - in ServiceIT

这向我们表明,尽管 ..help:describe.. 将阶段显示为 "Not defined."

,但 X 阶段正在执行故障安全插件的目标