2

Assume the following situation: my Maven project depends on a jar A, which depends on 10 other jars which transitively depend on a lot more other jars. I get a huge classpath and if am building a war/ear, I get a huge artifact.

Actually, I am using only the class foo in jar A. The class foo uses a few other classes, which are contained in three other jars. So I really only need jar A and three other jars to compile, not the whole bunch of dependencies (and their dependencies and so on).

Is there a way to (semi-)automatically analyse dependency trees on the class level? As far as I know Maven has no build-in functionality for this.

Just to make this clear: I know that such situations should not occur in a good software architecture. But if I get a jar A which is really just a collection of classes for different purposes, I potentially get a lot of unnecessary dependencies when I build the dependency tree with Maven. And changing A is not something I can do.

J Fabian Meier
  • 33,516
  • 10
  • 64
  • 142
  • You can exclude transitive artifacts from maven dependencies, but only manually. The dependency tree lists those. The quickest way would be to exclude them all and re-introduce them one by one until you project compiles again. Very messy, I know. If someone knows a different way: please post it :D – f1sh Dec 16 '16 at 15:22

1 Answers1

2

Some (long) time ago I've started Maven plugin for this:

https://github.com/highsource/storyteller-maven-plugin

How to find unneccesary dependencies in a maven multi-project?

It works but in no way finished/documented etc. I also don't want to "sell" it here in any way.

But what you write were exactly my thoughts then. maven-storyteller-plugin basically analyzed dependencies of classes and built a huge graph of them. Then it could tell if you actually need dependencies you've declared in your project or not. It could also export nice graphs of dependencies (using GraphViz).

I never had time to finish it, but maybe someone would be interested? Heavylifting is done already.

Community
  • 1
  • 1
lexicore
  • 42,748
  • 17
  • 132
  • 221
  • @JF Meier and lexicore - Just to understand, what's wrong in the practice of including only the dependencies(jar) that are needed for your project in the pom and exclude the rest of transitive ones? And how are you thinking different from `org.apache.maven.plugins:maven-dependency-plugin:3.0.0:analyze` ? – Naman Dec 16 '16 at 16:50
  • 1
    @nullpointer Assume your project A depends on B and B depends on C. But A does not need classes from B which need C. Then A does not need C and it can be excluded. However, you can only understand this by analyzing dependencies A, B and C on the class level. – lexicore Dec 16 '16 at 18:12
  • 1
    Let me try to explain it differently: Assume I build a war A which has a dependency to jar B which has a dependency to jar C. Maven builds the graph A -> B -> C and includes both B and C into my war. dependency:analyze will find out that I really use artifact B, so that the Maven dependency tree is correct. Actually, I only really use one class of B which is entirely independent from C. So everything works fine if I exclude C from my war project, but Maven has no way of knowing this because it only looks at the artifact level, not the class level. – J Fabian Meier Dec 16 '16 at 18:12
  • 1
    @lexicore Interestingly, we constructed the same kind of example simultaniously. – J Fabian Meier Dec 16 '16 at 18:13
  • @JFMeier My thoughts exactly. – lexicore Dec 16 '16 at 18:13
  • @lexicore: I have cloned your project from github. Could you give me a quick hint how to use the plugin? Then I would have a starting point for understanding the structure. – J Fabian Meier Dec 17 '16 at 14:42
  • Can't right now - please file a question issue on Github (so that I don't forget). – lexicore Dec 17 '16 at 15:08