0

This is an interesting state of affairs in Maven that I didn't expect. Maybe someone can explain exactly why this is happening.

I have a parent POM foobar-parent that declares logback-classic with a test scope in the <dependencyManagement> section.

I have a separate project project example that has its own example-parent, which inherits from foobar-parent and also serves as a parent to its submodules.

One submodule example-foo overrides the dependency logback-classic and gives it compile scope:

<dependency>
  <groupId>ch.qos.logback</groupId>
  <artifactId>logback-classic</artifactId>
  <scope>compile</scope>
</dependency>

Lastly I have another submodule example-bar which uses example-foo as a dependency.

Strangely, for the effective POM of example-bar, it shows that logback-classic has test scope!! Since example-foo declares logback-classic to be of compile scope (meaning it is required at compile time), and since example-bar has a compile-time dependency to example-foo, I expected example-bar to bring in logback-classic as a transitive dependency.

The way I interpret this is that the test scope specified in the <dependencyManagement> management section of a parent POM will override the scope of a transitive dependency from the compile scope!! Is this a correct interpretation, and is that how Maven is supposed to work?

Garret Wilson
  • 18,219
  • 30
  • 144
  • 272
  • Just make those dependency as `dependencyManagement` in `foo` parent pom insteed of `dependencies` – Antoniossss Oct 28 '18 at 19:17
  • IMO, best practice is to use `dependencyManagement` only for version, not for scope. That way, you avoid unpleasant surprises. A transitive dependency will be pulled in with the correct scope, and if you omit scope for a direct dependency it will have compile scope as expected. See also this related SO post: https://stackoverflow.com/questions/47394837/is-it-good-practice-to-define-scope-of-dependencies-in-maven-bom-bill-of-materi/47402262#47402262 – gjoranv Oct 28 '18 at 19:42
  • Sometimes scope sohuld be included - eg for Lombok – Antoniossss Oct 28 '18 at 21:33

1 Answers1

2

You are right: "dependency management takes precedence over dependency mediation for transitive dependencies" (taken from Introduction to the Dependency Mechanism)

  • Thanks for the reference! This may have even been something I had read earlier but it's not exactly intuitive so maybe I forgot. I'm not saying it's nonintuitive, but… at least unexpected. Cheers! – Garret Wilson Oct 28 '18 at 22:06