522

I am trying to package my project. But, it automatically runs the tests previous do performing the packaging. The tests insert some content in the database. This is not what I want, I need to avoid running tests while package the application. Anybody knows how run the package with out test?

rogerdpack
  • 62,887
  • 36
  • 269
  • 388
vks
  • 6,649
  • 7
  • 36
  • 55
  • 14
    A Unit-Test should consist of the following 4 phases: Initialization, Test, Verification and Teardown. Maybe you should adjust your tests and add an according teardown/cleanup of your database or you should use a separate database instance, not packaged with your jar file, for running your tests on. – Marc-Christian Schulze Sep 17 '11 at 16:00
  • It seems that your tests are "heavy" so you don't want to execute them. But it's a bad practice. Maybe some frameworks as DBUnit may help you ? Or you can revert database changes at the end of the test? – manash Nov 27 '11 at 20:32

23 Answers23

870

Run maven with

mvn package -Dmaven.test.skip
BullyWiiPlaza
  • 17,329
  • 10
  • 113
  • 185
Giorgos Dimtsas
  • 12,019
  • 3
  • 29
  • 40
  • 2
    thanks for ur immediate reply,am running from eclipse, where i add the command -Dmaven.test.skip=true? – vks Sep 17 '11 at 15:59
  • 10
    I've never launched mvn from eclipse, but in the Run Configuration window where you configure maven's targets and profiles, there is a 'Skip Test' checkbox. Maybe that will do the trick. – Giorgos Dimtsas Sep 17 '11 at 16:06
  • 11
    according to the [documentation](http://maven.apache.org/surefire/maven-surefire-plugin/examples/skipping-test.html) that will skip compilation aswell as execution. You can use -DskipTests to just skip execution. [This post](http://ericlefevre.net/wordpress/2008/02/21/skipping-tests-with-maven/) also gives some warnings about what versions you can use each flag with – JonnyRaa Feb 26 '14 at 12:39
  • In Run Configurations window in Eclipse, there is a table with columns as 'Parameter Name' and 'Value'. Entering Parameter name as maven.test.skip and value as true works. – Anmol Gupta Mar 31 '16 at 12:00
  • Since this answer is accepted, I wanted to note a very useful variation from the answer by Chris Beach: `-Dmaven.test.skip.exec`. This will build all the same artifacts, including the test jar, without executing the tests. Otherwise the test jar will be omitted, resulting in partial packaging if that was part of your expected distribution. – Kenn Knowles Jun 29 '16 at 21:01
  • 2
    Thanks a lot, I used to do it using ```-DskipTests``` but it does not work anymore, any idea why ? – sam Aug 03 '16 at 07:21
  • 11
    Note that [Maven Surefire Plugin](http://maven.apache.org/surefire/maven-surefire-plugin/examples/skipping-test.html) and [Maven Failsafe Plugin](http://maven.apache.org/surefire/maven-failsafe-plugin/examples/skipping-test.html) documentation suggests that setting `skipTests` is preferred to setting `maven.test.skip`. – Filip Bártek Aug 23 '16 at 18:48
  • `-DskipTests` is what you will need for most of the cases. For all other info just check this [this article](https://codepills.com/2020/12/03/how-to-build-with-maven-without-running-tests/) – Andrej Buday Dec 26 '20 at 14:58
  • mvn install -DskipTests=true – Smart Coder Aug 11 '23 at 15:34
274

Just provide the command mentioned below, which will ignore executing the test cases (but will compile the test code):

mvn package -DskipTests
Jayakumar J
  • 2,849
  • 1
  • 12
  • 4
63

you can add this plugin configuration to your pom if you do not want to set command line arg:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <configuration>
    <skipTests>true</skipTests>
  </configuration>
</plugin>
smp7d
  • 4,947
  • 2
  • 26
  • 48
  • 4
    You can no longer run the tests from the command line with this configuration. – Emmanuel Bourg Jul 23 '12 at 15:11
  • 1
    @EmmanuelBourg That can be remedied by following directions in this example http://maven.apache.org/plugins/maven-surefire-plugin/examples/skipping-test.html – smp7d Sep 18 '12 at 15:42
  • 2
    corrected link http://maven.apache.org/surefire/maven-surefire-plugin/examples/skipping-tests.html – ulab Aug 10 '17 at 12:27
50

Note that -Dmaven.test.skip prevents Maven building the test-jar artifact.

If you'd like to skip tests but create artifacts as per a normal build use:

-Dmaven.test.skip.exec
Chris Beach
  • 4,302
  • 2
  • 31
  • 50
43

If you are trying this in Windows Powershell, you will get this error:

[ERROR] Unknown lifecycle phase ".test.skip=true". You must specify a valid lifecycle phase or a goal in the format...

The reason for this is, in Powershell the "-" has special meaning and it is causing problem with maven.

The solution is to prepend it with a backtick (`), like so..

mvn `-Dmaven.test.skip=true install 

Reference: http://kuniganotas.wordpress.com/2011/08/12/invalid-task-test-skiptrue-you-must-specify-a-valid-lifecycle-phase/

Rosdi Kasim
  • 24,267
  • 23
  • 130
  • 154
34
<properties>
    <maven.test.skip>true</maven.test.skip>
</properties>

is also a way to add in pom file

vimal krishna
  • 2,886
  • 28
  • 22
31

In Inllij IDEA there is an option also to skip test goal.

enter image description here

Dushmantha
  • 2,911
  • 1
  • 14
  • 21
26

You can pass the maven.test.skip flag as a JVM argument, to skip running tests when the package phase (and the previous ones in the default lifecycle) is run:

mvn package -Dmaven.test.skip=true

You can also pass the skipTests flag alone to the mvn executable. If you want to include this information in your POM, you can create a new profile where you can configure the maven-surefire-plugin to skip tests.

manash
  • 6,985
  • 12
  • 65
  • 125
Vineet Reynolds
  • 76,006
  • 17
  • 150
  • 174
24

You only have to provide

-Dmaven.test.skip

You no longer need to append =true.

svarog
  • 9,477
  • 4
  • 61
  • 77
Adriaan Koster
  • 15,870
  • 5
  • 45
  • 60
13

Answering an old and accepted question here. You can add this in your pom.xml if you want to avoid passing command line argument all the time:

  <properties>
    <skipTests>true</skipTests>
  </properties>
anubhava
  • 761,203
  • 64
  • 569
  • 643
12

You can add either -DskipTests or -Dmaven.test.skip=true to any mvn command for skipping tests. In your case it would be like below:

mvn package -DskipTests

OR

mvn package -Dmaven.test.skip=true
Sahil Chhabra
  • 10,621
  • 4
  • 63
  • 62
11

just mvn clean install -DskipTests

Ravinda Lakshan
  • 1,006
  • 14
  • 14
7

A shorthand notation to do maven build and skip tests would be :

mvn clean install -DskipTests

Sourabh Bhavsar
  • 301
  • 3
  • 3
6

Below two commands are most useful

mvn clean package -Dmaven.test.skip=true 
mvn clean package -DskipTests

Thanks

Prasenjit Mahato
  • 1,174
  • 15
  • 10
5

you can use any maven goals like package / clean install

Solution: 1 ( package Goal )

mvn package -Dmaven.test.skip
mvn package -Dmaven.test.skip=true
mvn package -DskipTests

Solution: 2 ( clean install goals )

mvn clean install -Dmaven.test.skip
mvn clean install -Dmaven.test.skip=true
mvn clean install -DskipTests

Solution: 3 ( in pom.xml file )

you can use maven-skipping-tests in pom file and no need to provide the attributes like DskipTests,Dmaven.test.skip

Pom.xml

<properties>
        <java.version>1.8</java.version>
        <maven.test.skip>true</maven.test.skip>
</properties>

Later you can use maven command : mvn clean install/mvn package

Reference: https://www.baeldung.com/maven-skipping-tests

Lova Chittumuri
  • 2,994
  • 1
  • 30
  • 33
4

Tests should always[1] run before package. If you need to turn off the tests, you're doing something wrong. In other words, you're trying to solve the wrong problem. Figure out what your problem really is, and ask that question. It sounds like it's database-related.

[1] You might skip tests when you need to quickly generate an artifact for local, development use, but in general, creating an artifact should always follow a successful test run.

Ryan Stewart
  • 126,015
  • 21
  • 180
  • 199
  • I have a similar problem: my applications contains websocket. When i run mvn clean install i end up in an endless test series. Before i force closed it the process has been going on for 3 hours and the messages printed were just about Websocket. Can you please check out my issue to see what am i doing wrong ? https://stackoverflow.com/questions/54614739/mvn-clean-install-stuck-when-doing-tests-what-am-i-missing – georgeB Feb 10 '19 at 14:03
  • Rules always have exceptions. If you don't think so, you're wrong. – Jay Sullivan Mar 25 '20 at 01:31
2

For maven package without infecting maven test:

<properties>
    <maven.test.failure.ignore>true</maven.test.failure.ignore>
</properties>
igonejack
  • 2,366
  • 20
  • 29
1

In Intellij, go to View -> Tool Windows -> choose Maven Projects. On the Lifecyle dropdown, right-click on package -> choose Create 'your-project [package]'...

Enter this value: package -Dmaven.test.skip=true -f pom.xml in the Command line field. Click Apply and a Run Configurations dropdown menu should appear along with your created custom maven command.

1

Those who don't want to skip the test cases. just above the main test class comment out or delete annotation:

//@SpringBootTest

Then when Maven builds an app, it will still run tests inside this class but will not run SpringBoot app, so will not test the connection to DB and the build will be successful.

0

mvn clean install -Dmaven.test.skip=true

worked for me since the -Dskip did not work anymore.

Petter Friberg
  • 21,252
  • 9
  • 60
  • 109
0
mvn package -Dmaven.test.skip=true
mcvkr
  • 3,209
  • 6
  • 38
  • 63
0

You are, obviously, doing it the wrong way. Testing is an important part of pre-packaging. You shouldn't ignore or skip it, but rather do it the right way. Try changing the database to which it inserts the data(like test_db). It may take a while to set it up. And to make sure this database can be used forever, you should delete all the data by the end of tests. JUnit4 has annotations which make it easy for you. Use @Before, @After @Test annotations for the right methods. You need to spend sometime on it, but it will be worth it!

Ozyman
  • 503
  • 1
  • 6
  • 16
  • 11
    I think the question as well as the desire to package without running tests is perfectly valid. I don't think the OP "obviously is doing it the wrong way". Different people and projects have different needs, and these are commonly not in alignment with the Maven bible or the beliefs of various people (like yourself) about how software development should work. I think your answer would be more appreciated without its condescending approach. – Zero3 Jun 23 '15 at 17:59
-2

The best and the easiest way to do it, in IntelliJ Idea in the window “Maven Project”, and just don’t click on the test button. I hope, I helped you. Have a good day :)