Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mvn dependency:tree fails on trivial project

I've got a pretty complex project (about 100 modules) on which I'd like to run mvn dependency:tree. It fails, complaining about dependencies it cannot resolve. The project otherwise compiles fine. So I created the most basic project I could come up and it still fails with the same error. Obviously either I must be doing some very basic mistake or the maven-dependency-plugin has not been used by anyone yet. Here are the three POMs im by test project:

pom.xml:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>root</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>pom</packaging>
    <modules>
        <module>foo</module>
        <module>bar</module>
    </modules>
</project>

foo/pom.xml:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>foo</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>jar</packaging>
</project>

bar/pom.xml:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>bar</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <dependencies>
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>foo</artifactId>
            <version>1.0.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

Then I issue the following command mvn dependency:tree in the top-level directory and get the following output:

[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO] 
[INFO] foo
[INFO] bar
[INFO] root
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building foo 1.0.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ foo ---
[INFO] com.example:foo:jar:1.0.0-SNAPSHOT
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building bar 1.0.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO] 
[INFO] foo ................................................ SUCCESS [  0.756 s]
[INFO] bar ................................................ FAILURE [  0.011 s]
[INFO] root ............................................... SKIPPED
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.065 s
[INFO] Finished at: 2015-03-03T16:19:18+01:00
[INFO] Final Memory: 13M/309M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal on project bar: Could not resolve dependencies for project com.example:bar:jar:1.0.0-SNAPSHOT: Could not find artifact com.example:foo:jar:1.0.0-SNAPSHOT -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/DependencyResolutionException
[ERROR] 
[ERROR] After correcting the problems, you can resume the build with the command
[ERROR]   mvn <goals> -rf :bar

What am I missing? Shouldn't this just work?

like image 747
Feuermurmel Avatar asked Mar 03 '15 15:03

Feuermurmel


People also ask

What does mvn dependency tree do?

In Maven, you can use mvn dependency:tree to display the project dependencies in tree format.

How do I run a dependency tree in Maven?

How to get the Maven Dependency Tree of a Project. We can run mvn dependency:tree command in the terminal to print the project dependency tree. For our example, I will be using the Mockito Tutorial project. You can download the project from the GitHub repository.

How do I run a Maven dependency tree in IntelliJ?

In the POM, right-click anywhere in the editor to open the context menu and select Maven | Show Dependencies. Alternatively, press Ctrl+Alt+Shift+U or Ctrl+Alt+U . In the diagram window, IntelliJ IDEA displays the sub project and all its dependencies including the transitive ones.

What happens if Maven dependency version is not specified?

Each maven dependency defined in the pom must have a version either directly or indirectly for example, through dependencyManagement or parent. That being said, if the version is not given, then the version provided in the dependencyManagement or the parent pom will be used.


2 Answers

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.

like image 87
William Leung Avatar answered Oct 21 '22 12:10

William Leung


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.

like image 21
gizmo Avatar answered Oct 21 '22 12:10

gizmo