在默认插件之前运行插件目标

时间:2014-01-26 20:44:46

标签: maven maven-plugin

TL; DR:使用maven,我希望在测试实际运行之前,在test阶段开始时运行插件目标。什么是干净的方式呢?


我想在测试实际运行之前打印一条消息。因此,我想在测试阶段开始时使用echo pluginecho目标(告诉用户如果每次测试都失败了,他最好看看README因为他应该先建立一个测试环境)

尝试n°1

一种简单的方法可能是在the previous phaseprocess-test-classes中运行此插件。

它可行,但将此任务绑定到此阶段似乎在语义上不正确...

尝试n°2

根据Maven documentationWhen multiple executions are given that match a particular phase, they are executed in the order specified in the POM, with inherited executions running first.,我尝试明确设置surefire插件:

...
 <plugin>
   <groupId>com.soebes.maven.plugins</groupId>
   <artifactId>maven-echo-plugin</artifactId>
   <version>0.1</version>
   <executions>
     <execution>
       <phase>test</phase>
       <goals>
         <goal>echo</goal>
       </goals>
     </execution>
   </executions>
   <configuration>
     <echos>
       <echo>*** If most tests fail, make sure you've installed the fake wiki. See README for more info ***</echo>
     </echos>
   </configuration>
 </plugin>
 <plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-surefire-plugin</artifactId>
   <version>2.16</version>
   <executions>
     <execution>
       <phase>test</phase>
       <goals>
         <goal>test</goal>
       </goals>
     </execution>
   </executions>
 </plugin>
 ...

但测试在我的信息被打印之前运行。

所以,简而言之:有没有办法实现我的目标,或者我应该坚持使用“process-test-classes解决方案”,即使它看起来有点像“hacky”?

谢谢!

1 个答案:

答案 0 :(得分:7)

正如@khmarbaise所说,你的解决方案仍然是hacky,因为整个测试看起来像集成测试,应由Failsafe Plugin处理。 Failsafe有一个很好的阶段pre-integration-test用于测试虚假维基等:)

基于Guide to Configuring Default Mojo Executions这对我有用:

<plugin>
    <groupId>com.soebes.maven.plugins</groupId>
    <artifactId>maven-echo-plugin</artifactId>
    <version>0.1</version>
    <executions>
        <execution>
            <id>1-test</id>
            <phase>test</phase>
            <goals>
                <goal>echo</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <echos>
            <echo>*** If most tests fail, make sure you've installed the fake wiki. See README for more info ***</echo>
        </echos>
    </configuration>
</plugin>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.16</version>
    <executions>
        <execution>
            <id>default-test</id>
            <configuration>
                <skip>true</skip>
            </configuration>
        </execution>
        <execution>
            <id>2-test</id>
            <phase>test</phase>
            <goals>
                <goal>test</goal>
            </goals>
        </execution>
    </executions>
</plugin>

这对我来说很奇怪;)

  

我有两个执行绑定到generate-sources的插件,一个列在大约6个插件的列表中,另一个列在最后。但是,最后列出的那个(取决于首先列出的那个)总是首先执行。

How can I execute several maven plugins within a single phase and set their respective execution order?