In our previous post, we learned how to create and run a maven project via the command line itself, and in this post, we will discuss the Maven goals, plugins, lifecycle, and phases in detail.
In our previous post, we ran the below commands:
mvn archetype:generate -DgroupId=org.website.codekru -DartifactId=DemoProject -DpackageName=org.website.codekru -Dversion=1.0-SNAPSHOT -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
The first command was a single plugin goal, while the second one was a lifecycle phase.
Maven Plugins and Goals
To execute a single plugin goal, we have used the command mvn archetype:generate, where archetype is a plugin’s identifier and generate a goal’s identifier. A maven plugin is a collection of one or more goals like the Compiler plugin, which contains goals for compiling the source code, and the surefire plugin, which includes goals for executing the unit tests and generating reports.
The below command will let us know about the goals present within a plugin.
mvn pluginName:help
Where pluginName is the name of the plugin. Like if we want to know the goals contained within the compiler plugin, then we can use the below command:
mvn compiler:help
And this would print the below output on the terminal.
So, the compiler plugin has three goals, out of which the compiler:help displays information about the compiler plugin. Hence, we can say that the compiler plugin has effectively only two goals ( compile and testCompile ).
Maven Goals
A goal is a “unit of work” in Maven. A goal is a specific task that may be executed as a standalone or with other goals as part of a larger build. Examples of goals include the compiler plugin’s compile goal, the install plugin’s install goal, etc.
We can use the below command format to run the specific goal of a plugin:
mvn plugin:goal
So, to execute compile goal of the compiler plugin, we would be writing the mvn compiler:compile, and to execute the install goal of the install plugin, we would be writing the mvn install:install.
Now you can understand why we wrote mvn archetype:generate at the beginning of our post, which created a project for us. We were trying to run the “generate” goal of the “archetype” plugin.
Maven Lifecycle and Phases
The second command that we wrote at the beginning of our post was “mvn clean install“. This command isn’t specifying any plugin goal. Instead, it defines a Maven lifecycle phase. A phase is a step in what Maven calls the “build lifecycle”. The build lifecycle is an ordered sequence of phases involved in building a project.
Maven supports three built-in build lifecycles:
- Default lifecycle: It is responsible for the project’s build and deployment
- Clean lifecycle: It is responsible for cleaning the project and removing files generated by the previous build
- Site lifecycle: It is responsible for handling the creation of the project site’s documentation
The most commonly used lifecycle is the default lifecycle which begins with a phase to validate the project’s integrity and ends with a phase that involves deploying a project to production.
Default lifecycle
Below is the list of some of phases of the default lifecycle:
- validate – It checks whether the necessary information is available
- compile – It compiles the source code of the project to an output directory
- test – It will run unit test cases
- package – It will take the compiled source code and package it in its distributable format, such as a JAR.
- verify – It will run any checks on results of integration tests
- install – It will install the package into the local repository
- deploy – It copies the final package to the remote repository for sharing with other developers and projects.
There are other phases of the default lifecycle. Please check out the complete list on this link.
The phases will always be executed in order. So, when we write “mvn install”, it runs all phases up to the install phase.
Now, we have talked about goals and phases but not how they are connected?
Plugin goals can be attached to a lifecycle phase. Each phase may have zero or more goals attached to it. You can look at the output after running the “mvn install” command, and you can see that there would be multiple goals that are executed.
Here, the below goals executed after executing the “mvn install” command:
- resources:resources
- compiler:compile
- resources:testResources
- compiler:testCompile
- surefire:test
- jar:jar
- install:install
So, if we want to achieve the same result as that of the “mvn install” command without mentioning the phase ( install is a phase ), then we can also write the goals directly:
mvn resources:resources compiler:compile resources:testResources compiler:testCompile surefire:test jar:jar install:install
This would return the same result as the “mvn install” command.
Clean lifecycle
It has three phases:
- pre-clean: It executes processes that are needed before the actual project cleaning
- clean: It removes all files generated by the previous build
- post-clean: It executes processes required to finalize the project cleaning
Site lifecycle
It contains four phases:
- pre-site: It executes the process needed before actual project site generation
- site: It generates the project’s site documentation
- post-site: It executes the process required to finalize the site documentation and prepare for site deployment
- site-deploy: It deploys the generated site documentation to the specified web server
If you want to learn more about the maven build lifecycle, you can go to this link where we have written about the maven lifecycle in quite a detail.
We hope that you have liked the article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at admin@codekru.com.