CodeSteps

Python, C, C++, C#, PowerShell, Android, Visual C++, Java ...

Maven – Understanding maven build life cycle

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

What is Maven?

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.
  • Manage plugins.

Maven phases

Maven Build Life Cycle contains 7 core phases which are defined in the graphic below.

Maven Phases in Maven Build Life Cycle
Maven Phases in Maven Build Life Cycle

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.

Maven compile build phase
Maven compile build phase

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.

Maven test build phase
Maven test build phase

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.

Maven package build phase
Maven package build phase

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.

Maven verify build phase
Maven verify build phase

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.

Maven install build phase
Maven install build phase

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.

Maven deploy build phase
Maven deploy build phase

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.

Core Plugins

phaseplugin:goal
validateresources:resources
compilecompiler:compile
testresources:testResources
packagejar:jar or war:war
installinstall:install
deploydeploy:deploy

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.

Structure of pom.xml file
Structure of pom.xml file

How to create a Spring Boot Application using maven?

First, we need to install the following prerequisites:

  • JDK11
  • Maven
  • MongoDB

Then we’re going to generate a Spring Boot project using start.spring.io platform.

Generate Sprint Boot project
Generate Sprint Boot project

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.

Project structure

Spring Boot project structure
Spring Boot project structure

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.

Maven plugin

<build>
	<plugins>
		<plugin>
			<groupId>org.springframework.boot</groupId>
			<artifactId>springframework-boot-maven-plugin</artifactId>
		</plugin>
	</plugins>
</build>			

Spring Boot adds by default to the pom.xml file the maven dependency to build the project. It uses by default all core build phases we mentioned before.

Running mvn compile command from the root folder of this project will make maven:

  • Download all necessary artifacts from maven repository and add them to the local repository which located by default in ~/.m2 folder.
  • Compile Product.java class and added the compiling output to the target/ folder.

Before checking again the project structure of Spring Boot, we’ll first have a look at the command prompt after running mvn compile command.

[INFO] - maven-resources-plugin:3.1.0:resources (default-resources) @ demo ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO]
[INFO] - maven-compiler-plugin:3.8.1:compile (default-compile) @ demo ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 2 source files to C:\Users\OMAYMA\demo\target\classes 
[INFO] -------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] -------------------------------------------------------------------------
[INFO] Total time: 09:39 min
[INFO] Finished at: 2020-07-29T16:10:51+01:00
[INFO] -------------------------------------------------------------------------

As shown in the above capture, maven execute 2 plugins:

  • maven-resources-plugin:3.1.0:resources: this plugin download all required dependencies to use for our project.
  • maven-compiler-plugin:3.8.1:compile: this plugin compiles all java classes to bytecodes and add them finally to the target/ folder.

Project structure will look the following after running mvn compile command.

Spring Boot project structure - after maven compile
Spring Boot project structure – after maven compile
Maven – Understanding maven build life cycle

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top