【问题标题】:Running a plugin goal before the default one在默认目标之前运行插件目标
【发布时间】:2014-01-26 20:44:45
【问题描述】:

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


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

第 1 次尝试

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

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

第 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”?

谢谢!

【问题讨论】:

  • 在测试之前检查一下假维基的存在不是更好吗?除此之外,这听起来像是在运行集成测试而不是单元测试。

标签: maven maven-plugin


【解决方案1】:

正如@khmarbaise 所说,您的解决方案仍然很老套,因为整个测试看起来像集成测试,应该由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?

【讨论】:

  • 感谢您的解决方案、为我指出故障安全插件以及文档链接!
  • 非常好的技巧,但它会输出以下内容: [INFO] --- maven-surefire-plugin:2.18.1:test (default-test) [INFO] 跳过测试。更好的配置是“取消绑定”阶段: default-testnone 。这也适用于任何其他可能没有跳过属性的插件。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-05-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-26
  • 2011-03-22
  • 1970-01-01
相关资源
最近更新 更多