Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Last Updated: Mar 27, 2024
Difficulty: Medium

Maven Failsafe Plugin

Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction

Maven is a project management tool. It helps in building, documenting, and managing a project. It is used to manage Java projects. 

It is based on Project Object Model (POM). A Project Object Model is an XML file containing information about the project. The POM holds information about various configurations, details about the plugins, and the goals of a project.

In Maven, a Plugin is a reusable code that can be used to compile code, test code, create jar/war files and create documentation of the code. A failsafe plugin is used for integration testing. In Integration testing, we test the interactions between the various components. 

Maven Failsafe Plugin

In this article, we will discuss what is a failsafe plugin in Maven and how to use it. 

Failsafe Plugin

Before diving into the failsafe plugin, let’s discuss some things about maven that will help you understand the failsafe plugin better. We will discuss the Build lifecycle in Maven, followed by what a plugin is. 

What is Maven Build Cycle?

Before discussing the failsafe plugin, let’s discuss the Build Lifecycle in Maven. 

▶️ There are three build lifecycles in Maven - defaultclean, and site

▶️ These lifecycles have phases. Each phase is responsible for a task. A Phase is made up of a sequence of goals. 

▶️ A Goal is a specific task (finer than the phase). A Goal can be attached to zero or more phases. 

▶️ A group of goals (the goals might be related to different phases) is called a Plugin.

What is a Plugin?

As we have mentioned earlier in this article, a Plugin is a reusable piece of code that can be used to reuse common build logic across different platforms. 

Example - The clean plugin will allow you to clear the target directory, thus cleaning up the earlier build artifacts. You can run the command mvn clean to execute the Clean Plugin. 

We hope you have understood a Phase, a Goal, and a Plugin in Maven. Let’s discuss the failsafe plugin now!

What is the failsafe plugin?

A failsafe plugin is used for integration testing. Integration testing is the process of checking the interactions between the different components of the project.

There are Four phases of the failsafe plugin - 

Phase Name

Description

pre-integration-test 

This phase is for setting up the environment for integration testing.

integration-test

This phase is for running the tests. 

post-integration-test

This phase is for tearing down the integration test environment.

verify

This phase is for checking the results of the tests.

There are only two Goals in the failsafe plugin - 

Goal

Description

failsafe:integration-test

It runs the integration test of the environment. 

failsafe:verify

It verifies that the integration tests have passed. 

The advantage of the failsafe plugin is that if the integration-test phase fails, the build does NOT fail; it runs the next phase i.e., the post-integration-test. The post-integration-test phase tears down the environment safely. Thus, the failsafe plugin takes care of the cleanup. The failed tests are reported during the verify phase only. 

How to use the failsafe plugin in your project?

Add the following configurations to your POM.xml file to use the failsafe plugin. 

<project>

  [...]
  
  <build>
  
    <plugins>
    
      <plugin>
      
        <groupId>org.apache.maven.plugins</groupId>
        
        <artifactId>maven-failsafe-plugin</artifactId>
        
        <version>2.22.0</version>
        
        <executions>
        
          <execution>
          
            <goals>
            
              <goal>integration-test</goal>
              
              <goal>verify</goal>
              
            </goals>
            
          </execution>
          
        </executions>
        
      </plugin>
      
    </plugins>
    
  </build>
  
  [...]
  
</project>

 

The failsafe plugin can be invoked by running the following command - 

mvn verify

 

NOTE: To run the integration tests, DO NOT invoke the phases integration-test, pre-integration-test and post-integration-test directly, only invoke verify phase. Invoking any phase other than verify will leave a jetty container (a jetty container is a java web server or a java servlet) running. 

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Frequently Asked Questions

What is the difference between the failsafe and the surefire plugin?

The Failsafe plugin is an integration testing plugin, while the Surefire plugin is used to run unit tests. The Failsafe plugin is safer than the Surefire plugin as it performs cleaning operations in case the build fails. 

What is a Plugin in Maven? 

A Plugin is the heart of Maven. It is a reusable piece of code that can be used to reuse the common build logic across different platforms. A Plugin is a group of different goals. These goals might or might not be related to each other. 

What is the difference between phase and goal in Maven?

A Phase is a collection of goals that perform a task. As a phase is made up of several goals, all the goals inside that phase will run when we run a phase.  A Goal is a specific task that needs to be performed. It is a much finer task than a Phase. For example, in compiler: compile - compile is the Goal for the compiler phase of the compiler plugin. 

Conclusion

Congratulations on finishing this article. In this article, we discussed what a plugin is, its goals, and phase in Maven, followed by a discussion on the failsafe plugin. We discussed what a failsafe plugin is and how to configure and revoke it. 

Do not stop learning! We recommend you read these articles - 

🔥 Maven Eclipse Plugin

🔥 Maven Plugins

Head to the Guided Path on the Coding Ninjas Studio and upskill in Data Structures and AlgorithmsCompetitive ProgrammingSystem Design, and many more courses.

If you want to Practice top Coding Problems, attempt mock tests, or read interview experiences, head to Coding Ninjas Studio, our practice platform.

We wish you Good Luck!🎈 Please upvote our blog 🏆 and help other ninjas grow.

Topics covered
1.
Introduction
2.
Failsafe Plugin
2.1.
What is Maven Build Cycle?
2.2.
What is a Plugin?
2.3.
What is the failsafe plugin?
2.4.
How to use the failsafe plugin in your project?
3.
Frequently Asked Questions
3.1.
What is the difference between the failsafe and the surefire plugin?
3.2.
What is a Plugin in Maven? 
3.3.
What is the difference between phase and goal in Maven?
4.
Conclusion