0

I'm trying to run some unit tests with Apache Maven. I hoped this would be as simple as running the test "goal". But when I did that, maven complained that it could not download some dependencies and thus can't run my tests. This sounds fine, except that I have no idea why it decided I need those dependencies; they are not in my pom.xml, and I doubt they're in my transitive dependencies either. (I'm not sure about that last part; they very well might be in my transitive dependencies.)

Luckily, maven has the perfect tool for this: dependency:tree will tell us exactly which dependency is getting pulled in by what. Except for the small problem that maven thinks to itself "in order to build the tree, I have to resolve the dependencies first" so it tries (and fails) to download those very same dependencies so that it can build the part of the tree that's under them.

So now I don't have a tree, and I have no idea how to proceed from here.

Mark VY
  • 1,489
  • 16
  • 31
  • note: on further reflection, I think this question is poorly titled; if anyone has suggestions for better title I'm eager to hear them – Mark VY Aug 18 '22 at 17:47
  • Before you can run your build all dependencies (incl. plugins + deps) have to be downloaded... – khmarbaise Aug 18 '22 at 18:10
  • I'm not trying to do a "real" build. I'm just trying to find a way to ask maven "why do you claim that I depend on module ABC, even though ABC is not in my pom file". – Mark VY Aug 18 '22 at 18:23

2 Answers2

0

How exactly would you think that maven could resolve transitive dependencies (= dependencies of dependencies) without resolving the dependencies first? Escpecially for the goal "test" also the dependency scope "test" has to be used, which is more then the default scope "compile".

You can use the goal dependency:go-offline to prepare for the offline mode. Maven downloads then all required dependencies. Find the detailed docs for that on https://maven.apache.org/plugins/maven-dependency-plugin/go-offline-mojo.html

You could also have a look at this answer to get another opinion on going online.

cyberbrain
  • 3,433
  • 1
  • 12
  • 22
  • re: "how could maven resolve transitive deps without first resolving immediate ones?". Indeed, that's obviously impossible. However, that's NOT what I ask of it. All I want is the "shallow" part of the tree. In other words, maven thinks that my project depends on ABC. In order to print the full tree, it needs to download ABC, or at the very least get its POM file. But I don't want to know the full tree. All I want to know is the part of the tree that led maven to conclude that it needs to go fetch ABC. This obviously is possible in principle. Can it be done in practice? – Mark VY Aug 18 '22 at 17:40
  • Then I probably misunderstood you - and I don't think what you want is actually achievable because there could be multiple artifacts that require one specific other artifact that is not cached in your local repo. Also maven sorts out which version of a dependency to take depending on all requirements for that dependency - without knowing _all_ of them, it can't know the final tree. Of course it could look what is currently missing, but how would that help you? You would miss other missing dependencies... – cyberbrain Aug 18 '22 at 20:26
  • It would help me because then I could understand what's going on! I should not be depending on that artifact! It means there's likely a bug in my pom file. I want to know why maven decided that I need it. – Mark VY Aug 18 '22 at 20:30
  • I can't test it at the moment in your constellation (with a missing transitive dependency), but did you try the maven option `-X` to see if debug logs contain anything helpful? – cyberbrain Aug 18 '22 at 20:43
0

The main problem is maven downloads dependencies by demand, you may just check that by triggering different lifecycle phases like mvn initialize, mvn validate, mvn compile, mvn package and checking what maven is trying to download. Sometimes it is possible to figure out project dependencies via analysing project object model (pom), sometimes it is not, especially when plugins define their own dependencies either implicitly or explicitly, some examples below:

In short: neither maven plugin will able to download all required dependencies. The only "reliable" way to go offline is to run target goal and only then go offline, unfortunately even in this cases some weird things may happen, especially when you or dependency authors are using snapshot versions, version ranges, third-party repositories, etc (my own preference is to run maven with -llr flag to make it more reliable).

Andrey B. Panfilov
  • 4,324
  • 2
  • 12
  • 18
  • Sorry, it seems that my question is poorly titled in such a way as to cause everyone to misread everything else I wrote. All I want is for maven to answer the following simple question: "why do you claim that my project depends on artifact ABC? what's pulling it in?" – Mark VY Aug 19 '22 at 12:48