HomeJavaMaven – Understanding maven build life cycle
Maven – Understanding maven build life cycle
August 6, 2020
maven is a build automation tool, mainly used for Java projects. Through this article, we are going to discuss on; the core features of Maven (Phases, Plugins and Goals); how maven can be used as a part of a Java Web Application, and finally create a Spring Boot Application using Maven Build Tool.
Let’s start with Maven introduction
maven is a build automation tool, primarily to be used in Java web applications. This build tool relies on a Build Life Cycle mechanism that helps to:
Manage dependencies or artifacts.
Manage JAR files.
Maven Build Life Cycle contains 7 core phases which are defined in the graphic below.
validate build phase validates, if all necessary information of the project is available; this also makes sure to downloads the dependencies.
compile build phase compiles the source code of the project. By executing the command mvn compile from the root folder of the project, maven will executes all the previous build phases up to compile phase; in this case, it will execute first validate build phase as shown in the following graphic.
test build phase tests the compiled source code using an appropriate test framework tool. By executing the command mvn test from the root folder of the project, maven will executes all the previous build phases up to test phase; in this case, it will execute first validate and compile build phases as shown in the following graphic.
package build phase uses the packaging type defined in pom.xml file to package the project. By default it uses jar packaging type, if no packaging type is declared in pom.xml file. By executing the command mvn package from the root folder of the project, maven will executes all the previous build phases up to package phase; in this case, it will execute first validate, compile and test build phases as shown in the following graphic.
verify build phase verifies the integration test results. By executing the command mvn verify from the root folder of the project, maven will executes all the previous build phases up to verify phase; in this case, it will execute first validate, compile, test and package build phases as shown in the following graphic.
install build phase installs the packaged project in the local repository located by default in ~/.m2 folder. Executing the command mvn install from the root folder of the project, maven will executes all the previous build phases up to install phase, in this case, it will execute first validate, compile, test, package and verify build phases as shown in the following graphic.
deploy build phase send a copy of the packaged project to the remote repository in order to share the project with other developers. By executing the command mvn deploy from the root folder of the project, maven will executes all the previous build phases up to deploy phase; in this case, it will execute first validate, compile, test, package, verify and install build phases as shown in the following graphic.
What’s the use of clean in Maven Build Life Cycle?
Running mvn clean [phase] command, means that maven will first clean up the target folder from all the compiled java classes + jar files, then it will start executing all earlier build phases up to the specified build phase.
Plugins and Goals
In the default Build Life cycle, each build phase is binded to a specific plugin. This plugin executes one or more goals in the default build life cycle. The following table shows the core plugins of the default build life-cycle.
jar:jar or war:war
Running mvn install, automatically triggers the default plugin which is install, which it will execute the install goal.
We’ll use plugins on the next section of how to create a Spring Boot Application using Maven?
How maven can be used as a part of a Java Web Application?
maven depends on the pom.xml file to build a Java web application. This pom.xml file is located at the root of the project and describes the information about the project and it’s configuration (compiling java source code + packaging the project + downloading dependencies or artifacts).
The following graphic resumes that.
How to create a Spring Boot Application using maven?
First, we need to install the following prerequisites:
Then we’re going to generate a Spring Boot project using start.spring.io platform.
As shown in the capture above, I selected Java as language and maven as build tool for the project. Also, I defined 2.3.2 as a version of the project, after that, I entered base information about the project.
group: defines the network name of the project
artifact: defines the name of the project
packaging: defines the packaging type selected (jar or war)
version: defines the java version to program with
finally, I added some dependencies to help us building a Spring Boot Project like:
spring web: let us implement web services and call them after that.
spring data jpa: let us perform all CRUD operations on tables.
spring data mongodb: dependency that will help us use MongoDB.
After unzipping the generated project, the structure of the project will look like the following.
src/ folder will contain all java classes.
resources/ folder will contain a configuration file for the project called application.properties.
test/ folder will contain java unit testing classes.
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.