Introduction to Maven and How to Set up Maven Environment

Last updated on May 27 2022
Mrinalini Pandey

Table of Contents

Introduction to Maven and How to Set up Maven Environment

Introduction to Maven

What is Maven?

Maven is a project management and comprehension tool that provides developers a complete build lifecycle framework. Development team can automate the project’s build infrastructure in almost no time as Maven uses a standard directory layout and a default build lifecycle.
In case of multiple development teams environment, Maven can set-up the way to work as per standards in a very short time. As most of the project setups are simple and reusable, Maven makes life of developer easy while creating reports, checks, build and testing automation setups.
Maven provides developers ways to manage the subsequent −
• Builds
• Documentation
• Reporting
• Dependencies
• SCMs
• Releases
• Distribution
• Mailing list
To summarize, Maven simplifies and standardizes the project build ongoing. It handles compilation, distribution, documentation, team collaboration and other tasks seamlessly. Maven increases reusability and takes care of most of the build related tasks.

Understanding the problem without Maven

There are many problems that we face during the project development. They are discussed below:
1) Adding set of Jars in each project: In case of struts, spring, hibernate frameworks, we need to add set of jar files in each project. It must include all the dependencies of jars also.
2) Creating the right project structure: We must create the right project structure in servlet, struts etc., otherwise it will not be executed.
3) Building and Deploying the project: We must have to build and deploy the project so that it may work.

What it does?

Maven simplifies the above-mentioned problems. It does mainly subsequent tasks.
1. It makes a project easy to build
2. It provides uniform build ongoing (maven project can be shared by all the maven projects)
3. It provides project information (log document, cross referenced sources, mailing list, dependency list, unit test reports etc.)
4. It’s easy to migrate for new features of Maven

Apache Maven helps to manage

• Builds
• Documentation
• Reporing
• SCMs
• Releases
• Distribution

What is Build Tool

A build tool takes care of everything for building a ongoing. It does subsequent:
• Generates source code (if auto-generated code is employed)
• Generates documentation from source code
• Compiles source code
• Packages compiled code into JAR of ZIP file
• Installs the packaged code in local repository, server repository, or central repository

Maven Evolution

Maven was originally designed to simplify building ongoinges in Jakarta Turbine project. There were several projects and each project contained slightly different ANT build files. JARs were checked into CVS.
Apache group then developed Maven which can build multiple projects together, publish projects information, deploy projects, share JARs across several projects and help in collaboration of teams.

Objective

The primary goal of Maven is to provide developer with the subsequent −
• A comprehensive model for projects, which is reusable, maintainable, and easier to comprehend.
• Plugins or tools that interact with this declarative model.
Maven project structure and contents are declared in an xml file, pom.xml, referred as Project Object Model (POM), which is the fundamental unit of the entire Maven system. In later chapters, we will explain POM in detail.

Convention over Configuration

Maven uses Convention over Configuration, which means developers are not required to create build ongoing themselves.
Developers do not have to mention each and every configuration detail. Maven provides sensible default behavior for projects. When a Maven project is created, Maven creates default project structure. Developer is only required to place files accordingly and he/she need not to define any configuration in pom.xml.
As an example, subsequent table shows the default values for project source code files, resource files and other configurations. Assuming, ${basedir} denotes the project location −

Item Default
source code ${basedir}/src/main/java
Resources ${basedir}/src/main/resources
Tests ${basedir}/src/test
Complied byte code ${basedir}/target
distributable JAR ${basedir}/target/classes

In order to build the project, Maven provides developers with options to mention life-cycle goals and project dependencies (that rely on Maven plugin capabilities and on its default conventions). Much of the project management and build related tasks are maintained by Maven plugins.
Developers can build any given Maven project without the need to understand how the individual plugins work. We will discuss Maven Plugins in detail in the later chapters.

Features of Maven

• Simple project setup that follows best practices.
• Consistent usage across all projects.
• Dependency management including automatic updating.
• A large and growing repository of libraries.
• Extensible, with the ability to easily write plugins in Java or scripting languages.
• Instant access to new features with little or no extra configuration.
• Model-based builds − Maven is able to build any number of projects into predefined output types such as jar, war, metadata.
• Coherent site of project information − Using the equivalent metadata as per the build ongoing, maven is able to generate a website and a PDF including complete documentation.
Release management and distribution publication − Without additional configuration, maven will integrate with your source control system such as CVS and manages the release of a project.
Backward Compatibility − You can easily port the multiple modules of a project into Maven 3 from older versions of Maven. It can support the older versions also.
Automatic parent versioning − No need to specify the parent in the sub module for maintenance.
Parallel builds − It analyzes the project dependency graph and enables you to build schedule modules in parallel. Using this, you can achieve the performance improvements of 20-50%.
Better Error and Integrity Reporting − Maven improved error reporting, and it provides you with a link to the Maven wiki page where you will get full description of the error.

Maven – Environment Setup

Maven is a Java based tool, so the very first requirement is to have JDK installed on your machine.

System Requirement

JDK 1.7 or above.
Memory No minimum requirement.
Disk Space No minimum requirement.
Operating System No minimum requirement.

Step 1 – Verify Java Installation on your Machine

Open console and execute the subsequent java command.

OS Task Command
Windows Open Command Console c:\> java -version
Linux Open Command Terminal $ java -version
Mac Open Terminal machine:~ joseph$ java -version

Let’s verify the output for all the operating systems −

OS Output
Windows java version “1.7.0_60”

Java(TM) SE Runtime Environment (build 1.7.0_60-b19)

Java HotSpot(TM) 64-Bit Server VM (build 24.60-b09, mixed mode)

Linux java version “1.7.0_60”

Java(TM) SE Runtime Environment (build 1.7.0_60-b19)

Java HotSpot(TM) 64-Bit Server VM (build 24.60-b09, mixed mode)

Mac java version “1.7.0_60”

Java(TM) SE Runtime Environment (build 1.7.0_60-b19)

Java HotSpot(TM) 64-Bit Server VM (build 24.60-b09, mixed mode)

If you do not have Java installed, install the Java Software Development Kit (SDK) from https://www.oracle.com/technetwork/java/javase/downloads/index.html. We are assuming Java 1.7.0.60 as installed version for this tutorial.

Step 2 – Set JAVA Environment

Set the JAVA_HOME environment variable to point to the base directory location where Java is installed on your machine. For example −

OS Output
Windows Set the environment variable JAVA_HOME to C:\Program Files\Java\jdk1.7.0_60
Linux export JAVA_HOME=/usr/local/java-current
Mac export JAVA_HOME=/Library/Java/Home

Append Java compiler location to System Path.

OS Output
Windows Append the string “;C:\Program Files\Java\jdk1.7.0.60\bin” to the end of the system variable, Path.
Linux export PATH=$PATH:$JAVA_HOME/bin/
Mac not required

Verify Java Installation using java -version command as explained above.

Step 3 – Download Maven Archive

Download Maven 2.2.1 from https://maven.apache.org/download.cgi.

OS Archive name
Windows apache-maven-3.3.1-bin.zip
Linux apache-maven-3.3.1-bin.tar.gz
Mac apache-maven-3.3.1-bin.tar.gz

Step 4 – Extract the Maven Archive

Extract the archive, to the directory you wish to install Maven 3.3.1. The subdirectory apache-maven-3.3.1 will be created from the archive.

OS Location (can be different based on your installation)
Windows C:\Program Files\Apache Software Foundation\apache-maven-3.3.1
Linux /usr/local/apache-maven
Mac /usr/local/apache-maven

Step 5 – Set Maven Environment Variables

Add M2_HOME, M2, MAVEN_OPTS to environment variables.

OS Output
Windows Set the environment variables using system properties.

M2_HOME=C:\Program Files\Apache Software Foundation\apache-maven-3.3.1 M2=%M2_HOME%\bin MAVEN_OPTS=-Xms256m -Xmx512m

Linux Open command terminal and set environment variables.

export M2_HOME=/usr/local/apache-maven/apache-maven-3.3.1 export M2=$M2_HOME/bin

export MAVEN_OPTS=-Xms256m -Xmx512m

Mac Open command terminal and set environment variables.

export M2_HOME=/usr/local/apache-maven/apache-maven-3.3.1

export M2=$M2_HOME/bin

export MAVEN_OPTS=-Xms256m -Xmx512m

Step 6 – Add Maven bin Directory Location to System Path

Now append M2 variable to System Path.

OS Output
Windows Append the string ;%M2% to the end of the system variable, Path.
Linux export PATH=$M2:$PATH
Mac export PATH=$M2:$PATH

Step 7 – Verify Maven Installation

Now open console and execute the subsequent mvn command.

OS Output
Windows Append the string ;%M2% to the end of the system variable, Path.
Linux export PATH=$M2:$PATH
Mac export PATH=$M2:$PATH

Finally, verify the output of the above commands, which should be as follows −

OS Output
Windows Apache Maven 3.3.1 (r801777; 2009-08-07 00:46:01+0530)

Java version: 1.7.0_60

Java home: C:\Program Files\Java\jdk1.7.0_60 \jre

Linux Apache Maven 3.3.1 (r801777; 2009-08-07 00:46:01+0530)

Java version: 1.7.0_60

Java home: C:\Program Files\Java\jdk1.7.0_60 \jre

Mac Apache Maven 3.3.1 (r801777; 2009-08-07 00:46:01+0530)

Java version: 1.7.0_60

Java home: C:\Program Files\Java\jdk1.7.0_60 \jre

So, this brings us to the end of blog. This Tecklearn ‘Introduction to Maven and How to Set up Maven Environment’ blog helps you with commonly asked questions if you are looking out for a job in DevOps. If you wish to learn Maven and build a career in DevOps domain, then check out our interactive, Maven Training, that comes with 24*7 support to guide you throughout your learning period. Please find the link for course details:

Maven

Maven Training

About the Course

Tecklearn has specially designed this Maven Training Course to advance your skills for a successful career in this domain. The course will cover different components of Maven and how they are used in software development operations. You will get an in-depth knowledge of these concepts and will be able to work on related demos. Upon completion of this online training, you will hold a solid understanding and hands-on experience with Maven.

Why Should you take Maven Training?

• The average salary for “ant maven” ranges from approximately $71,430 per year for Entry Level Engineer to $126,916 per year for Development Operations Engineer. – Indeed.com
• According to Grand View Research, the DevOps market size is estimated to be worth $12.85 billion by 2025. DevOps professionals are highly paid and in-demand throughout industries including retail, eCommerce, finance, and technology.

What you will Learn in this Course?

Introduction to DevOps

• What is Software Development
• Software Development Life Cycle
• Why DevOps?
• What is DevOps?
• DevOps Lifecycle
• DevOps Tools
• Benefits of DevOps
• How DevOps is related to Agile Delivery
• DevOps Implementation

Maven

• Maven
• Maven Directory
• Maven Lifecycle
• Maven Dependencies
• Maven Repositories
• Phases and Goals

0 responses on "Introduction to Maven and How to Set up Maven Environment"

Leave a Message

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