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

时间:2017-07-08 02:09:39

标签: compilation maven-3 integration-testing multi-module maven-failsafe-plugin

主要目标

使用命令行在Maven Standard Directory Layout之后的多模块存储库的两个对等模块中编译和运行集成测试:

  

模块
  | _src
  | ____它集成测试
  | ____主要
  | ____测试单元测试

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

简介

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

在核心模块上发出mvn help:describe -Dcmd=install时,它显示Failsafe插件(在我的POM中另外定义)未绑定到相应的阶段。这可能解释了为什么我不能运行集成测试,但不能解释为什么它不能被绑定,因为它是在POM中定义的。此外,它没有解释为什么int-test源没有编译,因为我目前正在理解由test-compile阶段的编译器插件完成的int-test编译,因为没有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
  | ____ pom.xml
  |
  <_ em> 下面提供的模块核心   | ____ pom.xml
  |
  | _ module-backend (春天)
  | ____ pom.xml
  |
  | _ 模块前端(angular2)
  | ____ pom.xml

module-parent / 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>

module-core / 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的专家。春天父母POM || Springs父母的父POM正在设置Failsafe插件,所以核心模块 - 我相信 - 应该继承它。

2 个答案:

答案 0 :(得分:0)

我认为Failsafe没有显示在..help:describe..旁边的各个阶段是我的问题的根源,但事实证明它不是(请参阅故障安全未绑定到X或Y的答案)。在两个不同的源位置(src / it和src / test)之间设置两个测试类型(int / unit)是问题,并且在使用Maven时它似乎是常见的配置痛苦。这是因为它违背了Maven假设项目将如何设置的惯例。

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

解决方案1:常规

  1. 将整合测试移至&#34; src / test&#34;每个模块的classPath。
  2. 为集成测试命名,其中包括&#34; IT&#34; [Failsafe default name convention]
  3. mvn install parent - &gt;核心 - &gt;后端模块
  4. 解决方案2:非传统

    1. build-helper-maven-plugin添加到module-parent下的父pom.xml。
    2. mvn install parent - &gt;核心 - &gt;后端模块
    3. module-parent / 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>
      

答案 1 :(得分:0)

Failsafe未绑定到X或Y阶段。

其中x = integration-test,y = verify。

使用mvn help:describe -Dcmd=install的输出来确定绑定看起来更像是一个红鲱鱼。仅仅因为插件未列在输出中的某个阶段旁边,并不意味着插件在mvn install期间无法执行

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

  

<强>孤模块   
| _ src   
| _主要   
| _ java   
| _ Service.java   
| _ src   
| _测试   
| _ java   
| _ ServiceIT.java int-test   
| _ ServiceTest.java 单元测试   
  | _ 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 / *中观察),但它只运行单元测试(在cmd行观察)。接下来,运行mvn help-describe -Dcmd=install

注意“未定义”

* 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>

然后, 对于X和Y阶段,mvn help-describe -Dcmd=install仍会显示“未定义”的输出。

但是, 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

这向我们展示了X阶段正在执行failsafe-plugin的目标,尽管..help:describe..显示阶段为“未定义”。