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 22 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).

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.

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

-

-

-

compiling a user project may not require dependency

testing implies compilation of the main source

??

if dependency is not used in a particular run, because of dynamic loading in Java, depedency is not needed

if dependency is not used in a particular run, because of dynamic loading in Java, depedency is not needed

-

compile

Download and use dependency in the classpath.

??Download and use dependency in the classpath.

Download and use dependency in the classpath, since testing requires compilation of the main source.

??Download and use dependency in the classpath.

??Download and use dependency in the classpath.

??Download and use dependency in the classpath.

??

test

dependency is not used

dependency is not used

Download and use dependency in the classpath.

dependency is not used

dependency is not used

dependency is not used

??

scope runtime

dependency is not used

dependency is not used

dependency is not used

dependency is not used

??Download and use dependency in the classpath.

??Download and use dependency in the classpath.

?

provided

?

?

?

?

?

?

?

*<optional>true</optional>

?

?

?

?

?

?

?

  • No labels