What- Why - How Maven

WHAT is MAVEN


Introduction: Maven is the build management tool for Java projects written in XML language. pom.xml file in the application has the maven build information. This file gives the entire information about the project and its dependencies. 

 

WHY to use MAVEN


Maven makes the developers work easy while creating reports, checks, build and test automation steps.

 

Convention over Configuration: Maven helps the developers by providing a default build process which is termed as convention over configuration. 


Maven creates default project structure. For the project structure, developer doesn't have to define anything in pom.xml. Default project has src with main and test folders,  target folders. src folder has the java class files. test folder has the test class files. resource folder has the configuration details of the project.



 

HOW to use MAVEN


If an IDE is used, it provides a default maven with it. For all the projects in this blog, I'm currently using the default maven that I got with intelliJ IDE

The pom file is the main file to configure project details in maven. When a project is created, this is how it looks


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.test</groupId>
<artifactId>helloworld</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>helloworld</name>
<description>Demo project for Spring Boot</description>

<properties>
<java.version>11</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

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

</project>


 

This file has the model version of the maven used and the groupId, artifactId and the version of the project. It has the basic details like the java version that is used and all the dependencies that are needed for this project. It also has plugins and profiles information too.

 

Everything in maven works on build life cycles and phases. Maven command can have the combination of build life cycle and phase.

 

Maven build life cyclesdefault cycle handles the project deployment. clean cycle handles project cleaning. site handles project documentation which is usually stored in target folder.

 

Phases:

 

1. validate - validates if the project is correct and has all the necessary information.

2. compile - compile the source code of the project

3. test - test the compiled source code

4. package - takes the compiled code and package it in either JAR or WAR. This can be specified in pom file.

5. verify - runs all the integration tests to ensure quality is met.

6. install - install the package into local repository to use the project as a dependency in other projects.

7. deploy - this is done in build environment

 

Example maven commands with build life cycle and phase combinations and their usage:

 

mvn clean package - a maven command with clean build cycle and package phase


This command cleans the target folder and performs all the phases before package such as validating of the code, compiling the code, testing the compiled code and then taking the compiled code and creating a JAR or WAR file out of it and saving it in target folder.


mvn package - a maven command with default build cycle and package phase


This command takes doesn't clean the target folder as it did in the above command. It performs all the phases until package and creates a JAR or WAR file and saves it to target folder.


mvn install - a maven command with install phase


This command performs all the operations of phases until install.  It then creates a jar/war file for the project and stores it in your local repository for maven.

Usually it is in location: /.m2/repository/com/test/helloworld/0.0.1-SNAPSHOT/helloworld-0.0.1-SNAPSHOT.jar in your PC.


This jar can be used as a dependency in other projects as below:


<dependency>
<groupId>com.test</groupId>
<artifactId>helloworld</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>


mvn site - a maven command to create a documentation file for the project and store it in target folder


Tip: When a maven project is newly downloaded locally or when any changes are made, developers usually perform mvn clean verify to verify if the project is building successfully passing all the integration tests without any issues.


Note: You can set scope on each dependency or plugin. This will let the maven build know that in which phase that dependency or plugin needs to run.


This is just the basic introduction about the maven. There is a lot that can be done using maven

Comments

Popular posts from this blog

Getting started with Spring Boot Application and pushing it to Github

Spring Boot Caching

Performance test with Gatling