Canarys | IT Services

Blogs

Apache Maven – A tool to build Artifacts

Date:
Author:
Share

Apache 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. Using Maven, we can build and manage any Java based project. Maven software is used very often in the industry, and in this we're going to see the fundamentals of Maven software which will help make effective use of this software.

Introduction:

                Apache Maven is an open source build tool which is based on Java. Apache is a vendor to develop the Maven build tool. Maven is a cross-platform, open source build tool. Maven build tool is not the executable software, but we can download the software and then extract it and use it.

                The most powerful feature of the maven is the automatic downloading of project dependency libraries from the maven central repository, maven remote repository, or local repository. Maven 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.

                Maven 's primary aim is to enable a developer to understand in the shortest possible time the complete state of a development effort. In order to attain this goal, Maven deals with several areas of concern like making the build process easy, providing a uniform build system, providing quality project information, encouraging better development practices etc.

 

Installation:

                The Maven software can be downloaded and installed from official apache maven download page, and the URL for download is https:/maven.apache.org/download.cgi. Java software installation is mandatory before installing maven, since maven only runs on top of Java. So, we should first set JAVA HOME path, because Maven will use the jdk provided in JAVA HOME by default.

                Extract the file after downloading the correct file (i.e. tar or zip) according to requirement. Maven is also set in the PATH (i.e. M2_HOME) to ensure mvn commands work. Using the command ‘mvn -v’ to check whether maven is installed and can be accessed or not.

 

POM.XML:

                POM stands for Project Object Model. Pom.xml has all the information about your project and that's where you'll be telling maven what to do. Let's see pom.xml file contents are as follows.

1

All pom.xml files require the project element and three necessary fields i.e. groupId, artifactid, version. Pom.xml’s root element is project, groupId is the unique amongst an organization or a project, artifactid is the name of the project, version is nothing, but the version of the project and dependencies are the project dependencies etc.

 

Maven Repositories:

                Maven is containing three types of repositories. A maven repository can be one of the following types.

  1. Local Repository
  2. Central Repository
  3. Remote Repository

 

Local Repository:

                The Maven local repository is a local folder that is used to store all the dependencies of your project (i.e. plugins, jars and other files which are downloaded by maven). In simple terms, all the dependency files will be stored in your Maven local repository when you build a maven project. By default, Maven local repository is .m2 folder.

 

Central Repository:

                The central repository is the maven-community repository. It includes a comprehensive collection of frequently used libraries. When maven doesn't find libraries in the local repository, this repository comes in. One can find the central repository at http:/search.maven.org/#browse.

 

Remote Repository:

                Companies generally maintain their own repositories for libraries which are used for the project. These repositories differ from the local repository; on a separate server, a repository is maintained, different from the developer's machine, and is accessible within the organization.

 

Maven Build Lifecycle and Phases:

                Maven follows a build lifecycle to build and distribute artifacts. There are 3 main lifecycles

  1. Default Lifecycle – This lifecycle is all about building and deploying the artifacts.
  2. Clean Lifecycle – This lifecycle deals with project cleaning.
  3. Site Lifecycle – This lifecycle is all about project site documentation.

 

Default Lifecycle:

                Default Lifecycle consists of phases. The following are some of the important phases we will be exploring now.

  • Validate: Validate the project is right, and check if the project has all the necessary details.
  • Compile: Used to compile the source code of the project. E.g.: mvn compile
  • Test: Tests are done using the appropriate unit testing framework. Those tests do not require packaging or deployment of the code. E.g.: mvn test
  • Package: Take the compiled code and bundle it as distributed as a JAR, WAR, EAR etc. E.g.: mvn package
  • Install: Install the package into local repository, for use as a dependency in other projects locally. The local repository location is usually ${user.home}/.m2/repository. E.g.: mvn install
  • Deploy: Copies the final package for sharing with other developers and projects into the remote repository. E.g.: mvn deploy

 

Clean Lifecycle:

  • Clean: This command cleans up everything inside the target folder generated by the previous build. E.g.: mvn clean

 

Site Lifecycle:

  • Site: Generate the project’s site documentation.

 

                There’s a lot more to explore but, in this article, we have just covered the basics of maven i.e. What is Maven? Installation of Maven, Maven Repositories, pom.xml and Maven Build Lifecycles.

Leave a Reply

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

Reach Us

With Canarys,
Let’s Plan. Grow. Strive. Succeed.