Building with Maven

Sravanthi
5 min readOct 5, 2022

The purpose of this article is to explain how maven works and to focus on its core concepts.

What is Build tool?

  • A build tool automates the entire process of building software.

What are Build Activities:

  • For instance, a Java project will include activities such as these:
  1. Make the directories.
  2. Build the project’s class path with all its dependencies.
  3. Compile the project’s source code.
  4. Add non-java resources to the classes directory.
  5. Package the project.
  6. Execute the project.

Manually performing the above activities is time consuming and error prone.

The build tool automates all these build activities, which minimises the risk of manual errors and speeds up the build process.

What is Maven?

It is a build tool developed in Java that provides tools for building java applications.

Let’s take a closer look at some of Maven’s core features.

Standardized directory structure:

The maven project directory structure is standard. We simply need to provide information about the project to Maven and Maven will take care of building it. This is why maven is referred to as a “declarative build management tool”.

Benefits:

a) In no time, the developer can switch from one project to another. Since all the projects have the same structure, developers can start working on the projects quickly.

b) It is possible to work on any IDE for developing projects. Every IDE supports the maven standardized directory structure.

Project Name
- src
- main
- java
- resources
- test
- java
- resources
pom.xml
MAVEN DIRECTORY STRUCTURE

What is POM.XML?

The Project Object Model(POM) is an XML file describing the project resources like source code, test code, dependencies etc. It should be placed in the root directory of the project.

Maven reads the information declared inside the file and performs the build activities.

<?xml version=”1.0" encoding=”utf-8"?>
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.google</groupId>
<artifactId>gmail</artifactId>
<version>1.0</version>

</project>
pom.xml

We need to declare four elements in this file.

modelVersion: It specifies which version of the pom file we use when writing pom.

groupId: Identifier for the company’s domain name.

artifactId: Name of the project

version: Version of the project.

Together, groupId, artifactId, and version are called “gav” coordinates.

Dependency management:

Compiling and executing a project requires external jar dependencies to be added to the classpath. Maintaining these dependencies can be a comprehensive task as a project grows.

How do you specify dependencies in maven?

The developer can declare the dependencies required for compiling/packaging a project in pom.xml rather than downloading them manually.

Benefits:

a) At the time of building the project, Maven determines which dependencies are required from the pom.xml of your project and automatically downloads them. It also adds them to the classpath at the time of compilation.

b) It allows us to quickly identify which versions and dependencies are required to build a project.

All the dependencies of the project can be viewed by using the command:

mvn dependency:tree

How does Maven identifies dependencies uniquely?

By specifying which external libraries your project requires in pom.xml, maven downloads and manages them for you. Maven uses GAV coordinates to identify and locate these artifacts.

What are Maven repositories?

Maven supports three types of repositories for managing dependencies:
a) Local repository
b) Central repository
c) Remote repository

a) Local repository :

Maven’s local repository is a directory structure that organizes and manages artifacts in developer maching under the user’s default location /.m2 directory

Benefits:

  • It allows developers to reuse artifacts across projects and reduce the size of the computer’s local disk-space.
  • Reduces the network bandwidth consumption and build time by eliminating the need to download the same artifact each time the application is built.

b) Central repository:

Managing and distributing dependencies worldwide is made possible by Maven’s central repository. Open source contributors around the world can upload and make their dependencies discoverable through the maven central repository, which acts as a marketplace for distributing their libraries.

c) Remote repository

A remote repository resides on a web server and is often used to host artifacts that are shared among multiple projects within the organization.

For instance, an internal security library, which is used across multiple internal projects of the company. This library should not be publicly available and cannot be hosted in a central public maven repository. Instead, it can be hosted in an internal remote repository.

Transitive dependency management:

Transitive dependencies: When we add dependencies to a project it may rely on other dependencies. These dependencies are called transitive dependencies.

Developers typically spend a significant amount of time setting up dependent and transitive dependencies in the development stage. Using Transitive dependency management, Maven allows you to overcome the above problem. By declaring direct dependencies in pom.xml, Maven automatically identifies their transitive dependencies.

We may experience conflicting dependency resolution with transitive dependency management, where two dependencies may have declared the same transitive dependency for two different versions. In such cases, Maven uses dependency mediation to resolve the conflict.

Dependency Mediation Rules:

Using these rules, maven will resolve conflicting dependencies.

  • Always choose the transitive dependency that is close to the top of the tree.
  • If conflicting transitive dependencies are at the same level, then pick the leftmost of the tree.

Exclude dependencies:

Jars that are vulnerable or incompatible with any other dependencies can be excluded by using the <exclusion> tag under <exclusions> tag in pom.xml. To exclude a dependency, you will need to specify its groupId & artifact Id.

Suppose I want to exclude the aop dependency from the spring context. Following are the steps for exclusion:

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.23</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.3.23</version>
</exclusion>
</exclusions>

</dependency>

Dependency scopes:

All the dependencies are not required during all the phases of the development. For instance, some dependencies are needed at compile time, others during packaging, and others during unit testing etc. Dependency scopes allow you to specify which dependencies are needed during which phases of the build.

Some of the most used scopes are:

compile: The default scope of a dependency is compile. The compile scope means that a dependency should be included during the entire build cycle.

provided: Since these dependencies already come with the runtime environment into which the application is going to be run, they should be excluded during runtime/packaging. However, they should be included in the remaining phases of the build.

runtime: The dependencies should not be included during compilation, but should be included during runtime/packaging.

test: Only include these dependencies in the classpath when compiling and running the testcase code.

MAVEN VERSIONING:

Maven projects use a standard version convention of

majorVersion.minorVersion.incrementalVersion-qualifier.

Qualifier- denotes the stage of the artifact being produced.

These are the basic concepts that you should know when using Maven. In the next articles we will learn about the Maven build cycle, plugins, archetypes etc.

Thanks for reading!

--

--