Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 34 Next »

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 are 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 or goals are important:

  • compile: Compile the main source.
  • test: Compile the test source and run the tests. This requires the main source to be compiled (the test goal depends on the compile goal).
  • run: (non-existing) goal that runs a final artifact. Obviously, this requires the main sources to be compiled (the run goal depends on the compile goal).
  • assembly: Create an assembly of all kinds off stuff around the artifact. Amongst others, this might contain a lib-directory that contains external libraries the current project depends on.

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 scopes are supported:

  • compile: This dependency is needed for compilation of the main source
  • test: This dependency is needed for compiling and running tests. It is not needed for compiling the main source or running the final artifact.
  • runtime: This dependency is needed for running the final artifact. It is not needed for compiling the main source or compiling or running the tests.
  • provided: ??
  • tag <optional />

scope

compile current project

compile user project

test current project

test user project

run current project

run user project

assembly current project

assembly user project

compile

U

U!O

U

U!O

U

U!O

U

U!O

test

!

!

U

!

!

!

!

!

runtime

!

!

!

!

U

U!O

U

U!O

provided

U

!

!

!

!

!

!

!

Legend:

  • U: Download and use dependency in the classpath. / Dowload and include dependency in the assembly.
  • U!O: Download and use dependency in the classpath, unless the dependency is <optional />. / Dowload and include dependency in the assembly, unless the dependency is <optional />.
  • !: dependency is not used
  • No labels