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:

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.


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.

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.

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).

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.

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
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 *