Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find which dependency is pulling in a particular class file?

My project consists of some dependencies which are pulling the same common dependency.

The common dependency storm-kafka has a new version 1.0.2 and an old version 0.10.0

On building a shaded jar, I see classes from both the versions in my fat jar and somehow during execution, the older one is getting picked up which gives a ClassNotFoundError because other dependencies related to the older version are not there.

jar -xvf my_shaded_fat_jar.jar
find . -name KeyValueSchemeAsMultiScheme.class
./org/apache/storm/kafka/KeyValueSchemeAsMultiScheme.class
./storm/kafka/KeyValueSchemeAsMultiScheme.class

storm/kafka is older one and org/apache/storm/kafka is the new one I want.

Surprising part is that I do not see 0.10.0 in my ~/.m2 repo:

ls ~/.m2/repository/org/apache/storm/storm-kafka/
1.0.2
# no 0.10.0 here !

How do I debug maven to find out from where the older dependency is creeping into?

EDIT:

On running mvn dependency:tree -Dverbose -Dincludes=storm.kafka, I get:

[WARNING] The POM for org.apache.storm:flux-core:jar:1.0.2 is invalid, transitive dependencies (if any) will not be available, enable debug logging for more details
[WARNING] The POM for org.apache.storm:storm-kafka:jar:1.0.2 is invalid, transitive dependencies (if any) will not be available, enable debug logging for more details
[INFO] 
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli)  ---
[WARNING] Failed to build parent project for org.apache.storm:flux-core:jar:1.0.2
[WARNING] Invalid POM for org.apache.storm:flux-core:jar:1.0.2, transitive dependencies (if any) will not be available, enable debug logging for more details
[WARNING] Invalid POM for org.apache.storm:storm-kafka:jar:1.0.2, transitive dependencies (if any) will not be available, enable debug logging for more details
[WARNING] Failed to build parent project for org.apache.storm:flux-core:jar:1.0.2
[WARNING] Invalid POM for org.apache.storm:flux-core:jar:1.0.2, transitive dependencies (if any) will not be available, enable debug logging for more details

Why the poms are invalid? I just cleaned my .m2 repo by removing everything in ~/.m2/repository/org/apache/storm

like image 513
user2250246 Avatar asked Sep 13 '16 01:09

user2250246


People also ask

How can I see the dependencies in a jar file?

jar file. Use the -verbose:class option to find class-level dependencies or use the -v or -verbose option to include dependencies from the same JAR file.

How do you find transitive dependencies?

First go to View → Tool Windows → Maven, to make sure that the Maven window is visible. The top-level elements in the tree are your direct dependencies, and the child elements are the transitive dependencies.

Where can I find dependencies in Maven?

In your project's POM, press Ctrl and hover the mouse over the dependency. Click the dependency to open the dependency's POM. In the dependency POM, view the active dependency, its transitive dependencies and their versions. You can check the origin from which the dependency was pulled in.

How do you analyze a dependency tree in Maven?

Find it using maven infrastructure. Go to https://search.maven.org/ and enter your groupId and artifactId. Or you can go to https://repo1.maven.org/maven2/ and navigate first using plugins groupId, later using artifactId and finally using its version.


2 Answers

mvn dependency:build-classpath -Dmdep.outputFile=f

gives a list of all dependend jars.

now iterate of them and add their content into a file:

for f in `cat f | tr ';:' '  '`
do
    echo $f >> out
    unzip -v $f >> out
done
like image 171
weberjn Avatar answered Sep 17 '22 16:09

weberjn


You can use the Maven tree goal to show all dependencies that are used. This prints out the full dep tree by default but you can also get it to find a particular dependency you're interested in - for example try:

mvn dependency:tree -Dverbose -Dincludes=storm.kafka

to see what's pulling in Kafka. More info can be found here:

http://maven.apache.org/plugins/maven-dependency-plugin/examples/resolving-conflicts-using-the-dependency-tree.html

like image 34
adamskoye Avatar answered Sep 16 '22 16:09

adamskoye