Maven’s pom.xml Introduction

Abhishek Singh
3 min readMar 11, 2021

pom.xml :

Maven is Dependency management and build tool used to handle
both stand alone and Archetype (web, restful) applications.

Dependency Management:
Getting main Jars and its child jars with version support
(without conflicts) into project workspace is called as
Dependency Management.

Build:
Converting our application into final JAVA executable format i.e .jar/.war/.ear

Major components of pom.xml:
(1) Current Project
(2) Parent Project
(3) Dependencies
(4) Build plugins

pom.xml format:
<project>
<modelVersion>4.0.0</modelVersion>
<! — CUrrent Project info →
<groupId>a.l</groupId>
<artifactId>HelloApp</artifactId>
<version>1.0</version>

<! — Parent Project info →
<parent>
<groupId>a.a</groupId>
<artifactId>DBApp</artifactId>
<version>6.1</version>
<relativePath/> <! — lookup parent from repository →
</parent>

<! — Current Project info →

<properties>
<java.version>1.8</java.version>
</properties>

<! — Project JARs Dtails →

<dependencies>
<dependency>
<groupId>a.b</groupId>
<artifactId>Web-Test</artifactId>
<version>5.6</version>
<scope>compile</scope>
<exclusions>
<exclusion>
<groupId>xyz.com</groupId>
<artifactId>Web.abc</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

<! — Plugins Details →
<build>
<plugins>
<plugin>
<groupId>org.maven..</groupId>
<artifactId>maven-compiler</artifactId>
</plugin>
</plugins>
</build>
</project>

Dependency exclusions:
When <dependency> tag is added in pom.xml then it will download
parent jars and all its child jars also, to avoid any one or more
child jars from chain, use concept called exclusion,

Syntax: —
<dependencies>
<dependency>
<groupId>..</groupId>
<artifactId>..</artifactId>
<version>..</version>
<exclusions>
<exclusion>
<groupId>..</groupId>
<artifactId>..</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

Ex: —
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.5</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

Scope in Maven Dependency:

(<scope> </scope>

For every dependency one scope is given by maven i.e
default scope : compile, This tag is optional and
indicates when a JAR should be used/loaded

POM format: —

<dependency>
<groupId>…</groupId>
<artifactId>…</artifactId>
<scope>….</scope>
</dependency>

Possible Maven dependency scopes :

There are 5 types of scopes in pom.xml

(1) compile scope : A jar required from compilation time onwards, It is only default scope,
(2) runtime scope : A jar required when we are running an Application, not before that,
(3) test scope : A Jar required only for unit testing time,
(4) provided scope : A jar provided by servers or Frameworks (Container)
(5) system scope : A Jar loaded from File System (like D:/abc/myjar/…)
in this case we should also give <SystemPath> with
location of JAR e.g <systemPath>D:/asf/lib/</systemPath>

NOTE:
There is a dependency jar which not existing in the maven centre but locally
After mvn clean install, this dependency jar can’t be found from the fat jar
is this an known-issue? the workaround is have to install it into local
maven repo with command:

mvn install:install-file -Dfile=lib/routines.jar -DgroupId=org.talend -DartifactId=routines -Dversion=1.0 -Dpackaging=jar

Then using normal dependency format in the pom.xml like this:
<dependency>
<groupId>org.talend</groupId>
<artifactId>routines</artifactId>
<version>1.0</version>
</dependency>

Format of Scope:

<dependencies>

<! — Compiler time (Default) Execution →
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<scope>compile</scope> <! — Optional →
</dependency>

<! — Runtime Execution →
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>

<! — Test time Execution →
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<! — framework or container time Execution →
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.0</version>
<scope>provided</scope>
</dependency>

<! — From local system Execution →
<dependency>
<groupId>routines</groupId>
<artifactId>routines</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${basedir}/lib/routines.jar</systemPath>
</dependency>
</dependencies>

That is a brief introduction to pom.xml, there are a lot more things to know about pom.xml, i will cover in next post, see you in next post, Keep learning…

--

--