Maven Dependency Scopes

Maven is a powerful project management tool that is based on POM (project object model). It is used for projects to build, dependency, and documentation. It simplifies the build process like ANT. ... In short, we can tell maven is a tool that can be used for building and managing any Java-based project.

The Dependency management is a core feature of Maven. Managing dependencies for a single project is easy. But for managing dependencies for multi-module projects and applications that consist of hundreds of modules is complex.But it is possible due to scopes in maven dependencies. Maven helps a great deal in defining, creating, and maintaining reproducible builds with well-defined classpaths and library versions.

Maven dependency scope is used to specify the visibility of a dependency, relative to the different lifecycle phases (build, test, runtime etc). Maven provides six scopes i.e. compile, provided, runtime, test, system, and import.We will see them one by one.

1). Runtime Scope
2). Provided Scope
3). Compile Scope
4). Test Scope
5). System Scope
6). Import Scope
7). Transitivity


1). Runtime Scope

This types of dependency not required at the time where we compile the project or use maven clean install. They are present in the classpath.They required when we run the project.

<dependency> <groupid>mysql</groupid> <artifactid>mysql-connector-java</artifactid> <version>8.0.20</version> <scope>runtime</scope> </dependency>

2). Provided Scope

This kind of dependency are used while building and testing of the project. They are only available in compile-classpath and test-classpath.they are required to run the project,but not exported while making war or Jar files.
The dependency will be provided by runtime. A web or a servlet container etc will provide the dependent JARS in the deployed mode.

<dependency> <groupid>javax.servlet</groupid> <artifactid>javax.servlet-api</artifactid> <version>4.0.1</version> <scope>provided</scope> </dependency>

3). Compile Scope

This is the default scope for dependency in maven when no other scope is provided.
This kind of dependencies of compiles scopes are needed while building,testing and rurring the project.
Compiles scopes dependencies required in most of the cases to resolve the import statements for java classes in project.

<dependency> <groupid>commons-io</groupid> <artifactid>commons-io</artifactid> <version>2.7</version> </dependency>

4). Test Scope

This types of scopes is not needed while building and running the java project.
They are only needed for testing purpose.The commonly used dependecy with test scopes is junit.We need to test the code functionality using junit.

<dependency> <groupid>junit</groupid> <artifactid>junit</artifactid> <version>4.13</version> <scope>test</scope>

5). System Scope

This types of scopes is similar to the provided scope.In this scope the depndencies are not fteched from the remote repository but from the project itself ,inside other directory or given path.
This kind of scoped dependency jar taken from a path in our local system. We need to setup the path in pom.xml file manually to work.

<dependency> <groupId>com.javacodestuffs</groupId> <artifactId>my-dependency</artifactId> <version>4.5.0</version> <scope>system</scope> <systemPath>${project.basedir}/libs/my-dependency-4.5.0.jar</systemPath> </dependency>

6). Import Scope

This is comparitevy new scope of dependencies in maven.It is introduced in 2.0.9 version of maven. It is used to to import the managed dependencies from other projects.
It is useful for a multi-module maven based projects. To work with import scoped dependencies we need to configure <dependencyManagement> in our projects pom.xml.

<dependencyManagement> <dependencies> <dependency>    <groupId>com.javacodestuffs</groupId> <artifactId>my-dependency</artifactId> <version>4.5.0</version>     <type>pom</type>     <scope>import</scope> </dependency> </dependencies> </dependencyManagement>

7). Transitive Scope

This feature is facilitated by reading the project files of your dependencies from the remote repositories specified.
In general, all dependencies of those projects are used in your project, as are any that the project inherits from its parents, or from its dependencies, and so on.

Transitivity Resolution Rule
  • compile – In this case there is pulls in the transitive dependencies with runtime and compile scope without changing their scope.
  • test – In this case both compile and runtime transitive dependencies are pulled in with the test scope in our project.
  • runtime – In this case it pulls both compile and runtime transitive dependencies with the runtime scope in the project.
  • provided – In this case,both compile and runtime scope dependencies will be pulled in with the provided scope.

Questions related to Scopes for maven dependencies

How to get all dependencies in maven?
How do I add a project as a dependency of another project maven?
How to resolve transitive dependencies in maven?
How to resolve version conflicts in maven dependency?
How to exclude transitive dependencies in maven?
How to exclude a jar from maven dependency?
How to resolve the maven dependency problem in eclipse?

In this article, we have seen the Maven Dependency Scopes  with examples.