Search code examples
javamavenjunitsurefire

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


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.


Solution

  • 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