331

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.

djikay
  • 10,100
  • 8
  • 42
  • 50
Prabhjot
  • 3,606
  • 2
  • 16
  • 20
  • 1
    What version of maven-surefire-plugin are you using? Is it the same as doc version you're reading? – gerrytan Jul 13 '14 at 22:50
  • 2
    One skips building, the other skips running. If you want both use both. – Elliott Frisch Jul 13 '14 at 23:00
  • 1
    Can you provide details of the failure - the error message or stacktrace? – Raghuram Jul 14 '14 at 05:20
  • 1
    Why are you using such an older Maven version which is [already defined EoL](http://maven.apache.org/maven-2.x-eol.html). – khmarbaise Jul 14 '14 at 14:31
  • 1
    So is it really true that to completely skip everything test related I have to use `-Dmaven.test.skip=true -DskipTests`? One or the other can sometimes be omitted, depending on the circumstances, but who wants to think about that.. – Landon Kuhn Oct 07 '16 at 01:28
  • 1
    @ElliottFrisch If you set `-Dmaven.test.skip=true` you prevent building and also running, since something that has not been built can not be run. – David Balažic Jan 23 '18 at 16:31
  • 1
    @DavidBalažic True, how short-sighted of my comment 3 years ago. – Elliott Frisch Jan 23 '18 at 17:20

11 Answers11

196

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.

Mureinik
  • 277,661
  • 50
  • 283
  • 320
  • 2
    How to solve this problem? As my online maven command use -Dmaven.test.skip. – neptune Mar 16 '16 at 07:18
  • 3
    @neptune what problem? If you have a new question, please use a new post to ask it. – Mureinik Mar 16 '16 at 10:40
  • The `tests` is missing at your dependency. See [Default Artifact Handlers Reference](https://maven.apache.org/ref/3.8.2/maven-core/artifact-handlers.html). – Gerold Broser Sep 19 '21 at 18:57
94

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
Lii
  • 10,777
  • 7
  • 58
  • 79
gilwo
  • 1,149
  • 7
  • 7
79

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

Akshay Hazari
  • 2,995
  • 3
  • 40
  • 76
26

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

mvn clean install -DskipTests

into terminal window

Niraj Trivedi
  • 1,794
  • 16
  • 22
19

I can give you an example which results in 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 of A in B.

This dependency in B may look as follows:

<dependency>
     <groupId>com.foo</groupId>
     <artifactId>A</artifactId>
     <classifier>tests</classifier>
     <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 3. -->
     <scope>test</scope>
</dependency>

(For more information refer to https://maven.apache.org/guides/mini/guide-attached-tests.html)

Note that project Ausually produces a secondary artifact with a classifier tests (i.e. .../com/foo/A/<version>/A-<version>-tests.jar) where the test classes and test resources are located inside.

If you build project A with -Dmaven.test.skip=true, you will get a dependency resolution error when building B unless A's test artifact is found in your local repo or remote repositories. The reason is that the test classes of A were neither compiled nor the tests artifact of A was produced.

However, if you build A with -DskipTests its tests artifact will be produced (though the tests won't run) and the dependency in B will be resolved successfully.

Gerold Broser
  • 13,129
  • 4
  • 41
  • 97
Stepan Vavra
  • 3,764
  • 4
  • 28
  • 38
16

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

vtsamis
  • 1,176
  • 1
  • 10
  • 9
  • Thanks. `mvn clean install -Dmaven.test.skip.exec` command worked for me. – Ish Sep 02 '21 at 14:21
  • [``/ `maven.test.skip.exec`](https://maven.apache.org/surefire/maven-surefire-plugin/test-mojo.html#skipExec) is "_**Deprecated**. Use skipTests instead._". – Gerold Broser Sep 18 '21 at 20:13
9

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

Mehdi Bouzidi
  • 1,897
  • 3
  • 15
  • 28
6

For the fastest build, to skip both compiling the test files and running the tests:

mvn install -Dmaven.test.skip -DskipTests

From the Maven Surefire Plugin docs:

You can also skip the tests via the command line by executing the following command:

mvn install -DskipTests

If you absolutely must, you can also use the maven.test.skip property to skip compiling the tests. maven.test.skip is honored by Surefire, Failsafe and the Compiler Plugin.

mvn install -Dmaven.test.skip=true

Even more ways to speed up installing can be found at this answer to the question "Ways to make maven build faster?".

Razzi Abuissa
  • 2,504
  • 2
  • 24
  • 24
4

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>
Rishav
  • 3,515
  • 29
  • 48
Radadiya Nikunj
  • 900
  • 9
  • 10
  • 3
    It skips tests even when you do `mvn test` – Pratik Singhal May 03 '18 at 14:25
  • I do not recommend this (apart from special environments, e.g. having production code and test code in two different projects). Maven's default, its convention, is to run (unit) tests during a build. With that you override this default behaviour for no good reason. Use the command line property instead or use a build profile (if you really must) named `skipTests` that sets the property and is activated via `mvn ... -PskipTests ...` on the cmd line or via one of the other [activation methods](https://maven.apache.org/guides/introduction/introduction-to-profiles.html). – Gerold Broser Sep 20 '21 at 10:51
3

With latest version of Maven

The way of giving command bit change.

below command will works perfectly

mvn clean install "-Dmaven.test.skip=true"
Rahul Jain
  • 99
  • 2
  • This is not "_With latest version of Maven_". This exists since v2.0 of the Maven Compiler Plugin. We're at v3.8.1 now. – Gerold Broser Sep 18 '21 at 20:03
2

I use the following for skipping both compiling and running tests (maven 3.8.1) :

mvn install  -Dmaven.test.skip.exec -Dmaven.test.skip=true
ᐅdevrimbaris
  • 660
  • 9
  • 19
  • 2
    [``/ `maven.test.skip.exec`](https://maven.apache.org/surefire/maven-surefire-plugin/test-mojo.html#skipExec) is "_**Deprecated**. Use skipTests instead._". – Gerold Broser Sep 18 '21 at 20:08
  • For my case skipTests did not work. Only the flags I have used in my answer worked. It may be because of maven version. – ᐅdevrimbaris Sep 22 '21 at 09:30