我有一些JUnit测试,它们在test
阶段并行执行,每个测试输出一个.json文件,我想调用一个定制的.json方法,在所有测试完成后对这些文件进行一些聚合和后处理。
integration-test
阶段之后是默认Maven生命周期中的post-integration-test
阶段,但是测试阶段之后没有post-test
阶段,为此我不想滥用其他阶段。
问题:在test
阶段结束时对结果进行后处理的推荐方法是什么?
发布于 2016-04-02 12:56:34
正如在另一篇这样的文章中所描述的那样,Maven中没有post-test
阶段是有原因的(主要是单元测试就是单元测试)。
但是,在您的情况下,您不需要创建额外的Maven插件,这可能解决了这个问题,但也在维护、测试、共享方面增加了额外的复杂性。
由于Java方法中已经有了所需的代码--正如问题中提到的--使用Exec插件及其java
目标可能更有意义。
因此,您可以简单地添加到POM中:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.1.1</version>
<executions>
<execution>
<phase>test</phase> <!-- executed as post-test, that is, after Surefire default execution -->
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>com.sample.JsonFileAggregator</mainClass> <!-- your existing Java code, wrapped in a main -->
<arguments>
<argument>${project.build.directory}</argument> <!-- directory from where to load json files -->
</arguments>
<classpathScope>test</classpathScope> <!-- if your Java code is in test scope -->
</configuration>
</execution>
</executions>
</plugin>
也就是说,将其执行绑定到test
阶段,Maven将在任何默认绑定之后(因此在默认的Maven强制执行之后)执行它,并以post-test
的形式执行。
然后,可以通过巧尽心思构建的Java (如果不存在的话)调用现有的Java代码,并可能将参数传递给它(例如,从哪里加载.json文件的目录,在上面的片段中,通过其标准属性${project.build.directory}
到target
文件夹)。此外,正如片段中提到的,您的test
代码可能位于src/test/java
作用域(即src/test/java
下),因此,为了使其可见,您还需要相应地配置classpathScope
。
https://stackoverflow.com/questions/36342574
复制相似问题