Maven Life Cycle: When we hear the word Life Cycle, it’s nothing but the set of steps defined to execute the tasks. Similarly, the Maven build life cycle is nothing but executing the tasks and goals of the maven project. In the life cycle process, after each step execution is successful, we get the desired output.
The Maven 2.0 version is focused on build life cycles and states that maven must follow different build life cycles to achieve the desired output after a successful build cycle.
Post On: | Maven Life Cycle |
Post Type: | Maven Tutorial |
Published On: | www.softwaretestingo.com |
Applicable For: | Freshers & Experience |
Get Updates: | Join Our Telegram Group |
Maven Life Cycle
Maven comes with three built-in build life cycle phases:
- Clean Life Cycle
- Default Life Cycle
- Site Life Cycle
You can execute more than one lifecycle at a time with maven, and each lifecycle is independent of the others. Life cycles must be executed in sequential order, as shown above. The complete maven lifecycle is defined in the ‘component.xml’ file found in the core module. Each life cycle phase has a unique goal that has to be completed during that phase. Lifecycle methods such as ‘build’ and ‘test’ execute specific tasks to achieve the goal of that particular phase.
During the Maven Life Cycle phases following tasks/goals are performed:
- Resource Preparation- On this task, maven copies of resources like configuration files to the build folder.
- Compilation- Compiling source codes.
- Packaging- Copying dependency JAR files to build folder.
- Runs unit tests.
Now let’s try to understand the different phases of each life cycle in detail.
Clean Life Cycle: Maven Life Cycle
This Clean Life Cycle is used to clean up or delete the project directory name target and its contents and prepare it for a fresh compile and deployment. The command for this operation is mvn clean.
The mvn clean command runs a series of tasks that delete temporary files created during the build process. These commands like mvn pre-clean and mvn post-clean commands are running internally when you execute the mvn clean command.
- mvn pre-clean
- mvn clean
- mvn post-clean
When we are executing the mvn command, then maven will execute the current clean command and also execute its previous clean process.
- When you execute that time, maven will execute the mvn pre-clean and mvn clean command.
- mvn pre-clean: It will execute the pre-clean only.
- mvn clean: it will execute both commands’ pre-clean and mvn clean.
- mvn post-clean: This will execute pre-clean, clean, and post-clean altogether.
Default Life Cycle: Maven Life Cycle
This is the most typical and primary lifecycle of a maven. It starts with the validate phase, which checks that the Project is correct and all necessary information has been provided and ends with the deploy phase.
Phase | Description |
---|---|
validate | It validates whether the Project is correct or not, and all necessary information is available to complete a build |
initialize | It initializes the build state, for example, set properties or creating directories |
generate-sources | It generates any source code for inclusion in the compilation |
process-sources | It processes the source code, for example, to filter any values |
generate-resources | It generates the resources for inclusion in the package. |
process-resources | It copies and processes the resources into the destination directory, ready for packaging. |
compile | It compiles the source code of the Project. |
process-classes | For example, it post-processes the generated files from compilation to do bytecode enhancement on Java classes. |
generate-test-sources | It generates any test source code for inclusion in compilation. |
process-test-sources | It processes the test source code, for example, to filter any values. |
generate-test-resources | It creates the resources for testing. |
process-test-resources | It copies and processes the resources into the test destination directory. |
test-compile | It compiles the test source code into the test destination directory |
process-test-classes | For example, it post-processes the generated files from test compilation to do bytecode enhancement on Java classes. |
test | It runs tests using a suitable unit testing framework. These tests should not require the code to be packaged or deployed. |
prepare-package | It performs any operations necessary to prepare a package before the actual packaging. This often results in an unpacked, processed version of the package. |
package | It packages the compiled code in its distributable format, such as a JAR. |
pre-integration-test | It performs actions required before integration tests are executed. This may involve things such as setting up the required environment. |
integration-test | It processes and deploys the package if necessary into an environment where integration tests can be run. |
post-integration-test | It performs actions required after integration tests have been executed. This may include cleaning up the environment. |
verify | It runs any checks to verify the package is valid and meets quality criteria. |
install | It installs the package into the local repository for use as a dependency in other projects locally. |
deploy | It is done in an integration or release environment and copies the final package to the remote repository for sharing with other developers and projects. |
Note: If you execute the “mvn install” command, it will run all phases up to the install phase.
Each phase can be linked with zero or more plugin goals, and executing a particular phase is the same as running that phase’s plugin goals. For example, executing the install phase is like running the “install: install” goal.
But if you run the “mvn install” and “mvn install: install” commands, they’re not equivalent because “mvn install” will also execute all of the phases up to the install phase. So maven would also execute every goal associated with each prior phase–whereas “mvn install: install” will only execute this specific goal.
Here is the list of some most commonly used phased and attached goals.
Phase | plugin: goal |
---|---|
process-resources | resources: resources |
compile | compiler: compile |
process-test-resources | resources:testResources |
test-compile | compiler:testCompile |
test | surefire: test |
package | ejb:ejb or ejb3:ejb3 or jar:jar or par:par or rar:rar or war:war |
install | install: install |
deploy | deploy: deploy |
Site Life Cycle: Maven Life Cycle
We can clean, compile the source code with the maven build automation tool, and build a deployable application format. Apart from this, maven has a Site phase that can do more than the Clean and Default phase.
That phase is called the site phase, and this phase has one of the crucial features of a maven, which is generating detailed documentation of the Project and also preparing reports about the Project.
For Project documentation and site generation, in maven, we have a specific lifecycle dedicated for that, which has four phases and that are:
- pre-site: Executes processes before the project site generation
- site: Generates the Project’s site documentation
- post-site: Executes processes to finalize the site generation
- site-deploy: Deploys the generated site to the web server
There are also plugin goals attached to the site life cycle phase, which are:
- site ( site: site)
- site-deploy ( site: deploy )
When you execute this command “mvn site,” it will generate the Javadocs for the given Project. When you call this command, it is called “Doxia” document generation and other reporting plugins.
Conclusion:
We hope that you have liked the article. If you have any doubts or concerns, please feel free to write us in the comments section.