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
- Provided Scope
- Runtime Scope
- Test Scope
- System Scope
- Import Scope
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:
- Java programming
- Maven
- Spring Tool Suite
- Commend Prompt
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="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://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.
|