Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Last Updated: Mar 27, 2024

Various Scopes of Maven Dependency

Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction

Maven is a powerful build management tool. It has dependency management as its key feature. It is easy to manage the dependencies in a simple and small project. But, in multi-module projects, dependencies are complex. Maven helps in managing them in such projects. In this article, you will learn about different Maven dependency scopes. Before learning that, you must have a complete understanding of Maven.

Various scopes of Maven dependency

Maven Classpaths

These are the maven classpaths mentioned below:

  • compile-classpath: Used at the time of compilation of application code.
     
  • test-classpath: Comes into use when testing the application code.
     
  • run-classpath: Used at the time of running or packaging of the application code.
     

Let’s learn these classpaths in detail - 

In a typical Maven project, we have two kinds of source folders:

  • The src-main-java: Contains our application code intended for production runs.
    • The compile-classpath: Useful when Maven compiles our application -code in src-main-java.
       
  • The src-test-java: Contains test classes that are essential to test our application code.
    • Maven uses test-classpath to compile and run the application code in src-test-java.

 

  • The run-classpath: Used at the time of running or packaging our application
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Dependency Scope

Dependency scope tells the scope or stage to which a specific dependency is included. In other words, it limits the transitivity of a Maven dependency. It also determines when it should be in a classpath.

In Maven, a dependency can either be direct or transitive.

  • Direct dependencies- These are defined explicitly in the POM file.
     
  • Transitive dependencies- The direct dependencies require these. These dependencies are automatically included in the Maven project’s classpath. Transitive dependencies are the dependencies of the dependencies which are included in the pom.xml file. 
     

<scope> tag is used in the dependency configuration for defining the scope of Maven dependency. 

Maven provides us with six different scopes. Let’s understand them.

Various Dependency Scopes

Various dependency scopes

compile 

It is the default scope. The compile scope is automatically inferred when we don’t specify any scope. The dependency with compile scope is available in all of our classpaths - compile, test, and run. Therefore, our application code and test class can use any dependency class. It is also available when Maven runs our application.

<dependencies>
	<dependency>
		<groupId>com.codingninjas</groupId>
		<artifactId>spring-context</artifactId>
		<version>1.2.14</version>
		<scope>compile</scope>
	</dependency>
</dependencies>

 

test

The test scope is defined when the dependency is only needed in the test classes. 

<dependencies>
	<dependency>
		<groupId>junit</groupId>
		<artifactId>junit</artifactId>
		<version>4.2</version>
		<scope>test</scope>
	</dependency>
</dependencies>

 

runtime

This dependency scope helps when we need the dependency only when we test and run our application. It is helpful for dynamically linked dependencies that do not directly refer to our code, like JDBC drivers. Therefore, these dependencies are only available on the test and run classpath. They are not available in the compile classpath.

<dependencies>
	<dependency>
		<groupId>mysql</groupId>
		<artifactId>mysql-connector-java</artifactId>
		<version>4.0.6</version>
		<scope>runtime</scope>
	</dependency>
</dependencies>

 

provided

This scope expects the dependency to be available in the runtime. Hence this dependency is only available on the compile and test class paths and not on the run classpath. This scope is very similar to compile. The difference is that provided expects the environment where we run the application already contains these dependencies. For example, almost all java application servers or java web servers have servlet API. Servlet API is the core API that defines the contract between the servlet class and the runtime environment provided for an instance by a conforming servlet container. 

<dependencies>
	<dependency>
		<groupId>javax.servlet</groupId>
		<artifactId>servlet-api</artifactId>
		<version>4.0.1</version>
		<scope>provided</scope>
	</dependency>
</dependencies>

 

system

This scope is similar to provided, except Maven does not manage this dependency. We need to specify where this dependency exists in our local file system using the additional tag, i.e., the system path where we give the path to the dependency.   

<dependencies>
	<dependency>
		<groupId>example</groupId>
		<artifactId>example</artifactId>
		<version>1.0</version>
		<systemPath>${myDirectory}\jars\MyFolder\var\sampleDependency.jar</systemPath>
	</dependency>
</dependencies>


This scope, as mentioned, works similarly to the provided; thus, the dependency is only available on compile and test class paths. 

import

It is a bit different from other scopes. It comes under the dependency management section, allowing us to import all the dependencies configured in the Maven coordinate.

<dependencyManagement>
	<dependencies>
		<dependency>
			<groupId>com.codingninjas</groupId>
			<artifactId>project</artifactId>
			<version>1.1</version>
			<type>pom</pom>
			<scope>import</scope>
		</dependency>
	</dependencies>
</dependencyManagement>


It is reasonable when a team or organization uses a consistent set of dependencies.

Scope and Transitivity

When it comes to scope, we also need to understand how these scopes affect the transitive dependencies. It only works for compiletestruntime, and provided scope dependencies. Since the system scope is not applicable for transitive dependencies, import brings them as they are.

  • compile: Now you all know that compile scope Maven dependency applies to all the class paths - compile, test, and run. It brings along transitive dependencies with scope as compile or runtime, and their scope remains the same.
     
  • test: test scope dependencies are available only on the test classpath. It brings along the transitive dependencies with scope as compile or runtime. Also, these dependencies are marked as the test scope when added to the project.
     
  • runtime: The runtime scope dependencies are available on test and run class paths. It brings along the transitive dependencies that have scope as compile or runtime. Also, these dependencies are marked as the runtime scope when added to the project.
     
  • provided: The provided scope dependencies are available on test and run class paths. It brings along the transitive dependencies that have scope as compile or runtime. On importing these dependencies, they are marked as the provided scope when added to the project.

Frequently Asked Questions

What is Dependency Management?

It is a mechanism for centralizing dependency information. The idea is simple; you can put all the information about the dependency in the common POM when you have a set of projects inherited from a common parent.

What is the role of pom.xml in dependencies?

POM stands for the Project Object Model. It is an XML representation of a Maven project held in a file named pom.xml. It handles all the dependencies in multi-module projects.

How is the import scope dependency different from other scope dependencies?

import scope dependency is included under the <dependencyManagement> section of pom.xml. In contrast, all other scope dependencies get under the <dependency> section of pom.xml.

Where is the Maven dependency stored after downloading?

The dependency downloaded by Maven is placed in the local repository of Maven. The folder .m2 is the default location for the local repository. Use the settings .xml file to change the default location.

What is the need for various scopes of Maven dependency?

Dependency scopes help limit the dependencies’ transitivity. It also modifies the classpath for different build tasks.

Conclusion

In this article, you learned about the scopes of Maven dependency. In a nutshell, now you know that there are six scopes of Maven dependency: compile, runtime, test, import, provided, and system.

Check out the related articles to learn more:

And many more on our platform Coding Ninjas Studio.

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enroll in our courses and refer to the mock test and problems available. Take a look at the interview experiences and interview bundle for placement preparations.

Happy Learning :)

Topics covered
1.
Introduction
2.
Maven Classpaths
3.
Dependency Scope
3.1.
Various Dependency Scopes
3.1.1.
compile 
3.1.2.
test
3.1.3.
runtime
3.1.4.
provided
3.1.5.
system
3.1.6.
import
4.
Scope and Transitivity
5.
Frequently Asked Questions
5.1.
What is Dependency Management?
5.2.
What is the role of pom.xml in dependencies?
5.3.
How is the import scope dependency different from other scope dependencies?
5.4.
Where is the Maven dependency stored after downloading?
5.5.
What is the need for various scopes of Maven dependency?
6.
Conclusion