Introduction to Maven




Maven is used very often in the industry and I felt it will be good to cover the basics of Maven in this article so that it can be used efficiently.
This article will cover things like maven basics, maven plugins, maven dependencies and maven build lifecycle.

What is Maven

Maven was created to have a standard way in which Projects can be built. One of its powerful features is dependency management.
Maven is commonly used for dependency management, but it is not the only thing maven is capable of doing.
If you do not know what dependency management means, don’t worry. I will cover that in this article as well.

Maven History

Maven was initially designed and developed by the Jakarta turbine project. At the later point of time, the Apache group developed the Maven to such an extent to support developing & building multiple projects together, publishing those projects, deploying them and generating the reports.
The JARs/WARs of any maven project can be shared across any distributed environments.

Advantages of Maven over Ant
  • Better dependency management - Maven is superior to Ant when it comes to dependency management. 
  • More powerful builds - Maven's default plugins and life cycle allow a project to perform common build actions without touching a build configuration file. For example, without adding anything to a project's pom.xml
  • Better debugging - Maven repositories allow an artifact's source code to be published alongside the artifact's JAR. Integrated Development Environments such as Eclipse allow developers to automatically download the source code, which lets debuggers drill into the source code and investigate why the artifact did not behave correctly
  • Better collaboration - Maven repositories allow an artifact's Javadoc to be published alongside the artifact's JAR. Integrated Development Environments such as Eclipse allow developers to automatically download the Javadoc and understand how to use the artifact.
  •  More componentized builds - Maven makes it easy to identify when a project has a dependency on code outside its source control folders. Developers and managers alike prefer small, componentized builds because they allow code changes to be very quickly tested and integrated.
  • Reduced duplication - Maven projects can use a project object model (POM) hierarchy to reduce the duplication that typically exists in Ant projects. For example, rather than have each project create an Ant build.xml that takes care of all the details of configuring Sonar properties, such configuration can occur in a Maven parent POM that child projects inherit from.
  • More consistent project structure - All Maven projects have a common structure, which makes it easier to understand each project.



Setting up Maven Environment

Step 1: Download Maven

http://maven.apache.org/download.cgi is the link to download Maven. ANT and Maven both are by Apache Software Foundation.
Click and download the ‘Binary Zip’ (Ex: apache-maven-3.6.0-bin.zip) file from the ‘current stable’ version.

Step 2: Install Maven

Maven does not uses an executable file to install. It is just a zip file. Just unzip it to a folder in the hard disk.

Step 3: Setup Maven

1. Add environment variable MAVEN_HOME to the path where you extracted the maven binary zip file.
2. Add path variable, so that we can execute ‘mvn’ command from any location in command prompt.



Verify if you have done everything right,

What is POM?

The easiest way to describe a POM in a maven project is, it is nothing but the core element of any maven project. Basically, any maven project consists of one configurable file called pom.xml, which stands for the abbreviation Project Object Model. This pom.xml will always be located in the root directory of any maven project. This file represents the very basic and fundamental unit in maven.
The pom.xml basically contains the information related to the project which is built or to be built in. It contains all the necessary information about the configuration details, dependencies included and plug-ins included in the project. In simple, it contains the details of the build life cycle of a project.
Below are some of the configurations that can be handled in the pom.xml file :
·        Dependencies used in the projects (Jar files)
·        Plugins used (report plugin)
·        Project version
·        Developers involved in the project
·        Mailing list
·        Reporting
·        Build profiles

Sample Pom.xml File

A typical pom.xml of simple java project will look like as shown in the below figure.

A pom.xml will always start with the root element called <project> under which all the other required configurations will be made. Developers should ensure to define the below list of elements which are known as maven co-ordinates before defining a pom.xml file :
1. groupId - as the name itself shows that this is an id which is unique for any project in an organization.
2.  artifactId - Even though the name says as "id", this is basically defines the name of any project.
3.  version - This element is used to derive the version of any project in order to classify the versions as and when the major changes/implementations are carried during the development phase of a project.

Whenever it comes for executing a task for a project, maven scans through the entries made in the pom. Xml file. This will enable the maven to read all the configurations made, build profiles defined, repositories configured and all other important details and then executes the task accordingly.
NOTE : Actually pom.xml was earlier used in the name of project.xml in maven 1. As the maven 2 version was introduced, this was renamed to pom.xml

You get some ideas about Maven now. This article covered just the basics of what is maven, maven history pom, advantages over ant and how to setting up maven. To know more about Maven check the links I have given above.
Happy Coding 😄



Disqus Comments