Build Lifecycles in Maven
Maven clearly defines the process of creating and deploying a specific project. It uses Build Lifecycles to achieve this. Every plugin is a part of some build lifecycle. So to better understand the plugins, let us first look at the different lifecycles of Maven.
Maven has mainly three build lifecycles:
-
Default: It is the most important lifecycle in Maven. It deals with the deployment of the project.
-
Site: This lifecycle handles the website development of the project.
-
Clean: clean lifecycle manages project cleaning. This clean-up includes clearing previously built projects.
All the core Maven plugins revolve around these three lifecycles. Based on these lifecycles, we divide the plugins into two main categories:
-
Build Plugins: Maven executes these plugins during the build phase. These plugins are part of the Default lifecycle. While writing the POM file, we declare these plugins under the <build> element.
Syntax:
<build>
<plugins>
<!-- plugins go here -->
</plugins>
</build>.
-
Reporting plugins: These plugins get executed during the site generation phase. These plugins are part of the site lifecycle of Maven. We declare these plugins using the <reporting> element inside the POM file.
Syntax:
<reporting>
<plugins>
<!-- plugins go here -->
</plugins>
</reporting>.
Let us take a look at some of the core Maven plugins.
Core Maven Plugins
Some of the core Maven Plugins are as follows.
-
Clean: It is a Build plugin. It cleans up the earlier created project. We can consider it as a part of the clean lifecycle as well.
-
Compiler: This plugin compiles the java files and codes. It is also a Build plugin.
-
Deploy: This Build plugin deploys the project to a remote repository.
-
Javadoc: It is a Reporting plugin that generates the java documentation of the project.
-
Changes: This Reporting plugin tracks changes made in the document.
-
Install: This plugin installs the built artifacts into the local repository of the system.
-
Site: This Build type of plugin creates a website for the current project.
-
War: It creates a war file of the project. Similarly, the jar plugin builds the jar file of the project.
-
antrun: This plugin is a unique build tool that runs ant tasks from any build phase.
-
Surefire: This Build plugin runs JUnit unit tests in an isolated classloader. JUnit is a Java unit testing tool.
-
Pdf: This is a handy plugin tool that creates a pdf of the project documentation.
Please note that we create all these plugins inside the POM.xml file. POM stands for Project Object Model. It is nothing more than an XML file that stores information about the project and its configuration.
We will now discuss some valuable facts and key concepts about plugins.
Some Key Concepts
-
As stated earlier, we define plugins inside the POM.xml file using the <plugins> element.
-
We can set multiple goals for each plugin. It is possible to assign a different set of tasks to each goal. We bound the tasks to each goal using the <tasks> element.
-
We use the <phase> element to tell the plugin from which phase it should start its execution.
-
Sometimes, the required plugin is absent on the local system but in some remote repositories. In this case, Maven first installs that plugin and then executes it.
Congratulations Ninja! You have successfully understood one of the essential parts of Maven. You have now learned how to build plugins. You also possess knowledge of various plugins and their usage.
Frequently Asked Questions
What do you understand by the Maven Build cycle?
A Maven build cycle decides the order of the execution of tasks. We divide each life cycle into different phases. We follow a sequence while running these phases.
There are three Build lifecycles in Maven clean, default and site.
How to determine the version of Maven installed in our system?
We can quickly determine the Maven version we use in our system by typing the following command in the console: mvn -- version.
What is a Maven repository?
A Maven repository stores all the Maven projects, library jars, documentation and plugins in a single place. Maven can easily access this stored data. There are three types of repositories in Maven, central, remote, and local.
Conclusion
Hey Ninja! We hope you enjoyed reading this article. We started by understanding Maven plugins. We then discussed the uses and benefits of plugins in Maven. We discussed the various types of plugins and how they are divided based on build lifecycles. We then learned some key concepts and facts about Maven plugins.
We hope the above discussion helped you understand Maven plugins and their importance in Java project development. If you wish to learn more about Maven and its various components, you can refer to blogs on maven setup, configuration, and interview questions.
Do visit our website to read more such blogs. Make sure you enroll in our courses. You can take mock tests, solve problems, and interview puzzles. Also, you can check out some exciting interview stuff- interview experiences and an interview bundle for placement preparations. Do upvote our blog to help fellow ninjas grow.
Keep Grinding! 🦾
Happy Coding! 💻