Maven Invoker 插件未检测到失败的测试

Maven Invoker Plugin not detecting failed test

作为根父 pom 项目的一部分,添加了几个集成测试以在示例项目上对其进行测试。

项目文件夹结构如下:

-root-maven-parent-project
    |- src
    |   |-it
    |      |-sample-project-test1
    |      |-sample-project-test2
    |      |-sample-project-test3
    |      |-settings.xml
    |- pom.xml

主要问题是:虽然 sample-project-test2 的构建错误地失败了(它不应该),但 Invoker 插件的构建是 SUCCESSFUL并且整体构建不会失败。

这里是关注的maven-invoker-plugin配置:

<profile>
    <id>it-tests</id>
    <build>
        <plugins>
            <!-- Integration tests configuration -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-invoker-plugin</artifactId>
                <version>3.0.0</version>
                <configuration>
                    <streamLogs>true</streamLogs>
                    <goals>
                        <goal>clean</goal>
                        <goal>generate-sources</goal>
                    </goals>
                    <settingsFile>src/it/settings.xml</settingsFile>
                    <failIfNoProjects>true</failIfNoProjects>
                </configuration>
                <executions>
                    <execution>
                        <id>integration-test-release</id>
                        <goals>
                            <goal>install</goal>
                            <goal>integration-test</goal>
                            <goal>verify</goal>
                        </goals>
                        <configuration>
                            <cloneProjectsTo>${project.build.directory}/its/sample-project-test1</cloneProjectsTo>
                            <pom>src/it/sample-project-test1/pom.xml</pom>
                            <properties>
                                <scmBranch>release-something</scmBranch>
                            </properties>
                        </configuration>
                    </execution>
                    <execution>
                        <id>integration-test-hotfix</id>
                        <goals>
                            <goal>install</goal>
                            <goal>integration-test</goal>
                            <goal>verify</goal>
                        </goals>
                        <configuration>
                            <cloneProjectsTo>${project.build.directory}/its/sample-project-test2</cloneProjectsTo>
                            <pom>src/it/sample-project-test2/pom.xml</pom>
                            <properties>
                                <scmBranch>hotfix-something</scmBranch>
                            </properties>
                        </configuration>
                    </execution>
                    <execution>
                        <id>integration-test-master</id>
                        <goals>
                            <goal>install</goal>
                            <goal>integration-test</goal>
                            <goal>verify</goal>
                        </goals>
                        <configuration>
                            <cloneProjectsTo>${project.build.directory}/its/sample-project-test3</cloneProjectsTo>
                            <pom>src/it/sample-project-test3/pom.xml</pom>
                            <properties>
                                <scmBranch>master</scmBranch>
                            </properties>
                        </configuration>
                    </execution>
            </plugin>
        </plugins>
    </build>
</profile>

如您所见,配置了多个执行,因为每个执行都需要自己的属性。每次执行也是指向自己的集成测试工程和pom.

特定执行的构建显然失败了:

[INFO] [INFO] ------------------------------------------------------------------------
[INFO] [INFO] BUILD FAILURE
[INFO] [INFO] ------------------------------------------------------------------------
[INFO] [INFO] Total time: 2.337 s
[INFO] [INFO] Finished at: 2017-07-04T17:35:49+02:00
[INFO] [INFO] Final Memory: 12M/220M
[INFO] [INFO] ------------------------------------------------------------------------
[INFO] [ERROR] Failed to execute goal org.apache.maven.plugins:maven-enforcer-plugin:1.4.1:enforce (enforce-snapshot-management) on project cmp-sample-project-test2: Some Enforcer rules have failed. Look above for specific messages explaining why the rule failed. -> [Help 1]
[INFO] [ERROR] 
[INFO] [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[INFO] [ERROR] Re-run Maven using the -X switch to enable full debug logging.
[INFO] [ERROR] 
[INFO] [ERROR] For more information about the errors and possible solutions, please read the following articles:
[INFO] [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
[INFO]           pom.xml .......................................... FAILED (4.1 s)
[INFO]   The build exited with code 1. See C:\data\git-repositories\root-maven-parent\target\its\sample-project-test2\build.log for details.

然而,在构建的底部,我们看到 maven-invoker-pluginverify 目标聚合了结果,将相关测试标记为 Passed 并使构建 SUCCESS:

[INFO] 
[INFO] --- maven-invoker-plugin:3.0.0:verify (integration-test-release) @ root-maven-parent ---
[INFO] -------------------------------------------------
[INFO] Build Summary:
[INFO]   Passed: 1, Failed: 0, Errors: 0, Skipped: 0
[INFO] -------------------------------------------------
[INFO] 
[INFO] --- maven-invoker-plugin:3.0.0:verify (integration-test-hotfix) @ root-maven-parent ---
[INFO] -------------------------------------------------
[INFO] Build Summary:
[INFO]   Passed: 1, Failed: 0, Errors: 0, Skipped: 0
[INFO] -------------------------------------------------
[INFO] 
[INFO] --- maven-invoker-plugin:3.0.0:verify (integration-test-master) @ root-maven-parent ---
[INFO] -------------------------------------------------
[INFO] Build Summary:
[INFO]   Passed: 1, Failed: 0, Errors: 0, Skipped: 0
[INFO] -------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------

此外,仅通过 运行 来自命令行的失败测试为:

mvn invoker:integration-test@integration-test-hotfix invoker:verify -Pit-tests

测试项目的子构建失败,输出在测试摘要中正确标记为Failed,构建正确以FAILURE结束。

问题:为什么在使用maven-invoker-plugin执行多个集成测试时,虽然测试失败,但在测试摘要中标记为Passed并且构建不会失败,而 运行 只有隔离测试一切都正确失败?

注意:未使用 invoker property file

使用这个配置:-

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-enforcer-plugin</artifactId>
        <version>1.0</version>
        <configuration>
            <rules>
                <banDuplicateClasses>
                    <findAllDuplicates>true</findAllDuplicates>
                </banDuplicateClasses>
            </rules>
            <fail>false</fail>
        </configuration>
        <dependencies>
            <dependency>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>extra-enforcer-rules</artifactId>
                <version>1.0-alpha-1</version>
            </dependency>
        </dependencies>
    </plugin>

有关更多信息,请参阅此 link:

http://maven.apache.org/enforcer/maven-enforcer-plugin/

问题已解决,但我认为插件的行为可以改进(见下文)。

整个maven-invoker-plugin被缩减为如下配置:

<profile>
    <id>it-tests</id>
    <build>
        <plugins>
            <!-- Integration tests configuration -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-invoker-plugin</artifactId>
                <version>3.0.0</version>
                <configuration>
                    <streamLogs>true</streamLogs>
                    <goals>
                        <goal>clean</goal>
                        <goal>generate-sources</goal>
                    </goals>
                    <settingsFile>src/it/settings.xml</settingsFile>
                    <failIfNoProjects>true</failIfNoProjects>
                    <cloneProjectsTo>${project.build.directory}/its</cloneProjectsTo>
                </configuration>
                <executions>
                    <execution>
                        <id>integration-test-release</id>
                        <goals>
                            <goal>install</goal>
                            <goal>integration-test</goal>
                            <goal>verify</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</profile>

基本上:只有一个插件执行,而不是每个测试执行一次,这确实是冗长且不可扩展的,但由于每个集成测试中相同 属性 具有不同值的需要。显然,这是不可能通过 pom 配置实现的,只能通过 test.properties 文件实现——除非我没记错。 因此,作为对上述配置的补充,我在每个集成测试项目文件夹中添加了一个包含以下内容的 test.properties 文件,例如:

scmBranch=master

事实上替换了 pom.xml 文件中的内容(作为 maven-invoker-plugin:

执行的一部分
<properties>
    <scmBranch>master</scmBranch>
</properties>

这种机制(插件的单次执行 + 每个测试文件夹的测试属性文件)解决了这个问题,允许构建具有多个集成测试,每个测试对相同的 属性 都有自己不同的值。希望此解决方案可以帮助解决类似问题。

这是构建正确聚合测试并有效地尊重其子构建输出的最终结果(而在构建之前每次生成 6 Build Summary of Passed: 1,虽然不正确) .

[INFO] --- maven-invoker-plugin:3.0.0:verify (pom-integration-test) @ root-maven-parent ---
[INFO] -------------------------------------------------
[INFO] Build Summary:
[INFO]   Passed: 6, Failed: 0, Errors: 0, Skipped: 0
[INFO] -------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------

但是,还有一些问题

  • 不使用test.properties文件,如何通过pom.xml配置实现同样的效果?通常,它应该只是一个替代方案,而不是强制性的和唯一可能的解决方案。这就是为什么这对我来说是一个不完整的功能(错误?)。
  • 插件的多次执行导致构建结束时的测试摘要正确遵循执行顺序,执行的测试数量(在这种情况下每次执行总是 1 个),但显然没有反映每个子构建的有效结果。为什么?这可能是由于意外使用而导致的错误或插件的不当行为。