0

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

项目文件夹的结构如下:

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

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

以下是相关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.

但是,在构建的底部,我们看到聚合结果的verify目标,maven-invoker-plugin将相关测试标记为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在测试摘要中标记为并且构建没有失败,而只运行隔离测试一切都失败了?

注意:不使用调用程序属性文件

4

2 回答 2

1

问题通过以下解释解决,尽管我认为插件的行为可以改进(见下文)。

整体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>

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

这是构建正确聚合测试并有效尊重其子构建输出的最终结果(虽然在构建之前每次生成 6Build Summary个,但不正确)。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 个),但显然不能反映每个子的有效结果-建造。为什么?这可能是插件的一个错误或不当行为,可能是由于它的意外使用。
于 2017-07-05T14:53:10.023 回答
-1

使用此配置:-

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

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

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

于 2017-07-04T17:07:27.053 回答