Maven Dependency Scopes

By | October 25, 2020

In this post, We will learn about Maven Dependency Scopes

The dependency scope is mainly used to limit the transitivity of a dependency and to determine when a dependency is included in a classpath.

There are 6 scopes:

compile
This is considered the default scope. It is used if no scope is specified. Dependencies with this scope are needed to buildtest, and run the project.

provided

This scope is very similar to compilebut indicates that we expect the JDK or a container to provide the dependency at runtime. For example, when We build a web application for the Java Enterprise Edition then we would set the dependency on the Servlet API and related Java EE APIs to this scope because the web container provides those classes. A dependency with this scope is added to the classpath at compilation and test time but not the runtime classpath.

runtime
This scope usually indicates that the dependency is not required at compilation time but it is required at execution time. In Other Words, we can say that this maven scope is required to test and run the project.

test
The maven test scope indicates that the dependency is not required at the compilation time of the application main source code but it is only available for the test compilation and execution phases. This scope is typically used for test libraries such as JUnit and Mockito. It can also be used for non-test libraries such as Apache Commons(io). If these libraries are used in unit test cases (src/test/java) but not in the main source code (src/main/java).

system
This maven scope is similar to provided except that you have to provide the JAR which contains under the project’s subdirectory are referred from there. The artifact is always available at a custom specified path and is not looked up in a remote repository.

import
This scope is only supported on a dependency of type pom in the <dependencyManagement> the section in pom.xml. It tells the dependency can be replaced with the list of dependencies in the specified POM’s <dependencyManagement> section. 

Maven dependency transitivity resolution

When we include a maven dependency and it has its own other dependencies (i.e. transitive dependencies) then we may want to be clear about the scope of these transitive dependencies as well.

Let’s try to understand maven transitive dependencies with the help of a simple table. In this below table, if a dependency is set to the scope in the left column then transitive dependencies at the top row will result in a dependency with the scope listed at their intersection.

Dependency compile provided runtime test
compile compile runtime
provided provided provided
runtime runtime runtime
test test test

That’s all about Maven Dependency Scopes

You May Also Like:

What is Maven?
How to install and setup Maven Environment on windows?
Create a maven project using the command line
How to import the maven project in Eclipse?
How to Create a New Maven Project in Eclipse
How to create a maven web application project in Eclipse
How to create a multi-module project using maven?
How to Fix missing src/main/java & src/test/java folders in the Eclipse Maven web Project?
How to create user-defined properties in Maven?
What is the maven local repository location & how to change it?
How to add or update the maven settings.xml file in eclipse?
Importing maven remote Archetype Catalogs in eclipse ?
What is POM in the maven Project?
What is Super POM in Maven Project?
Understanding the purpose of Maven Plugin
Compiling the Maven project with different JDK versions? 
Understanding of settings.xml in maven
How to enable a proxy setting in Maven?
How to generate javadoc in the maven Project
How to create a runnable JAR file with Maven?
How to add local jar files to a Maven project?
How to convert an existing Java Project to Maven in Eclipse?
How to exclude dependency in maven?
Managing External Dependencies in maven
Understanding of Maven Repositories
What is an archetype in Maven?
Snapshot Vs Version in maven
Apache Maven – Profiles

If you have any feedback or suggestion please feel free to drop in below comment box.

Leave a Reply

Your email address will not be published. Required fields are marked *