2

TL;DR: Using maven, I want to run a plugin goal at the beginning of the test phase, before tests actually run. What would be a clean way to do it?


I want to print a message just before the tests actually run. Hence I want to use the echo goal of the echo plugin at the beginning of the test phase (to tell the user that if every tests fail, he'd better have a look at the README since there's a test environment he should set up first)

Attempt n°1

A simple approach could be to run this plugin in the previous phase, process-test-classes.

It works, but it doesn't seem semantically correct to bind this task to this phase...

Attempt n°2

According to Maven documentation, When multiple executions are given that match a particular phase, they are executed in the order specified in the POM, with inherited executions running first., so I tried to set explicitly the surefire plugin:

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

But tests run before my message is printed.

So, to put it in a nutshell: is there a way to reach my goal, or should I stick to the "process-test-classes solution" even though it seems a bit "hacky"?

Thanks!

gturri
  • 13,807
  • 9
  • 40
  • 57
  • 1
    Wouldn't it be better to check the existence of the fake wiki before your tests? Apart from that it sounds like running integration tests and not unit tests. – khmarbaise Jan 26 '14 at 21:41

1 Answers1

7

As @khmarbaise said, your solution is still hacky, because whole test looks like Integration Test and should be processed by Failsafe Plugin. Failsafe has nice phase pre-integration-test for testing fake wiki etc :)

Based on Guide to Configuring Default Mojo Executions this works for me:

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

This is very odd for me ;)

I have two plugins with executions bound to generate-sources, one listed first in the list of about 6 plugins and the other listed last. However, the one listed last (which depends on the one listed first) always executes first.

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

Community
  • 1
  • 1
MariuszS
  • 30,646
  • 12
  • 114
  • 155
  • 1
    Thank you, both for the solution, for pointing me out the failsafe plugin, and for the link to the documentation! – gturri Jan 27 '14 at 06:36
  • Very nice trick, but it will output the following: [INFO] --- maven-surefire-plugin:2.18.1:test (default-test) [INFO] Tests are skipped. A better configuration would be to 'unbind' the phase: default-test none . Also this can apply to any other plugin which might not have a skip property. – dcendents Dec 14 '15 at 20:23