[maven] Maven skip tests

I am using Maven 2.2.1 and to build my project I used this command

mvn clean install -Dmaven.test.skip=true

However, the build failed saying it couldn't find one of the artifact. However, when I used:

mvn clean install -DskipTests

everything worked fine.

So far I have been thinking that these 2 commands are equivalent. However, this link seems to suggest that -Dmaven.test.skip=true also skips compiling the test cases.

However, that still didn't explain to me why one command is working and another is not. Will be thankful if anyone please explain this to me.

This question is related to maven

The answer is


During maven compilation you can skip test execution by adding following plugin in pom.xml

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.20.1</version>
    <configuration>
         <skipTests>true</skipTests>
    </configuration>
</plugin>

I have another approach for Intellij users, and it is working very fine for me:

  1. Click on the "Skip Test" button

enter image description here

  1. Hold the "CTRL" button
  2. Select "clean" and "install"

enter image description here

  1. Click on the "Run" button in the maven pannel

enter image description here


The parameter -DskipTests may not work depending on your surefire-plugin version.

You can use "-Dmaven.test.skip.exec" instead of "-DskipTests"

Source: Surefire Parameter Details


There is a difference between each parameter.

  • The -DskipTests skip running tests phase, it means at the end of this process you will have your tests compiled.

  • The -Dmaven.test.skip=true skip compiling and running tests phase.

As the parameter -Dmaven.test.skip=true skip compiling you don't have the tests artifact.

For more information just read the surfire documentation: http://maven.apache.org/plugins-archives/maven-surefire-plugin-2.12.4/examples/skipping-test.html


I can give you an example which results with the same problem, but it may not give you an answer to your question. (Additionally, in this example, I'm using my Maven 3 knowledge, which may not apply for Maven 2.)

In a multi-module maven project (contains modules A and B, where B depends on A), you can add also a test dependency on A from B.

This dependency may look as follows:

<dependency>
     <groupId>com.foo</groupId>
     <artifactId>A</artifactId>
     <type>test-jar</type> <!-- I'm not sure if there is such a thing in Maven 2, but there is definitely a way to achieve such dependency in Maven 2. -->
     <scope>test</scope>
</dependency>

(for more information refer to https://maven.apache.org/guides/mini/guide-attached-tests.html)
Note that the project A produces secondary artifact with a classifier tests where the test classes and test resources are located.

If you build your project with -Dmaven.test.skip=true, you will get a dependency resolution error as long as the test artifact wasn't found in your local repo or external repositories. The reason is that the tests classes were neither compiled nor the tests artifact was produced.
However, if you run your build with -DskipTests your tests artifact will be produced (though the tests won't run) and the dependency will be resolved.


As you noted, -Dmaven.test.skip=true skips compiling the tests. More to the point, it skips building the test artifacts. A common practice for large projects is to have testing utilities and base classes shared among modules in the same project.

This is accomplished by having a module require a test-jar of a previously built module:

<dependency>
  <groupId>org.myproject.mygroup</groupId>
  <artifactId>common</artifactId>
  <version>1.0</version>
  <type>test-jar</type>
  <scope>test</scope>
</dependency>

If -Dmaven.test.skip=true (or simply -Dmaven.test.skip) is specified, the test-jars aren't built, and any module that relies on them will fail its build.

In contrast, when you use -DskipTests, Maven does not run the tests, but it does compile them and build the test-jar, making it available for the subsequent modules.


To skip the test case during maven clean install i used -DskipTests paramater in following command

mvn clean install -DskipTests

into terminal window


I had some inter-dependency with the tests in order to build the package.

The following command manage to override the need for the test artifact in order to complete the goal:

mvn -DskipTests=true  package