Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
All apps of any size must go through a set of protocols, such as creating and compiling source code. Developers can manually configure these processes, but it takes time.
Apache group offers Maven, a planning and analysis tool, to solve this problem. It completely automates the process and streamlines the daily tasks of developers. Developers have access to an entire build lifecycle framework with Maven. In this article, we will discuss the maven build lifecycle.
Build Lifecycle
The build lifecycle in Maven refers to the set of phases that must be followed to complete the objectives and tasks of each Maven project. In this case, phase denotes a stage in the life cycle.
The build life cycle is primarily the focus of the Maven 2.0 version. It explicitly states that these phases must be well-defined to produce the intended results when the build lifecycle has been completed.
Maven includes three built-in build lifecycles:
Default: The deployment of your project is handled by the default lifecycle.
Clean: The clean life cycle handles your project's cleanup.
Site: The setup of your project's website is handled by the site lifecycle.
We will discuss each built-in build lifecycle one by one.
Default Lifecycle
A default build lifecycle is made up of the following phases.
Prepare Resources:It takes care of copying the required resources. In this stage, resource copying can be altered.
Validate: It verifies that the project is accurate and that the relevant data is accessible to complete the build process.
Compile: After creating the source code, it compiles the project's source code.
Test: Utilize a suitable unit testing framework to run tests (Junit is one).
Package: Take the compiled code and package it in a file type that can be distributed, like a JAR, WAR, or EAR.
Verify: Verify the package is safe and meets quality standards by running any checks.
Install: Install the package to be used as a dependency in nearby projects by adding it to the local repository.
Deploy: It copies the completed package to the remote repository so that other projects and developers can use it.
Every phase has a pre and post-phase that must run before or after it to register goals. For example, the pre-test phase, post-test phase, generate resources, process resources etc.
Clean Lifecycle
The project is cleaned up during this step to prepare for a new compilation and deployment. The command is as follows:
mvn clean
When this command is used, maven internally executes the following commands to complete the tasks listed below:
Pre-Clean: It completes the steps required before the actual project cleaning.
Clean: It gradually delete all files produced by the previous build.
Post-clean: It conducts the steps required to complete the project's cleanup.
When this maven's clean goal is executed, all compiled files are removed from the output directory (target folder).
Site Lifecycle
Maven features a phase that performs additional tasks beyond cleaning, compiling the source code, and creating a deployable format for the app. One of the crucial features offered by Maven is the Site Lifecycle phase, which creates thorough documentation for any Java project. The command is as follows:
mvn site
The site Lifecycle has specific phases, shown below:
Pre-Site: It executes the actions that need to be completed before the entire project site generation.
Site: It generates the project's site documentation.
Post-site: It performs post-site steps to complete site generation and set up site deployment.
Site Deploy: It distributes the site's documentation to the selected web server.
Doxia Document
As mentioned above, 'mvn site' is the maven command to produce Javadoc for a specific project. Maven calls the "Doxia" document generation plugin and other report-generating plugins when this command is issued.
Doxia is a framework that Maven uses to create docs. This accomplishes both static and dynamic documentation generation. Maven heavily relies on Doxia, which runs the project's complete docs system.
For each project, a POM(Project Object Model) is created.
Maven's basic unit of work is called a POM. It is an XML file with details about the project's config that Maven will use to build the project.
Executing Lifecycle
Maven follows the lifecycle steps according to the selected build profile.
Step 1: Read the POM.xml file.
Step 2: After reading the file, download all the required dependencies into the local repository.
Step 3: Execute the build lifecycle.
Step 4: Execute the plugins.
Note
Maven runs the phases up to and including the invoked phase whenever a command for any life cycle is issued.
For example, when the "mvn test" is used, Maven runs the test phase. However, no package, deployment, or site phase has been invoked.
Basic Maven Commands
We will discuss some basic commands related to the Maven build lifecycle.
→ mvn clean: Trashes all files produced by the previous build and thoroughly cleans the project.
→ mvn compile: Compiles the project's source code.
→ mvn deploy: After compiling, running tests, and building the project, it copies the packaged JAR/WAR file to the remote repository.
→ mvn install: Install the packaged JAR or WAR file to the local repository using the mvn command.
→ mvn package: Creates a JAR or WAR file for the project to make it into a distributable format. → mvn test: Runs tests for the project with mvn test.
→ mvn test-compile: Compiles the test source code using mvn test-compile.
Frequently Asked Questions
What is the Maven Build Lifecycle?
The build lifecycle in Maven refers to the set of phases that must be followed to complete the objectives and tasks of each Maven project. In this case, phase denotes a stage in the life cycle.
How many Maven Build LifeCycles are there?
There are three maven-build lifecycles. These are default, clean and site lifecycles. The default lifecycle handles the deployment of your project. The clean lifecycle handles your project's cleanup. The site lifecycle handles the setup of your project's website.
Which Build Lifecycle is responsible for the documentation part?
The Maven Site Lifecycle creates thorough documentation for any Java project. The 'mvn site' is the maven command to produce Javadoc for a specific project. Maven calls the "Doxia" document generation plugin and other report-generating plugins when this command is issued.
Conclusion
We discussed the Maven build lifecycle. We discussed the types of build in the Maven build Lifecycle: the default, clean and site build lifecycle. Further, we discussed how to execute the Build Lifecycle. We also covered some basic commands of the Maven build lifecycle.
We hope this blog has helped you. We recommend you visit our articles on different topics of Maven, such as