5

I'm trying to exclude a single test from my maven build (I don't want the test to be compiled or executed). The following doesn't work:

<project ...>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <excludes>
            <exclude>**/MyTest.java</exclude>
          </excludes>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

What is the correct way to achieve my goal? I know that I can use the command-line option -Dmaven.test.skip=true, but I would like this to be part of the pom.xml.

oberlies
  • 11,503
  • 4
  • 63
  • 110
Konstantin Weitz
  • 6,180
  • 8
  • 26
  • 43
  • See [this](http://stackoverflow.com/questions/9123075/maven-how-can-i-skip-test-in-some-projects-via-command-line-options), is similar to your needs. (not the question, see the first answer) – Arturo Volpe Aug 12 '14 at 23:44

3 Answers3

16

Skip the test

From the docs, if you want to skip a test, you can use:

<project>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.17</version>
        <configuration>
          <excludes>
            <exclude>**/MyTest.java</exclude>
          </excludes>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

See the diference, in your example, you use <artifactId>maven-compiler-plugin</artifactId>, and the docs say that you shoul use <artifactId>maven-surefire-plugin</artifactId> plugin instead.

And, if you want to disable all test, you can use:

    <configuration>
      <skipTests>true</skipTests>
    </configuration>

Also, if you are using JUnit, you can use @Ignore, and add a message.

Exclude the test from compilation

From this answer, you can use. The trick is intercept the <id>default-testCompile</id> <phase>test-compile</phase> (default test compile phase) and exclude the class:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-compiler-plugin</artifactId>
  <executions>
    <execution>
      <id>default-testCompile</id>
      <phase>test-compile</phase>
      <configuration>
        <testExcludes>
          <exclude>**/MyTest.java</exclude>
        </testExcludes>
      </configuration> 
      <goals>
        <goal>testCompile</goal>
      </goals>
    </execution>                  
  </executions>
</plugin>
Community
  • 1
  • 1
Arturo Volpe
  • 3,442
  • 3
  • 25
  • 40
5

Exclude one test class, by using the exclamation mark (!)

mvn test -Dtest=!LegacyTest

Exclude one test method

mvn verify -Dtest=!LegacyTest#testFoo

Exclude two test methods

mvn verify -Dtest=!LegacyTest#testFoo+testBar

Exclude a package with a wildcard (*)

mvn test -Dtest=!com.mycompany.app.Legacy*

This is from: https://blog.jdriven.com/2017/10/run-one-or-exclude-one-test-with-maven/

Irtaza
  • 3,142
  • 1
  • 9
  • 8
1

The simplest way of skipping the compilation and execution of test by default in Maven is to add the following property in your pom.xml:

 <properties>
    <maven.test.skip>true</maven.test.skip>
 </properties>

You still can change the behavior by override the property from the command-line:

-Dmaven.test.skip=false

Or by activating a profile:

<profiles>
    <profile>
        <id>testing-enabled</id>
        <properties>
           <maven.test.skip>false</maven.test.skip>
        </properties>
    </profile>
</profiles> 
Ricardo Veguilla
  • 3,107
  • 1
  • 18
  • 17