What is Maven Dependency

In this article, I have explained the concept of maven dependencies. I have also covered types of dependencies, external dependency, optional dependency and transitive dependency. This article is part of a series on topic Maven Complete Understanding. If you are really interested to learn Maven in depth from basic to advanced. Please go through all the articles of this series. You can start from here.


Table of Contents:

  1. What is a Dependency
  2. Types of Dependencies
  3. Defining Dependency in POM File
  4. External Dependencies
  5. Optional Dependencies

What is a Dependency

If we talk about dependency in the context of Maven, so a dependency is nothing but simply a JAR file used by our Java application.

In any application, we would always require some other libraries which we don’t want to reinvent the wheel for writing that code again. So, we would want to reuse the code which has already been written by somebody else and available in the form of jar files.

So, these jar files are called dependencies for our project. Since, our project compilation is dependent on these jar files and without using them our project will neither compile nor run. These dependencies can exist on remote, central or local repository.

Types of Dependencies

There are two types of Maven dependencies:

  1. Direct dependency: These are the dependencies which we need to define in the POM.xml file (maven configuration file) of our project inside the tag.
  2. Transitive dependency: These dependencies are required by our direct dependencies. We don’t need to explicitly define these dependencies. Maven automatically downloads and includes these dependencies in our project.

You can get the list of all your project’s direct and transitive dependencies by running following command:mvn dependency:tree

Please go through this article for complete understanding about transitive dependencies.

Defining Dependency in POM File

Now, let’s see the below example to understand how we define a dependency in the pom.xml file.

Maven Dependency Example

This is a quick example of the dependency section in pom.xml. <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.3.5.RELEASE</version> </dependency> </dependencies>

Few things to note here:

  1. We have used the <dependencies> tag first.
  2. Inside <dependencies> tag we have used <dependency> tag for each of the individual dependency.
  3. Inside <dependency> tag, we have used <groupId> <artifactId> and <version> tags for both of the dependencies. These 3 tags are required to uniquely identify a dependency.
  4. First dependency is the junit dependency. Which is required to run the test cases of our project.
  5. The second dependency is specific to spring project.
  6. For the first dependency, we have defined <scope> tag also. Which tells that junit dependency is not required for the normal use case but required only for test-compilation and test-execution. You can learn about dependency scope on the article.

External Dependencies

Most of the times, maven would find the required dependency on either central, remote or local repository. But, in some rare cases, it is possible that some jar is not available on any of the repo.

One of the use case can be like: we have developed some other project on local and built a jar file out of that on local itself. Now, we want to use that in our main project. So, this particular jar won’t be available on any of the repository. Then in such cases we can use this jar(dependency) as external dependency.

External dependencies are also called System Dependencies.

To use external dependency first we will create one directory inside our project. Let’s assume, I have created directory named as extlibs inside the project root directory. Now assume that the other project’s jar file name is logutil.jar

Now, we will copy and paste logutil.jar in the extlibs directory.

We need to define this as system dependency in the project’s pom.xml file now.

Here is the example of that:
<dependency> <groupId>com.skilledengg</groupId> <artifactId>logutil</artifactId> <version>1.0</version> <scope>system</scope> <systemPath>${project.basedir}\extlibs\logutil.jar</systemPath> </dependency>

Please note here, we have kept the scope value as system.

Here, the values of groupId, artifactId and version would be the same what is defined in the jar file.

systemPath specifies the relative path of the jar file to the project location.

Optional Dependencies

To explain the optional dependencies, I will take help of the below example. Since, optional dependency concept is a bit important and difficult to explain using just theory.

Example 1:

Let’s say we are developing a java application named all-dbconnector, which supports 4 databases: Mysql, Postgresql, Mongodb and Dynanodb currently and we are working to support more databases in future.

Our application is sort of a wrapper to communicate with these 4 databases. We are developing this application so that in our organisation, if somebody else wants to connect with Mysql in his/her project then he/she will not have to write the connection logic and code. He/she is expected to just use our project’s jar (all-dbconnector.jar) as dependency and use the code to connect with Mysql.

Similarly in some other team, for some other application if there is a requirement to connect with Dynamodb then the developer doesn’t need to write the corresponding code to connect with Dynamodb. The developer is expected to use our application jar (all-dbconnector.jar) file instead.

But, now the catch here is: In our application(all-dbconnector) we have used jar drivers for all 4 databases as dependencies in our pom.xml file.

So, if anybody uses our application as dependency, maven will download jar drivers for all 4 databases because of transitive dependencies. But, we need only one database driver (say, Mysql only).

The solution of this above problem is: optional dependencies.

So, in such scenarios, we will define the dependencies of jar drivers for all 4 databases as optional. So, that Maven will not download those as transitive dependencies. Although, they will be downloaded if they are defined as direct dependencies.

So, in our project (all-dbconnector), we will define them as direct dependency so our build will not fail.

And, whosoever will use all-dbconnector.jar as dependency in their project, they will need to define the exact database jar driver which is required for their application. So that only one database jar driver will be downloaded not all of them.

To summarise this, let’s take another example:

Example 2:Project-Y -> Project-Z

We have Project-Y which depends on Project-Z. And, Project-Y declares Project-Z as optional dependency but since this is direct dependency so while building Project-Y, maven will download Project-Z dependency as usual.Project-X -> Project-Y

Now, let’s say Project-X depends on Project-Y. So, here while building Project-X, Maven will not download Project-Z dependency since that is a transitive dependency and marked as optional inside Project-Y’s pom.xml file.

In case, we need Project-Z dependency in Project-X then we need to explicitly define in pom file of Project-Z.

How to Use Optional Dependency

To mark a dependency as optional dependency , we use <optional> tag for that specific dependency.
<project> ... <dependencies> <dependency> <groupId>com.mysql</groupId> <artifactId>mysql-dbconnector</artifactId> <version>1.0</version> <scope>compile</scope> <optional>true</optional> <!-- This value can be true or false only --> </dependency> <dependency> <groupId>com.postgres</groupId> <artifactId>psql-dbconnector</artifactId> <version>1.0</version> <scope>compile</scope> <optional>true</optional> <!-- This value can be true or false only --> </dependency> </dependencies> </project>

I hope you would have learned something new from this article. This article is part of Complete Maven Understanding Series. So, to learn Maven from Basic to Advanced you can go through all the articles>>.

Still, if you have any feedback/suggestion/concern, please write us.

Leave a Comment