Maven artifact scopes revisited

In a prior post about when to use which scope for a Maven artifact, I left out the provided scope, because, as I said at the time, “I don’t get as confused about that one so I left it out.” I spoke too soon.  : )

When I said that, I was only thinking of which classpath(s) the artifact would available to in its immediate project.  But there are two additional concerns besides the classpath availability: whether the artifact will be built into the resulting (.war) target, and whether artifacts depending on this one will inherit the dependency.  The latter issue is also affected by the <optional> tag.

The following table reflects my current understanding of how the scope and optionalness of a dependency affect:

  • The classpath(s) in which the artifact is available;
  • Whether the artifact is included in the war; and
  • When artifact A declares a dependency on artifact D: whether artifacts declaring a dependency on artifact A will also transitively get a dependency on artifact D.
Scope Optional? Classpaths in which artifact is available Artifact included in war** Dependency propagates transitively
(main) compile (main) runtime test*
compile No X X X X X
compile Yes X X X X
runtime No X X* X X
runtime Yes X X* X
test No X ? X
test Yes X ?
provided No X X
provided Yes X X
system No X X
system Yes X X

*Note that the “test” classpath is used both at test compile time and test runtime; it doesn’t seem to  be possible to specify that an artifact should be available at test runtime but not test compile time.

**I don’t think it has to necessarily be packaged as war… trying to make a concrete example.


One thought on “Maven artifact scopes revisited

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s