This page is started because I (the first author) don't understand the dependency scope mechanism. In the first version of this page, I am trying to make sense of different sources about the topic. This means the information here is not correct per definition! The intention of this page is to get it correct and clear, so please, if you do know something about dependency scopes, change the nonsense below (wink).

Introduction to the Dependency Mechanism

Dependencies are defined in the POM, and can be resolved transitively. However, you don't need all dependencies in all situations. That is why dependecies can have a scope defined. Furthermore, there is a tag <optional>true</optional> you can use for a dependency.

In the following table, we explain the behavior of each scope for different goals. We talk about the current project, the dependency and a user project. The current project is the project whose POM we are editing. The dependency is a project the user project directly depends on. A user project is a project for which the current project is a direct depency. Consequentially, a user project has an indirect depency on the dependency. We also presume a maven2-based mechanism (goal) to run final projects.

The following lifecycle phases are important:

Furthermore, it is important to see that some dependencies that are required for compilation (and testing), are optional for using the artifact on a user project or for running it if it is a final project itself. Examples are the dependency of commons-logging on log4j or of hibernate on ehcache and c3po and others. These projects or coded in such a way that they detect whether or not a library is available in the classpath, and use it if it is, but it is not necessary.

Also, indirect dependencies are not necessarily needed for compiling a user project: the current project might depend on the dependency internally, but have no mentioning of the dependency in its API that is used by the user project. Thus, dependency is not needed for compiling the user project, but it might be for running the final project.

The following dependency scopes are supported:

Legend:

For the current project:

scope/phase -->

compile

test

run

assembly

compile

U

U

U

U

test

!

U

!

!

runtime

!

U

U

U

provided

U

!

!

!

For a user project that has the current project as a dependency with scope compile:

scope/phase -->

compile

test

run

assembly

compile

U!O

U!O

U!O

U!O

test

!

U

!

!

runtime

!

U!O

U!O

U!O

provided

U!O

!

!

!

For a user project that has the current project as a dependency with scope test:

??

For a user project that has the current project as a dependency with scope runtime:

??

For a user project that has the current project as a dependency with scope provided:

??