90

Is there a simple way to not build the test classes?

mvn clean install -Dmaven.test.skip=true
ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199

5 Answers5

149

According to the documentation on the Maven Surefire Plugin, -Dmaven.test.skip should skip both compilation and execution of the tests. By contrast, -DskipTests just skips the test execution: the tests are still compiled.

Daniel
  • 10,115
  • 3
  • 44
  • 62
  • 31
    +1 I hate the naming, however. It's completely non-obvious to me why `skipTests` just skips *executing* the tests and `maven.text.skip` skips *compiling and executing* the tests. I always have to look up which one is which. – Joachim Sauer Apr 07 '10 at 15:47
21

Just to be explicitly clear:

skipTests will compile anything in the <testSourceDirectory>, but will not execute them.

maven.test.skip will NOT compile any tests, but WILL execute any compiled tests that made their way into the <testOutputDirectory>.

So the behavior of the above 2 is opposite. Just wanted to point out that maven.test.skip doesn't skip compilation AND execution if test files are unpacked/copied/etc. into <testOutputDirectory>.

Also, depending on which version of Maven your using, there's also maven.test.skip.exec=true which additionally skips test execution similar to skipTests.

johntrepreneur
  • 4,514
  • 6
  • 39
  • 52
  • 3
    That is unfortunately not true, at least in maven 3.0.5 and maven-compiler-plugin 3.5.1. You can test it in every project containing tests: `mvn clean package -DskipTests=true && mvn test -Dmaven.test.skip=true` will compile the tests (because of the first package-execution) but not run them in the second command. Assuming standard directories, `target/test-classes` will contain all tests afterwards, but they have not been executed. – David Georg Reichelt Feb 12 '16 at 14:33
  • This is the correct answer and should be accepted as such – Artem Dec 01 '18 at 21:55
9

Run a phase that doesn't include test-compile, for example compile.

mvn clean compile
Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
8

I'm not an expert at maven, but what I use in my current project is:

mvn clean install -DskipTests=true

Depending on your use case using:

mvn compile -DskipTests=true

might work for you too.

JimP
  • 1,070
  • 15
  • 26
1

I found a work-around in another question, which actually overrides a plugin execution by adding the following snippet in your pom.xml by default:

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <executions>
        <execution>
          <id>default-testCompile</id>
          <phase>none</phase>
        </execution>
      </executions>
   </plugin>
  </plugins>
</build>

This seems to work but definitely does not disable phase but disables the default actions that a plugin defines at a specific phase.

Jahan Zinedine
  • 14,616
  • 5
  • 46
  • 70