brain storm
brain storm

Reputation: 31272

maven dependency tree showing dependency that is not present in parent jar?

I ran maven dependency:tree on my module. Here is the snippet of this output.

[INFO] +- org.springframework:spring-webmvc:jar:4.0.2.RELEASE:compile
[INFO] |  +- org.springframework:spring-beans:jar:4.0.2.RELEASE:compile
[INFO] |  +- org.springframework:spring-expression:jar:4.0.2.RELEASE:compile
[INFO] |  \- org.springframework:spring-web:jar:4.0.2.RELEASE:compile
[INFO] +- javax.servlet:javax.servlet-api:jar:3.0.1:provided
[INFO] +- org.apache.httpcomponents:httpcore:jar:4.3.2:compile
[INFO] +- org.apache.httpcomponents:httpclient:jar:4.3.2:compile
[INFO] |  \- commons-codec:commons-codec:jar:1.6:compile
[INFO] +- org.apache.commons:commons-lang3:jar:3.2.1:compile
[INFO] +- commons-lang:commons-lang:jar:2.6:compile
[INFO] +- com.fasterxml.jackson.core:jackson-core:jar:2.5.0:compile
[INFO] +- com.fasterxml.jackson.core:jackson-annotations:jar:2.5.0:compile
[INFO] +- com.fasterxml.jackson.core:jackson-databind:jar:2.5.0:compile
[INFO] +- com.newrelic.agent.java:newrelic-api:jar:3.6.0:compile

I see transitive dependency to commons-codec:commons-codec:jar:1.6 from org.apache.httpcomponents:httpclient:jar:4.3.2.

but I could not find this jar inside $M2_HOME/org/apache/httpcomponents/httpclient/4.3.2/httpclient-4.3.2.jar.

I also did NOT see this jar located in 'referenced Libraries' or 'maven dependencies' in Eclipse. My project Base64Util class from commons-codec and it compiles fine.

Upvotes: 2

Views: 1927

Answers (1)

Peter Tillemans
Peter Tillemans

Reputation: 35341

The dependency is defined in the POM as you can see here :

http://central.maven.org/maven2/org/apache/httpcomponents/httpclient/4.3.2/httpclient-4.3.2.pom

There is a compile dependency to commons-codec. It has a parent pom reference to httpcomponents-client, which defines the version as 1.6 as can be seen here:

http://central.maven.org/maven2/org/apache/httpcomponents/httpcomponents-client/4.3.2/httpcomponents-client-4.3.2.pom

So the maven output is correct and the library will be provided. Now if you use a class from this library in your code, I would recommend to have an explicit dependency in your pom.

Maven builds this tree of dependencies to see which libraries are needed, as each jar is only containing its own classes and not the dependencies. Maven will verify that if multiple libraries ask the same dependency that a version is used which will work with both libraries. Sometimes it can't find a solution and will throw its hands in the air with a dependency resolution error. When it has the list of libraries it will download them and put them on the java classpath for compilation, etc...

This is needed as the normal java classloader puts all classes in the same namespace. This is not always the case, e.g. servlet containers make a classloader per war, osgi containers one per bundle, however all these systems come at a price : weird ClassCast exceptions because a class is loaded in multiple classloaders. That is the reason that jars do not contain their dependencies in a selfcontained uberjar.

Upvotes: 1

Related Questions