Wednesday, March 4, 2020

What is Maven - Why We need Maven - Maven Introduction | Maven Tutorial

What is Maven?

Maven is a popular tool for building and managing any Java based projects. It allows the developer to create projects using Project Object Model and plugins. It helps to build projects, dependency, and documentation. Its development process is very similar to ANT. However, it is much advanced than ANT.

What Maven does ?
  • Compilation of Java Source Code
  • Execute Tests (unit tests and functional tests)
  • Packaging the compiled code into JAR,WAR, EAR artifacts
  • Upload the packages to remote repositories (Nexus, Artifactory)

Maven pom.xml file

POM is an acronym for Project Object Model. The pom.xml file contains information of project and configuration information for the maven to build the project such as dependencies, build directory, source directory, test source directory, plugin, goals etc.

Maven reads the pom.xml file, then executes the goal. pom.xml should have 4 important information.

<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mycomp.dept</groupId>
  <artifactId>MyWebApp</artifactId>
  <version>1.0.0</version>
</project>
  1. project - It is root element of pom.xml 
  2. modelVersion- sub-element that specifies modelVersion, it should be set to 4.0.0 
  3. groupId —will identify your project uniquely across all projects, ex:ebs.obill.webs, com.companyname.project
  4. artifactId — is the name of the jar without version(keeping in mind that it should be jar-name friendly)
  5. version — if you distribute it then you can choose any typical version with numbers and dots (1.0, 1.1, 1.0.1, …)

Create a Java project using Maven
The following command will create a Java application called MyWebApp

mvn archetype:generate -DgroupId=com.capone.af -DartifactId=MyWebApp -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false

Maven Project structure




The src directory is the root directory of source code and test code.
The main directory is the root directory for source code related to the application itself, not test code.
The test directory contains the test source code.
The java directories under main and test contains the Java code for the application itself which is under main and the Java code for the tests which is under test.
The resources directory contains the resources needed by your project.
The target directory is created by Maven. It contains all the compiled classes, JAR files etc.

Maven Build lifecycle

The Maven build follows a specific life cycle to deploy and distribute the target project.
There are three built-in life cycles:
  • default: the main life cycle as it's responsible for project deployment
  • clean: to clean the project and remove all files generated by the previous build
  • site: to create the project's site documentation
Maven Phases

A Maven phase represents a stage in the Maven build lifecycle. Each phase is responsible for a specific task.
Here are some of the most important phases in the default build lifecycle:

Build Phase Description
validate Validates that the project is correct and all necessary information is available. This also makes sure the dependencies are downloaded.
compile Compiles the source code of the project.
test Runs the tests against the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployed.
package Packs the compiled code in its distributable format, such as a JAR.
install Install the package into the local repository, for use as a dependency in other projects locally.
deploy Copies the final package to the remote repository for sharing with other developers and projects.

Maven Architecture Diagram




1 comment:

  1. Thanks for sharing it. it is very clear to know complete details about maven tool.

    ReplyDelete