Maven Depedency Scope

Maven dependency scope attribute 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. compileprovidedruntimetestsystem, and import.
Table of Contents

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

Maven dependency scope – compile

This is maven default scope. Dependencies with compile scope are needed to buildtest, and run the project.
Scope compile is to be required in most of the cases to resolve the import statements into your java classes sourcecode.
        <!-- You can ommit this because it is default -->

Maven dependency scope – provided

Maven dependency scope provided is used during build and test the project. They are also required to run, but should not exported, because the dependency will be provided by the runtime, for instance, by servlet container or application server.

Maven dependency scope – runtime

Dependencies with maven dependency scope runtime are not needed to build, but are part of the classpath to test and run the project.

Maven dependency scope – test

Dependencies with maven dependency scope test are not needed to build and run the project. They are needed to compile and run the unit tests.

Maven dependency scope – system

Dependencies with system are similar to ones with scope provided. The only difference is system dependencies are not retrieved from remote repository. They are present under project’s subdirectory and are referred from there. See external dependency for more detail.

Maven dependency scope – import

import scope is only supported on a dependency of type pom in the dependencyManagement section. It indicates the dependency to be replaced with the effective list of dependencies in the specified POM’s dependencyManagement section.

Maven dependency transitivity resolution

When you include a maven dependency and it has it’s own other dependencies (i.e. transitive dependencies) then you may want to be clear about the scope of these transitive dependencies as well.
Let’s understand about maven transitive dependencies with a simple table. In this table, if a dependency is set to the scope in the left columntransitive dependencies at top row will result in a dependency with the scope listed at their intersection.
Drop me your questions in comments section.

Post a Comment

Mới hơn Cũ hơn