A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/advance-java/maven-dependency-scopes/ below:

Maven Dependency Scopes - GeeksforGeeks

Maven Dependency Scopes

Last Updated : 23 Jul, 2025

Maven Dependency scopes are used to specify the visibility and life cycle of a dependency in a Maven project. In Maven we have six different dependency scopes. Below we explain them by using a pom.xml file.

Compile Scope:

This is the default scope in the Maven. Dependencies with this scope are available in all class paths like compile, test, and runtime and are also packaged in the final artifact like JAR, WAR, and other formats.

Example:

<!-- compile scope -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>

Provided Scope:

Dependencies with this scope are required for compiling the project code but are expected to be provided by the runtime environment. These Dependencies are not packaged with artifact.

Example:

<!-- provided scope -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
Runtime Scope:

Dependencies with this scope are need for execution of the source code but are not required for compilation. These dependencies are not included in the compilation classpath but are included in the runtime classpath and also these are packaged in the final artifact.

Example:

<!-- runtime scope -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.24</version>
<scope>runtime</scope>
</dependency>
Test Scope:

Dependencies with this scope are only needed for compiling and running tests, not for the production code.

<!-- test scope -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
System Scope:

Dependencies with this scope are not retrieved from the Maven repository but are referenced from the local system. This scope is generally not recommended because it bypasses Maven's dependency management.

Example:

<!-- system scope -->
<dependency>
<groupId>com.example</groupId>
<artifactId>custom-jar</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/custom-jar.jar</systemPath>
</dependency>
Import Scope:

This scope is used only in Maven 2.0.9 and above. It is used in a pom's dependency Management section to allow you to import dependency management information from other POM files into the current project.

Example:

<!-- Parent POM -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.3.9</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.3.9</version>
</dependency>
</dependencies>
</dependencyManagement>
Tools And Technologies:

Note: Before going to this article you should knowledge on maven project creation and It's build life cycle then only you can understand this concept in right way. And you must haven maven in your local system.

Example of Maven Dependency Scopes

Here, we created a sample maven project then write required source code. After that, we run above commends on this created maven project.

Dependencies:
<dependencies>
<!-- compile scope -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>

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

<!-- runtime scope -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.24</version>
<scope>runtime</scope>
</dependency>

<!-- test scope -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>

<!-- system scope -->
<dependency>
<groupId>com.example</groupId>
<artifactId>custom-jar</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/custom-jar.jar</systemPath>
</dependency>
</dependencies>
Project Folder: pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="https://maven.apache.org/POM/4.0.0" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.6</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.app</groupId>
<artifactId>mavencommends</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>mavencommends</name>
<description>Spring Reactive</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<!-- compile scope -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>

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

<!-- runtime scope -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.24</version>
<scope>runtime</scope>
</dependency>

<!-- test scope -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>

<!-- system scope -->
<dependency>
<groupId>com.example</groupId>
<artifactId>custom-jar</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/custom-jar.jar</systemPath>
</dependency>
</dependencies>

<build>


<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>


Project Running

Once Project is successfully created then run this project as Spring Boot App.



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4