Maven — First Steps

Deepak Kashyap
6 min readJun 19, 2019

--

tl;dr Maven is an awesome build automation tool which helps to make the project life cycle simple and powerful

What is Maven ?

Maven is a build tool which helps in automation of build tasks necessary such as Dependency management,it has a very good repository, supports plugin framework and helps to ensure best practices in the projects are maintained.

Key Concepts

  1. Conventions: Maven follows various conventions there by providing an advantage of running the system with little or no configuration and do remember maven provides flexibility to override. One such convention is with respect to the project structure.
Maven Sample Project Structure

2. Project Object Model (POM): This can be considered as the base file or master file for a maven project.

i. As name suggests this file is object based representation of the build.POM is of type XML.It contains various project meta-data like Group (project belongs to), artifact and the version which is collectively called as ‘Maven Coordinates’ which helps to uniquely identify the maven project.

ii.This POM file also includes the dependencies (various other libraries which are referenced in the project) and other build settings.

iii. Every maven project inherits ‘Super POM’ which has various settings defaulted which can be overridden in the project specific POM.

3. Dependency Management: One of the most powerful features or most loved utility of maven is dependency management.If the project has references to various external libraries we can just reference that in our POM file along with the version and maven takes care of getting it from the repository or internet.

i. Transitive Dependencies : If the project has direct dependency then it has be provided in POM , if the dependency in-turn has a dependency on other files then maven resolves the issue for us and there is no need for us to explicitly provide the necessary details.
Example: If Project has dependency on JAR A and JAR A indeed refers to JAR B then in our project we need to just add JAR A dependency in our POM file and maven will in-turn add JAR B on its own.

ii. Cache: If the dependency is downloaded from internet a local copy is maintained which will be referred to when the project invokes/needs it.

4. Maven Repository: Most of the necessary artifacts required for maven which supports in build are stored in a placed called ‘Maven Repository’ and maven has one remote repository called as ‘central’ which has huge number of open source projects. We can build local private repository with files which are confidential or licensed for certain organization.

5. Coverage: Maven tasks/activities are basically divided into Goals,Phases and Life cycles.
i. Goals : Granular level of defining the task in maven.
ii. Phases : Logical grouping of similar goals leads to phases.
iii. Life-cycle : Many such phases are grouped together to form a life-cycle.

6. Plugins: Plugins are the components which provides extended functionality to maven.It will help to achieve more dependencies and goals.

Installing Maven

  1. Download maven from here
  2. Unzip the file downloaded (Preferably to C: Drive but not mandatory)
  3. Set the Environment Variables of ‘PATH’ to include the maven bin directory
PATH being updated by maven bin directory

4. Set the new system variable ‘M2_HOME’ and provide the directory of the apache maven which we unzipped.

System Variable for M2_HOME

5. To crosscheck the installation, go to command prompt and type ‘mvn --version’

Crosscheck Maven version installed

6. The apache-maven directory will contain a file README.txt which provides basic necessary information about maven which is worth to read.

Apache MavenWhat is it?
-----------
Maven is a software project management and comprehension tool. Based on the concept of a Project Object Model (POM), Maven can manage a project's build, reporting and documentation from a central piece of information.
Documentation
-------------
The most up-to-date documentation can be found at https://maven.apache.org/.
Release Notes
-------------
The full list of changes can be found at https://maven.apache.org/docs/history.html.System Requirements
-------------------
JDK:
1.7 or above (this is to execute Maven - it still allows you to build against 1.3
and prior JDK's).
Memory:
No minimum requirement.
Disk:
Approximately 10MB is required for the Maven installation itself. In addition to
that, additional disk space will be used for your local Maven repository. The size
of your local repository will vary depending on usage but expect at least 500MB.
Operating System:
Windows:
Windows 2000 or above.
Unix based systems (Linux, Solaris and Mac OS X) and others:
No minimum requirement.
Installing Maven
----------------
1) Unpack the archive where you would like to store the binaries, e.g.:Unix-based operating systems (Linux, Solaris and Mac OS X)
tar zxvf apache-maven-3.x.y.tar.gz
Windows
unzip apache-maven-3.x.y.zip
2) A directory called "apache-maven-3.x.y" will be created.3) Add the bin directory to your PATH, e.g.:Unix-based operating systems (Linux, Solaris and Mac OS X)
export PATH=/usr/local/apache-maven-3.x.y/bin:$PATH
Windows
set PATH="c:\program files\apache-maven-3.x.y\bin";%PATH%
4) Make sure JAVA_HOME is set to the location of your JDK5) Run "mvn --version" to verify that it is correctly installed.For complete documentation, see https://maven.apache.org/download.html#InstallationLicensing
---------
Please see the file called LICENSE.Maven URLS
----------
Home Page: https://maven.apache.org/
Downloads: https://maven.apache.org/download.html
Release Notes: https://maven.apache.org/docs/history.html
Mailing Lists: https://maven.apache.org/mailing-lists.html
Source Code: https://gitbox.apache.org/repos/asf/maven.git
Issue Tracking: https://issues.apache.org/jira/browse/MNG
Wiki: https://cwiki.apache.org/confluence/display/MAVEN/
Available Plugins: https://maven.apache.org/plugins/

Sample Skeletal POM File

<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 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>learn.maven.sample</groupId>
<artifactId>skeletal-pom</artifactId>
<version>1.0</version>
</project>

Common Maven Phases

  1. validate: To ensure project structure is correct and basic necessary information are provided
  2. compile: To compile the source code in the project
  3. test: Test the source code compiled using #2 using unit testing framework.
  4. package: Compiled code from #2 grouped so it can be transferred or shared in formats like jar/ear/war etc
  5. integration-test: To deploy the package from #4 into higher environment
  6. verify: Quick checks to verify package is valid adheres to the quality
  7. install: The package from #4 will be installed in local repository
  8. deploy: Package from #4 is provided for usage in higher environments
  9. clean: Cleans up artifacts which are not necessarily needed like target directory
  10. site: Is used to generate site documentation of the project

Maven Archetypes

Archetype is a maven project template toolkit. It is one of quick ways of building a project specific to our needs.

mvn archetype:generate

Maven do support lot of archetypes and few examples are as below:

 maven-archetype-quickstart --> For sample maven project
maven-archetype-simple --> For simple maven project
cucumber-archetype --> For sample Cucumber project

For detailed documentation:

If you have any experience in working with Maven and do have any tips/best practices to be followed do comment the same.

--

--

Deepak Kashyap
Deepak Kashyap

No responses yet

Write a response