top of page
hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-

About Maven


Maven is a popular open-source build automation tool that has become an essential tool for developers building Java applications. It provides a standard way to manage projects, dependencies, and build processes. In this blog, we will explore what Maven is, how it works, and how it can benefit developers.


What is Maven

Maven is a powerful build automation tool that was developed by Apache Software Foundation. It is a command-line tool that uses a project object model (POM) to describe the project and its dependencies. The POM is an XML file that contains information about the project's dependencies, build settings, and other metadata. With Maven, developers can easily manage the entire lifecycle of a project, including compiling source code, packaging the application, and deploying it to a server.


How to Use Maven

The 3 things we need to remember to use Maven

  1. Configure Maven in Java, using Project Object Model (POM) found in a pom.xml file.

  2. All Maven-related configuration settings are found in the POM. You can edit and configure plug-ins in the <plugins> tag of a pom.xml file.

  3. Maven provides default settings for configurations, so you don’t have to add every configuration into the pom.xml file.


Advantages of Maven

  • Helps manage all the processes, such as building, documentation, releasing, and distribution in project management

  • Simplifies the process of project building

  • Increases the performance of the project and the building process

  • The task of downloading Jar files and other dependencies is done automatically

  • Provides easy access to all the required information

  • Makes it easy for the developer to build a project in different environments without worrying about the dependencies, processes, etc.

  • In Maven, it’s easy to add new dependencies by writing the dependency code in the pom file conversely, Maven has a few drawbacks.

  • Maven requires installation in the working system and the Maven plug-in for the IDE

  • If the Maven code for an existing dependency is unavailable, you cannot add that dependency using Maven itself

An Example

Following 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

compile byte code

${basedir}/target

distributable JAR

${basedir}/target/classes

POM(Project Object Model)


POM stands for Project Object Model. It is fundamental unit of work in Maven. It is an XML file that resides in the base directory of the project as pom.xml.

The POM contains information about the project and various configuration detail used by Maven to build the project(s).

POM also contains the goals and plugins. While executing a task or goal, Maven looks for the POM in the current directory. It reads the POM, gets the needed configuration information, and then executes the goal. Some of the configuration that can be specified in the POM are following −

  • project dependencies

  • plugins

  • goals

  • build profiles

  • project version

  • developers

  • mailing list

Before creating a POM, we should first decide the project group (groupId), its name (artifact) and its version as these attributes help in uniquely identifying the project in repository.

There should be a single POM file for each project.

  • All POM files require the project element and three mandatory fields: groupId, artifactId, version.

  • Projects notation in repository is groupId:artifactId:version.


Example


<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/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.companyname. project-group</groupId><artifactId>project</artifactId><version>1.0</version></project>

Maven has the following three standard lifecycles

  • clean

  • default(or build)

  • site

A goal represents a specific task which contributes to the building and managing of a project. It may be bound to zero or more build phases. A goal not bound to any build phase could be executed outside of the build lifecycle by direct invocation.

The order of execution depends on the order in which the goal(s) and the build phase(s) are invoked. For example, consider the command below. The clean and package arguments are build phases while the dependency:copy-dependencies is a goal.

mvn clean dependency: copy-dependencies package

There are few important concepts related to Maven Lifecycles, which are worth to mention −

  • When a phase is called via Maven command, for example mvn compile, only phases up to and including that phase will execute.

  • Different maven goals will be bound to different phases of Maven lifecycle depending upon the type of packaging (JAR / WAR / EAR).

Site Lifecycle

Maven Site plugin is generally used to create fresh documentation to create reports, deploy site, etc. It has the following phases −

  • pre-site

  • site

  • post-site

  • site-deploy.

Build Profile

A Build profile is a set of configuration values, which can be used to set or override default values of Maven build. Using a build profile, you can customize build for different environments such as Production v/s Development environments.

Profiles are specified in pom.xml file using its activeProfiles/profiles elements and are triggered in variety of ways. Profiles modify the POM at build time, and are used to give parameters different target environments (for example, the path of the database server in the development, testing, and production environments).

Types of Build Profile

Build profiles are majorly of three types.

TypeWhere it is definedPer ProjectDefined in the project POM file, pom.xmlPer UserDefined in Maven settings xml file (%USER_HOME%/.m2/settings.xml)GlobalDefined in Maven global settings xml file (%M2_HOME%/conf/settings.xml)

Profile Activation

A Maven Build Profile can be activated in various ways.

  • Explicitly using command console input.

  • Through maven settings.

  • Based on environment variables (User/System variables).

  • OS Settings (for example, Windows family).

  • Present/missing files.

Profile Activation Examples

Let us assume the following directory structure of your project −

Now, under src/main/resources, there are three environment specific files −

Sr.No.File Name & Description1

env.properties

default configuration used if no profile is mentioned.

2

env.test.properties

test configuration when test profile is used.

3

env.prod.properties

production configuration when prod profile is used.

46 views0 comments

Recent Posts

See All

Comentarios

Obtuvo 0 de 5 estrellas.
Aún no hay calificaciones

Agrega una calificación
bottom of page