Maven: surefire plugin ignores -Dgroups when <groups> is already configured
Asked Answered
M

1

5

My project has a few JUnit tests that I rarely want to run. To do so I put them in a @Category and then I did this:

<plugin>
   <artifactId>maven-surefire-plugin</artifactId>
   <!-- Run all but the inject tests -->
   <configuration>
      <groups>!be.test.InjectTests</groups>
   </configuration>
</plugin>

I'd like to override the configuration in the command-line to run the Inject tests like this:

mvn clean install -Dgroups=be.test.InjectTests

But that doesnt work, the -Dgroups gets ignored by Maven.

If I don't put the the command works fine.

Myca answered 18/1, 2013 at 21:57 Comment(0)
K
8

Unfortunately it seems that if something is set in the pom it is not easily overridden (if you set skipTests explicitly it'd be hard to override with a property as well)... But! (and this is a bit of a hack) you can defer the setting of the property, to a pom property, and then override it on the command line.

<project>
...
<properties>
    <groups>!Slow</groups>
<properties>
....
<build>
    <plugins>
        ...
        <plugin>
           <groupId>org.apache.maven.plugins</groupId>
           <artifactId>maven-surefire-plugin</artifactId>
           <version>2.13</version>
           <configuration>
            <groups>${groups}</groups>
           </configuration>
        </plugin>
    </plugins>
</build>
...

With this (and a quick built out project, running on OSX, Maven 3.0.4, Java 1.6.0_37):

$ mvn clean test
...
Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
...
$ mvn clean test -Dgroups=Slow
...
Results :

Tests run: 3, Failures: 0, Errors: 0, Skipped: 0
Koweit answered 18/1, 2013 at 22:50 Comment(1)
I just tried this and it does not actually work :| not sure why. Running mvn in debug I can see that the groups is assigned to whatever I pass on the command line, but the relevant @Tag test is still ignored and not executedStereoscope

© 2022 - 2024 — McMap. All rights reserved.