EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials Maven Tutorial Maven Dependency Scope
Secondary Sidebar
Maven Tutorial
  • Maven Basic and advance
    • What is Maven
    • How to Install Maven
    • Maven Commands
    • Maven GroupId
    • Maven WAR Plugin
    • Maven Build Command
    • Maven Failsafe Plugin
    • Maven Profile
    • IntelliJ Maven
    • Maven Enforcer Plugin
    • Maven Javadoc Plugin
    • Maven WAR Plugin
    • Maven Build Command
    • Maven GroupId
    • Maven Force Update
    • Maven Encrypt Password
    • Maven Environment Variables
    • AssertJ Maven
    • Maven Run Single Test
    • Maven kafka-clients
    • Maven Quickstart Archetype
    • Maven Install Dependencies
    • Maven XMLBeans
    • Maven Local Repository
    • Maven Versions
    • Maven Jar Plugin
    • Maven Assembly Plugin
    • Maven exec plugin
    • Maven Central Repository
    • Maven Surefire
    • Maven Deploy
    • Maven Phases
    • Maven Archetype
    • Maven Skip Test
    • Maven Dependency Scope
    • Maven Shade Plugin
    • Maven Repository Spring
    • Maven Eclipse Plugin
    • Maven Exclude Dependency
    • Maven Life Cycle
    • Maven Repository
    • Maven POM File
    • Maven Plugins
    • What is Maven Plugins
    • Maven Interview Questions
    • Maven Flags
    • Maven Project
    • Maven Settings.XML

Maven Dependency Scope

By Payal UdhaniPayal Udhani

Maven Dependency Scope

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.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

All in One Software Development Bundle(600+ Courses, 50+ projects)
Python TutorialC SharpJavaJavaScript
C Plus PlusSoftware TestingSQLKali Linux
Price
View Courses
600+ Online Courses | 50+ projects | 3000+ Hours | Verifiable Certificates | Lifetime Access
4.6 (86,130 ratings)

Types of Dependency Scope in Maven

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:

Types of Scope in Maven

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

This is a guide to Maven Dependency Scope. Here we discuss the Introduction to Maven Dependency Scope and its Various types along with Code Implementation. You can also go through our other suggested articles to learn more –

  1. Maven Life Cycle
  2. Maven Repository
  3. Maven Plugins
  4. Gradle vs Maven
Popular Course in this category
Maven Training (4 Courses, 6 Projects)
  4 Online Courses |  6 Hands-on Project |  26+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

C# Programming, Conditional Constructs, Loops, Arrays, OOPS Concept

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more