In my project I have a set of a very fast unit-tests and small integration-tests as well a set of slower integration, end2end and performance tests (10mintes+). I use junit5 tags to classify them and in my pom.xml
I configured maven-surefire-plugin
to run only the fast ones with <groups>fast</groups>
(I have other projects based on this one, that I'm working on in parallel, so I don't want to wait 10minutes after each tiny change to the base project).
However when I deploy to central using nexus-staging-maven-plugin
I definitely do want to run the slow ones also. Currently I do it by manually overriding groups
system property on the command line: mvn -Dgroups="fast|slow" clean deploy
. This is however prone to mistakes and I wonder if there's a way to automate it: is it for example possible to automatically use different configuration for maven-surefire-plugin
depending whether I do install
or deploy
?

- 2,513
- 4
- 25
- 31
2 Answers
Yes. That is what the execution tag is for.
To bind maven-surefire-plugin (or any plugin for that matter) to, for example, the install phase, you would configure:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.1.2</version>
<executions>
<execution>
<id>execution1</id>
<phase>install</phase>
<configuration>
<!-- include your configurations here -->
</configuration>
<goals>
<!-- goals go here -->
</goals>
</execution>
</executions>
</plugin>
You can of course have multiple execution
entries, so you can create a different configuration
for your deploy phase.
As mentioned in a comment by @khmarbaise, the maven-surefire-plugin is the default plugin intended for unit tests, and as such it already has some default bindings in the Maven lifecycle. Although it is possible to disable these defaults, the better solution is to consider using the maven-failsafe-plugin. This plugin 1) does not have any default bindings, 2) is intended specifically for integration tests, and 3) the configurations are >90% compatible with the surefire plugin (failsafe is a fork of surefire).

- 10,003
- 10
- 39
- 90
-
this almost worked, however this resulted in 3 test runs because of the "default binding" in `test` phase that was running **all** tests ;-] to reconfigure it, I had to add an execution with id `default-test`. After that I removed the "install-tests` retaining `deploy-tests` and now it works as intended :) Many thanks!! – morgwai Jul 26 '23 at 16:39
-
for future readers: `goals` should contain `
test ` as [documented here](https://maven.apache.org/surefire/maven-surefire-plugin/plugin-info.html) – morgwai Jul 26 '23 at 16:45 -
ah, if I bind an execution to `deploy` phase it runs the slow tests **after** the actual deployment :( ...and `deploy` is right after `install` (according to [the maven docs](https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html#default-lifecycle)) so I'm unable to bind where I need it, it seems :( any ideas for a workaround? – morgwai Jul 26 '23 at 17:02
-
ok, solved: the executions are performed in order they are defined in pom.xml, so placing surefire before nexus-staging did the trick (kudos to [this post](https://mkyong.com/maven/maven-plugin-execution-order-in-same-phase/)) – morgwai Jul 26 '23 at 18:24
-
1@morgwai Added information to my answer regarding failsafe plugin. – SiKing Jul 27 '23 at 16:04
to summarize all the comments to SiKing's answer:
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.1.2</version>
<executions>
<execution>
<!-- reconfigure the default execution from test phase -->
<id>default-test</id>
<configuration>
<excludedGroups>slow</excludedGroups>
</configuration>
<goals>
<goal>test</goal>
</goals>
</execution>
<execution>
<!-- additional execution at the beginning of deploy phase -->
<id>pre-deploy-test</id>
<phase>deploy</phase>
<configuration>
<groups>slow</groups>
</configuration>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<!-- nexus-staging must be placed *AFTER* surefire -->
<groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId>
<!-- nexus staging config here... -->
</plugin>
<!-- other plugins here... -->
</plugins>
update: as per this comment, some may consider the above an abuse of surefire, hence below is a version that uses maven-failsafe-plugin
for pre-deploy tests, so there's no need to "hack" into default-test
execution of surefire.
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.1.2</version>
<configuration>
<excludedGroups>slow</excludedGroups>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>3.1.2</version>
<executions>
<execution>
<id>pre-deploy-tests</id>
<phase>deploy</phase>
<configuration>
<includes>
<include>**/*Test.java</include>
<include>**/*Tests.java</include>
</includes>
<groups>slow</groups>
</configuration>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<!-- nexus-staging must be placed *AFTER* failsafe -->
<groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId>
<!-- nexus staging config here... -->
</plugin>
<!-- other plugins here... -->
</plugins>

- 2,513
- 4
- 25
- 31
-
2No... surefire plugin is inteded for unit tests ... if you have a kind of integration tests you should use failsafe plugin instead.... if you change the names of your tests (tags: slow) like `*IT.java` they will be executed in pre-integration-test, integration-test and post-integration-test phase which can be simply handled using like `mvn verify` for unit tests only `mvn test` and if you do `mvn deploy` the verify phase is part of it which means they are executed as well... as you wished... follow the conventions etc... – khmarbaise Jul 27 '23 at 11:25
-
@khmarbaise integration tests are run each time I do `mvn install` which is exactly what I do **not** want: my `slow` tests take about 10 minutes each time and I have other projects based on this one that I'm working on at the same time. I specifically need some tests to be executed only at the beginning of `deploy` phase and the above xml does exactly that. Nevertheless, it may be easier to use failsafe plugin for this, so that I do not need to reconfigure `default-test` execution of surefire: I'll try that when I have a moment: many thanks for the suggestion! :) – morgwai Jul 27 '23 at 17:52