Maven:在“发行:准备”阶段,如何在“安装”阶段之后运行一些测试?

时间:2019-04-22 23:36:55

标签: java maven

我有一些复杂的测试,这些测试使用独立的Maven项目来验证一些代码。这些Maven项目由测试以编程方式打包,然后使用生成的.jar文件。这些项目使用了我的主要多模块项目当前版本pom.xml中的工件。换句话说,测试的Maven项目需要能够找到我的主项目提供的工件。

在IDE中,一切正常,因为运行测试时,动态解析了主项目中的当前工件(不需要本地存储库)。另外,我可以在运行测试之前安装那些SNAPSHOT版本。但是,当我要发布项目的新版本时,我需要release:prepare进行以下操作:

  • 更新版本(删除所有的“ -SNAPSHOT”)。
  • 运行所有不以*.PostInstallTest.java结尾的测试。
  • 包装工件并在本地安装
  • THEN运行以*.PostInstallTest.java结尾的测试,因为这些测试需要访问以前安装的工件!如果测试失败,则release:prepare不会推送任何提交。

我知道这不是完美的,因为当“ PostInstallTest”测试失败时,可以在本地安装工件的“不良”版本。但我宁愿完全不要运行这些测试!

当前,我唯一的工作思路是在使用release配置文件时设置系统属性,如果该属性存在,则让*.PostInstallTest.java文件禁用自身。这样,当在我的IDE中运行这些测试(没有release配置文件)时,它们仍然可以工作,但是在release:prepare命令中根本不会执行。但是,我还是希望它们能被执行。

我查看了Maven Release Plugin的preparationGoals配置,但不确定它如何帮助我。我还查看了Maven Failsafe Plugin,但它似乎不支持“安装”阶段。

所以,我的问题:在使用“ release:prepare”(或“安装”阶段,但在默认插件之后)?

更新:如果可以帮助您了解发生了什么,这是一个快速模式:

enter image description here

更新2 :毕竟我没有正确测试故障安全。它确实在“安装”阶段起作用!看看df778899的答案。

2 个答案:

答案 0 :(得分:2)

检查以下设置。 它依靠preparationGoals来执行安装。 在故障安全插件的配置中,我将其配置为在安装阶段运行。

我相信这种方法的变种也可以正常工作-您可以尝试从任何阶段(将阶段设置为None)解除绑定故障保护,并在preparationGoals中显式调用它(可能这需要一些额外的配置,例如执行id ,但我认为您可以从这一点继续。)

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
        <plugin>
            <artifactId>maven-failsafe-plugin</artifactId>
            <executions>
                <execution>
                    <phase>install</phase>
                    <goals>
                        <goal>integration-test</goal>
                        <goal>verify</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-release-plugin</artifactId>
            <version>2.5.3</version>
            <configuration>
                <preparationGoals>clean verify install</preparationGoals>
            </configuration>
            <dependencies>
                <dependency>
                    <!-- Specify the version of maven-scm-plugin to avoid https://issues.apache.org/jira/browse/SCM-682
                    (Maven release fails when releasing from a named branch)
                    -->
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-scm-plugin</artifactId>
                    <version>1.9.5</version>
                </dependency>
            </dependencies>
        </plugin>
    </plugins>
</build>

输出(稍作编辑以减少噪音)显示,preparationGoals启动的验证任务在安装后执行。

[INFO] --- maven-release-plugin:2.5.3:prepare (default-cli) @ demo ---
...
[INFO] Checking dependencies and plugins for snapshots ...
What is the release version for "demo"? (com.example:demo) 0.0.1: :
What is SCM release tag or label for "demo"? (com.example:demo) demo-0.0.1: :
What is the new development version for "demo"? (com.example:demo) 0.0.2-SNAPSHOT: :
[INFO] Transforming 'demo'...
[INFO] Not generating release POMs
[INFO] Executing goals 'clean verify install'...
[WARNING] Maven will be executed in interactive mode, but no input stream has been configured for this MavenInvoker instance.
[INFO] [INFO] Scanning for projects...
[INFO] [INFO]
[INFO] [INFO] --------------------------< com.example:demo >--------------------------
[INFO] [INFO] Building demo 0.0.1
[INFO] [INFO] --------------------------------[ jar ]---------------------------------
[INFO] [INFO]
[INFO] [INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) @ demo ---
[INFO] [INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ demo ---
[INFO] [INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ demo ---
[INFO] [INFO] --- maven-resources-plugin:3.1.0:testResources (default-testResources) @ demo ---
[INFO] [INFO] --- maven-compiler-plugin:3.8.0:testCompile (default-testCompile) @ demo ---
[INFO] [INFO] --- maven-surefire-plugin:2.22.1:test (default-test) @ demo ---
[INFO] [INFO] --- maven-jar-plugin:3.1.1:jar (default-jar) @ demo ---
[INFO] [INFO] --- spring-boot-maven-plugin:2.1.4.RELEASE:repackage (repackage) @ demo ---
[INFO] [INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ demo ---
[INFO] [INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ demo ---
[INFO] [INFO] --- maven-resources-plugin:3.1.0:testResources (default-testResources) @ demo ---
[INFO] [INFO] --- maven-compiler-plugin:3.8.0:testCompile (default-testCompile) @ demo ---
[INFO] [INFO] --- maven-surefire-plugin:2.22.1:test (default-test) @ demo ---
[INFO] [INFO] --- maven-jar-plugin:3.1.1:jar (default-jar) @ demo ---
[INFO] [INFO] Building jar: demo-0.0.1.jar

[INFO] [INFO] --- maven-install-plugin:2.5.2:install (default-install) @ demo ---
[INFO] [INFO] Installing demo-0.0.1.jar to 

[INFO] [INFO] --- maven-failsafe-plugin:2.22.1:integration-test (default) @ demo ---
[INFO] [INFO]
[INFO] [INFO] --- maven-failsafe-plugin:2.22.1:verify (default) @ demo ---
[INFO] [INFO] ------------------------------------------------------------------------
[INFO] [INFO] BUILD SUCCESS
[INFO] [INFO] ------------------------------------------------------------------------
[INFO] [INFO] Total time: 13.784 s

[INFO] Checking in modified POMs...

答案 1 :(得分:1)

我认为这还不能解决问题,但是为了扩展@Lesiak的观点,Failsafe插件在安装阶段本身看起来还不错。例如:

<plugin>
    <artifactId>maven-failsafe-plugin</artifactId>
    <executions>
        <execution>
            <phase>install</phase>
            <goals>
                <goal>integration-test</goal>
                <goal>verify</goal>
            </goals>
        </execution>
    </executions>
</plugin>

输出结果:

...
[INFO] --- maven-install-plugin:2.4:install (default-install) @ it-test ---
[INFO] Installing ...\target\it-test-0.0.1-SNAPSHOT.jar to ...\.m2\repository\group\it-test\0.0.1-SNAPSHOT\it-test-0.0.1-SNAPSHOT.jar
[INFO] Installing ...\pom.xml to ...\.m2\repository\group\it-test\0.0.1-SNAPSHOT\it-test-0.0.1-SNAPSHOT.pom
[INFO]
[INFO] --- maven-failsafe-plugin:3.0.0-M3:integration-test (default) @ it-test ---
[INFO]
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running TheIT
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.093 s - in TheIT
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO]
[INFO] --- maven-failsafe-plugin:3.0.0-M3:verify (default) @ it-test ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS

请注意maven-failsafe-plugin:3.0.0-M3:integration-testmaven-failsafe-plugin:3.0.0-M3:verify目标的最终运行方式-在install阶段。