mvn dependency:tree fails on trivial project

I'm frustrated why it didn't work.
A simple dependency analysis just didn't work.
And the official has no any guide how to do it.
Another much usefully command didn't work neither

mvn dependency:resolve

but, maybe you could try these commands instead

mvn test-compile dependency:resolve
mvn test-compile dependency:tree

anyway, it works for me


Update on Mar 13 2017

We can make it much more faster by skipping the compilation

 mvn test-compile dependency:resolve -Dmaven.main.skip=true -Dmaven.test.skip=true
 mvn test-compile dependency:tree    -Dmaven.main.skip=true -Dmaven.test.skip=true

So sad it didn't work for our project because our project was using kotlin, maybe it is kotlin's bug which didn't skip the compilation, maybe I should report this bug to jetbrains.


Ok, let's make a proper answer as the comments are a bit too short for a correct explanation.

Maven is a tool of various combined facets and it is sometime difficult to identify which parts are playing a role for a given command.

In your example, you have two classical projects, foo and bar and a special one, root.
root is special in the sense that it plays, in your example two roles.

  • The first one is called parent pom. It is usually used to fix the dependencies and plugins versions, as well as some common configuration that need to cascade to child projects. It has also a, sometime usefull, property in that the child projects inherit of the version unless explicitly specified in the child projects themselfs.
  • The second role is called reactor pom. This is the part mainly defined in the <modules> tag. It defines a set of other projects that to which a command can be sent as a group (e.g.: mvn clean install). When such command is executed, maven will look at the projects described in the <modules> and, based on their declared dependencies, will identify an order in which they have to called with the given command in order to maximize the chance of the build to succeed.

Now, about the behaviour of the various commands you tried (assuming they were all called on the root project:

  • mvn dependency:tree will perform a dependency analysis on all the project listed in the <modules> tag plus itself. This analysis is performed against the repository, meaning your local .m2 repo, and other external repository when needed. If you did not install your projects first in your repository, it will fail on bar as it cannot found com.example:foo:1.0.0-SNAPSHOT in there.
  • mvn [clean] install, using the same order, will perform a full packaging and deployment of your projects into your local repository. As bar will be executed after foo has been put into your repository, all will be fine and everybody will be happy.

But what abouth the mvn compile that works then?

Well, your case is a bit tricky. As your sample does not have actual code to compile, the dependency resolution for the compilation is skipped, and thus, no error occurs while your foo artifact is not available yet.

Now, about your remarks about branching and artifacts in your repo...
When you switch to a branch, unless all the other projects (modules) are using stable version number (i.e. without the SNAPSHOT suffix), you should perform a mvn [clean] install run on your reactor pom to ensure that you start working with a coherent set of modules and dependencies.
You might think that this is a waste of time, compared to e.g. interpreted languages, but it is the MAVEN way to handle projects. It ensures that all your modules are aligned before you start working.