【问题标题】:Maven integration-test phase not binding to FailsafeMaven 集成测试阶段未绑定到故障安全
【发布时间】:2017-12-12 09:43:32
【问题描述】:

主要目标

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

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

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

简介

我有多模块存储库,其模块继承自父 POM(项目和 Spring 相关)。我无法从“新鲜”mvn clean 开始的命令行编译或运行集成测试,但我可以让 IntelliJ 编译所有源并运行所有测试(作为 Maven 模块)——之后,int 测试也将运行在尽管没有将故障安全绑定到阶段(尽管有点道理),但命令行。如果有的话,我无法确定导致命令行冲突的原因。已经尽我所能搜索了这个问题,而且——是的——我已经尝试了几乎所有我能用谷歌搜索的东西,但无济于事。我的 POM 在这里定义为之前所有尝试更改后的当前状态。

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

Maven 帮助输出

假设我mvn clean install module-parent。那么,

~$: 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
|_ 模块父级 在下面提供
|____ pom.xml
|
|_ 模块核心 在下面提供
|____ pom.xml
|
|_ 模块后端(春季)
|____ pom.xml
|
|_ 模块前端 (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 的父 POM 正确设置了 Failsafe 插件,所以 core-module -- 我相信 -- 应该继承自该插件。

【问题讨论】:

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


    【解决方案1】:

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

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

    解决方案 1:常规

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

    解决方案 2:非常规

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

    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,直到情况表明需要单独的测试位置。
    • 此外,“src / it”类路径实际上是用于 maven-plugin 集成测试,不应与与开发 maven 插件无关的项目的集成测试相混淆。跨度>
    【解决方案2】:

    Failsafe 未绑定到 X 或 Y 阶段。

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

    使用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>
    

    那么, mvn help-describe -Dcmd=install 仍将显示 X 和 Y 相的“未定义”输出。

    但是, 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 阶段正在执行故障安全插件的目标,尽管 ..help:describe.. 将阶段显示为“未定义”。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-10
      • 2012-08-21
      • 1970-01-01
      • 1970-01-01
      • 2018-06-02
      • 2020-03-16
      相关资源
      最近更新 更多