kan
kan

Reputation: 28951

Maven brings "test" transitive dependency as "compile"

When I run "mvn dependency:tree" for my project it shows the following:

[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ xxxxx ---
[INFO] com.xxx.xxx:xxxxx:war:3.1.0-SNAPSHOT
...
[INFO] +- commons-configuration:commons-configuration:jar:1.5:compile
[INFO] |  \- commons-beanutils:commons-beanutils-core:jar:1.7.0:compile
[INFO] +- org.seleniumhq.selenium:selenium-api:jar:2.34.0:test
[INFO] |  +- com.google.guava:guava:jar:14.0:test
[INFO] |  \- org.json:json:jar:20080701:test
[INFO] +- org.seleniumhq.selenium:selenium-htmlunit-driver:jar:2.34.0:test
[INFO] |  +- org.seleniumhq.selenium:selenium-remote-driver:jar:2.34.0:test
[INFO] |  |  +- cglib:cglib-nodep:jar:2.1_3:test
[INFO] |  |  +- net.java.dev.jna:jna:jar:3.4.0:test
[INFO] |  |  \- net.java.dev.jna:platform:jar:3.4.0:test
[INFO] |  \- net.sourceforge.htmlunit:htmlunit:jar:2.12:test
[INFO] |     +- org.apache.commons:commons-lang3:jar:3.1:test
[INFO] |     +- org.apache.httpcomponents:httpmime:jar:4.2.3:test
[INFO] |     +- net.sourceforge.htmlunit:htmlunit-core-js:jar:2.12:test
[INFO] |     +- xerces:xercesImpl:jar:2.10.0:test
>>>[INFO] |     |  \- xml-apis:xml-apis:jar:1.4.01:compile
[INFO] |     +- net.sourceforge.nekohtml:nekohtml:jar:1.9.18:test
[INFO] |     +- net.sourceforge.cssparser:cssparser:jar:0.9.9:test
[INFO] |     |  \- org.w3c.css:sac:jar:1.3:test
[INFO] |     \- org.eclipse.jetty:jetty-websocket:jar:8.1.9.v20130131:test
[INFO] +- org.seleniumhq.selenium:selenium-firefox-driver:jar:2.34.0:test
...

As you see on the marked line, the xml-apis has "compile" scope, and as result it is packed into .war file. Why could it happen?

More interestingly it happens only while Java5 is used, for Java6 the dependency appears as "test".

Maven version: 3.0.4

Upvotes: 11

Views: 3638

Answers (3)

klr8
klr8

Reputation: 665

Study the output of the following Maven command.

mvn -X dependency:tree -Dverbose

That should tell you why Maven upgraded the scope from test to compile.

Upvotes: 8

Christoph Dietze
Christoph Dietze

Reputation: 902

I had a similar problem.

In my case an entry in the dependencyManagement of a parent pom set the scope of the dependent artefact to compile. Actually I omitted the scope tag which effectively is the same as setting it to compile. Changing it to provided helped. Seems the scope in dependencyManagement takes precedence over the transitive scope. Which makes sense but can still cause confusion when all you wanted to do is define the version.

It was actually not hard to spot: Looking at the effective-pom showed the dependencyManagement entry.

Upvotes: 4

khmarbaise
khmarbaise

Reputation: 97359

If you take a look at xercesImpl it contains a dependency to xml-apis:xml-apis:jar:1.4.01:compile with the scope compile so the display of dependency plugin is correct. The usage of -Dverbose will do things as written in the docs:

Whether to include omitted nodes in the serialized dependency tree.

Apart from the above a test dependency as in your case is never being packaged into a war file.

There must be an other source of the same dependency which causes the packaging into the war

Furthermore the change in behaviour in relationship with adding explicit xml-apis to your pom is a supplemental evidence for this.

Upvotes: 3

Related Questions