EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Software Development Software Development Tutorials Maven Tutorial Maven Skip Test
 

Maven Skip Test

Payal Udhani
Article byPayal Udhani
EDUCBA
Reviewed byRavi Rathore

Updated April 12, 2023

Maven Skip Test

 

 

Introduction to Maven Skip Test

Maven build lifecycle provides us to perform various actions on our project that are necessary such as verify to make sure everything is specified correctly in the project, further the project is cleaned, packaged, tested, and deployed. Testing is an important part of the build process that makes sure that the application or our project is written in the maven structure works correctly for the multiple test cases and should not be skipped as it is considered bad practice to deploy the project without testing. However, there are some situations in which we need to skip the test operation. For example, while working on the new module and we want to run the builds that are not passing or compiling to check them on an intermediate basis. In these cases, we can skip the testing to avoid overhead on the system that occurs when we compile and run the tests. In this article, we will learn about various methodologies that can be used to skip the test in the maven project.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

How Testing works in Maven

Let us know how testing works in maven. The Maven build lifecycle can ignore, compile, or run the tests. There are various plugins that can be used for testing such as failsafe plugin, surefire plugin, and many others. Out of them skipping the testing is supported by compiler plugin, failsafe plugin, and surefire plugin. In this article, we will execute the package phase that compiles and runs the test and will use the surefire plugin in our examples.

Various ways to Skip the Test in Maven

There are various ways using which we can skip the testing in maven.

  • skip tests element property can be set inside the configurations of your plugin tag.
  • Through command line using -DskipTests argument in your maven execution command.
  • By using the maven.test.skip property while firing the maven command for executing the phase.
  • Skipping by default by initializing the skipTests element value to true in properties and then overriding it through the command line by mentioning the value of -DskipTests in the maven phase/build execution command.
  • Excluding some of the test classes by using the exclude element with the name of the class to be excluded in your pom.xml’s plugin tag while using surefire.

Let us study all the above methods one by one:

1. skip tests element in pom

If you wish to skip the test for a certain project, you can specify the skipTests property to true in your plugin tag of the project’s pom.xml file in the following way:

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

that is placed inside the plugins element of build tag in your project element of the pom.

2. DskipTests argument

You can skip the test for any project while executing the maven command from command prompt for that particular project’s build lifecycle’s phase by specifying the argument -DskipTests. The default value of -DskipTests argument is considered as true when used without assigning the value. Suppose, I am executing the package command for the current maven project by using mvn package then instead of the above command, I will fire the following command to be executed in order to skip the test of that maven project.

3. maven.test.skip property

If you have to completely skip the test compilation then you can even make the use of the maven-test-skip property in your command of maven. This property is supported by most of the testing plugins including failsafe and surefire and even the compiler plugin of maven. The above property can be used in following ways while executing the maven command from the command line:

Command:

mvn package -Dmaven.test.skip=true
Note: It is necessary to specify the value of this argument to true to skip the test compilation.

4. Skipping by default and then overriding it when necessary

There is often a need while developing the application to skip the test by default and execute it sometimes when there is a necessity. As the development involves too many changes and running the command for every change will lead to large overhead of compiling and running the test if the test is not skipped in maven projects.

In these situations, we can mention the default behavior of the maven project to skip the test for each maven command fired. This is specified inside the pom.xml file by initializing a property variable to true and assigning its value to the skip Tests element. Whenever there is a necessity to perform the test, we can override the value of this property by specifying the property value in our command to be executed for maven on the command line. This can be done in the following way –

Firstly, pom.xml needs to include the property that is set to true, in our case defaultValueOfSkip is the property whose value is set to true and then this property is assigned to the skip Tests element of the configuration of surefire plugin element. Hence, whenever We fire the mvn package or any other maven phase command such as mvn install the test will be skipped by default.

Code:

<project>
<properties>
<defaultValueOfSkip>true</defaultValueOfSkip>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M4</version>
<configuration>
<skipTests>${defaultValueOfSkip}</skipTests>
</configuration>
</plugin>
</plugins>
</build>
</project>

Now, suppose, in the middle of application development, I feel a necessity to test the project, then I can do so, by simply setting the value of the property defaultValueOfSkip to false in my command in the following way:

Command:

mvn package -DdefaultValueOfSkip=false

OR

mvn install -DdefaultValueOfSkip=false

and likewise for all other phases occurring after a test phase in a sequence of order of phases.

5. Excluding some of the test classes

While using the surefire plugin, we can skip the test for certain classes by specifying them inside the excluded element of the pom file.

Code:

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M4</version>
<configuration>
<excludes>
<exclude>SampleTest.java</exclude>
</excludes>
</configuration>
</plugin>

This will lead to the exclusion of the SampleTest file for unit testing even when it contains the test word in its name because it was mentioned in exclude the element of configuration element in the pom.xml file.

Conclusion

We can skip the test in maven projects in various ways by using command-line commands or pom.xml file by using the above-mentioned ways. However, we should make sure that the test is skipped only when some specific purpose is there as it is a bad practice to deploy the project without testing.

Recommended Articles

We hope that this EDUCBA information on “Maven Skip Test” was beneficial to you. You can view EDUCBA’s recommended articles for more information.

  1. Java Deployment Tools
  2. Maven Commands
  3. Maven POM File
  4. Maven Repository

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW