Maven Dependency Management

Samiksha Jadhav
3 min readMay 5, 2023

Introduction:

For java developers, when building a project, build tools like maven and ant comes into their mind. Now Ant was a lengthy process, you had to write multiple lines to execute any goal. But with maven everything is sorted out with clear instructions. It is very easy to manage, compile and build your project.

When using maven you should manage your code duplications, dependency management, plugin management etc.

This blog focuses on dependency management of maven and best practices when dealing with multiple dependencies.

Maven dependencies:

Maven dependencies is nothing but a jar that your application needs to run. e.g. junit, spring-boot-maven-plugin, maven-surefile-plugin etc. Now these dependencies will need more dependencies to run and these dependencies are known as transitive dependencies.

If you want to exclude any transitive dependency you can do that by excluding those using exclusion block in your dependency to stop your code downloading unnecessary jars.

If you are working on a big project with multiple modules it becomes hard to manage all these dependencies and their versions. In order to change any version you need to make changes in all the modules where these dependencies are used.

In that case we use dependency management section where we define all the versions of dependencies used in our modules and get rid of compatibility problems when updating libraries.

  • Below are the points we should keep in mind to avoid future errors and and make our build faster:

key points:

  1. Dependency convergence: Defining multiple versions in different modules for same jar file, it will give you convergence warning and you will see something like below:

To resolve this you need to create a dependency management section in your project’s root POM and define this dependency with the correct version and remove version from your module’s dependency.

Below is the command to check the dependency convergence:

mvn dependency:tree

2. Unused dependencies:

In a complex project, it is very common to get warnings for unused dependencies. So getting rid of those dependencies won’t harm your project, but will make your application more readable.

Using unnecessary dependencies will make your project heavier and the project needs more RAM to accommodate more classes.

3. Scope:

Another thing you can do is to define scopes for the dependencies.

By default the scope for a dependency is compile, until and unless you define it. Now suppose if you want to run junit only when test cases are executed , then you can set the scope to test. This will help not building unnecessary jars and reduce build time. So when defining your dependency you can mention your scope like below:

there are other scopes as well like runtime, compile, provided etc. , to learn more please refer: https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html

Note: If you want to use a local repository instead of downloading directly from the internet then use the maven settings file to define mirrors for the individual repositories that are defined in the POMS. please find sample format below:

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 https://maven.apache.org/xsd/settings-1.0.0.xsd">
<localRepository/>
<interactiveMode/>
<offline/>
<pluginGroups/>
<servers>
<server>
<id>internal-repository</id>
<username>user-name</username>
<password>user-password</password>
</server>
</servers>
<mirrors>
<mirror>
<id>internal-repository</id>
<name>Internal Repo</name>
<url>https://repo-url/repo/maven2/</url>
<mirrorOf>*</mirrorOf>
</mirror>
</mirrors>
<proxies/>
<profiles/>
<activeProfiles/>
</settings>

Plugin management:

Similarly use the pluginmanagement section of the parent POM to define versions for all plugins that your build uses, including maven plugins like maven-compile-plugin and maven-source-plugin so that build will not behave differently when a new version of a plugin is released.

Summary:

We tried to explain basics of dependency management and what should we keep in mind when dealing with POM’s so that we can save time and also avoid future errors. Theses practices will help to keep our code clean, reduce build time and also provide a clear understanding of the project to everyone.

--

--