Updated April 12, 2023
Introduction to Maven Dependency Scope
Maven is a building tool that is most often used in java projects for defining and maintaining the build process. Besides that maven provides us with full-proof dependency management features that helps in building a single module and multi-module building of projects and deploying the same. Maven has maintained a central repository where all the jars and javadocs are available and can be added by adding the dependency tag corresponding to your dependency in your pom.xml. Besides that, there are some dependencies of the dependencies that you have added in your pom.xml. This is called transitive dependencies that are automatically added by maven. In this article, we will discuss transitive dependencies and various scope of dependency in maven.
Types of Dependency Scope in Maven
There are six different dependency scopes used in the maven that will be described one by one in the upcoming session.
1. Transitive Dependencies in Maven
The dependencies that are required by the dependencies specified by you in pom.xml are automatically included by maven by reading all the project files of your dependencies from the remote repository and adding them. This transitive dependency addition can be done up to any level that means adding dependencies of specified dependencies in pom and then further adding dependencies of that dependencies and so on unless and until the cyclic dependency occurs. This leads to the addition of a huge set of dependencies. To minimize this some of the strategies that are followed that help in knowing which dependencies should be included that are as follows –
2. Dependency Mediation
In this technique, if multiple versions are present in the dependencies then the dependency version that is closest to the project is added in the dependency tree. For example, if the dependency relation between V, X, Y, and Z is as follows – V -> X -> Y -> Z 3.6 and V -> W -> Z 2.3 then the nearest path from V to Z via W is chosen and version 2.3 of Z dependency is added in the project.
3. Dependency Management
The versions of the dependencies can be directly mentioned by the authors that will be used while the addition of dependencies in the project. For example, if in the above example the 3.6 version of Z is mentioned in projects’ POM.xml file by the author then instead of 2.3 version of Z 3.6 version of Z dependency will be added in the project.
4. Dependency Scope
The dependencies that are required in the current phase or stage of the build lifecycle are only added in the project.
5. Excluded Dependencies
If project A, B, and C exists that have the relation such that A is dependent on B and B on C then the A project can by using the exclusion element the project A can remove project C from dependencies.
6. Optional Dependencies
If project A is dependent on project B then the project named A can keep the B project dependency as an optional dependency. Now, if any other project C has a dependency on A then C project has the only dependency on project A and not on project B. However, project C can explicitly add the dependency of project B in its project if required.
7. Scope of Dependencies in Maven
One of the techniques to limit the transitive dependencies in the maven project is the dependency scope that also helps in maintaining the classpath that is further used for multiple build tasks. These dependency scopes are mentioned using the <scope> tag inside the dependency tag. Each one of them except the import dependency scope helps in managing and limiting the transitive dependency addition in the project.
Types of Scope in Maven
There are six different scopes in the maven project:
1. Compile Dependency Scope
This is the default scope that is present when none of the scopes is mentioned or specified in the dependency tag. It is transitive i.e the dependencies are available to also the sub-projects that are dependent projects and have the availability over all the classpaths in the project. For example –
Code:
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>1.2.14</version>
<scope>compile</scope>
</dependency>
</dependencies>
In the above dependency tag, even if you don’t add the <scope> tag and not mention compile it is fine as the default scope considered for maven dependencies is compile.
2. Provided Dependency Scope
This scope is similar in working as of compile but you must make sure that the container of JDK supplies with the necessary dependencies during runtime. One of the most common scenarios of provided dependencies is when you are creating the web application using the JEE(Java Enterprise Edition), you can set the scope of servlet API to provided as its related classes will be provided by the web container.
This is non-transitive and is available to only test and compilation classpaths. For example – the servlet API dependency’s scope is mentioned in the following manner –
Code:
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
3. Runtime Dependency Scope
This scope helps in specifying that the current dependency will be used only during execution and not while compilation that is available for test and runtime classpaths and unavailable for compile classpath. For example –
Code :
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>4.0.6</version>
<scope>runtime</scope>
</dependency>
4. Test Dependency Scope
This scope helps to mention that the dependency will only be used while testing and is made available only in the compilation and execution phases of the testing and is non-transitive. This dependency scope is not transitive and hence not available for dependent projects. For example –
Code :
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.2</version>
<scope>test</scope>
</dependency>
5. System Dependency Scope
This dependency scope is similar to the provided scope just difference lies in mentioning the jar on the system instead of any container providing dependencies. The system path pointing towards the required jar should be mentioned in the dependency tag for example
Code :
<dependency>
<groupId>sample-dependency</groupId>
<artifactId>sample-dependency</artifactId>
<scope>system</scope>
<version>1.0</version>
<systemPath>${myDirectory}\jars\MyFolder\var\sampleDependency.jar</systemPath>
</dependency>
6. Import Dependency Scope
These dependency scopes do not help in limiting the transitive dependency addition issue. This dependency scope can only be used for the dependencies that have the type as pom in the <dependencyManagement> tag section. This dependency is replaced by the list of dependencies that are mentioned in the section named <dependencyManagement> inside the pom.xml file. For example
Code :
<dependency>
<groupId>educba.sampleImportScope</groupId>
<artifactId>sample-dependency</artifactId>
<type>pom</type>
<scope>import</scope>
<version>1.0</version>
</dependency>
Conclusion
We can make the use of the dependency scopes in our dependency tags inside the pom.xml file that will help to manage the transitive dependencies and also help in being available for the classpaths where that dependency is required by choosing the appropriate dependency scope for our dependency.
Recommended Article
We hope that this EDUCBA information on “Maven Dependency Scope” was beneficial to you. You can view EDUCBA’s recommended articles for more information.