Differences between dependencyManagement and dependencies in Maven

前端 未结 13 1125
北荒
北荒 2020-11-22 04:14

What is the difference between dependencyManagement and dependencies? I have seen the docs at Apache Maven web site. It seems that a dependency def

相关标签:
13条回答
  • 2020-11-22 05:11

    Sorry I am very late to the party.

    Let me try to explain the difference using mvn dependency:tree command

    Consider the below example

    Parent POM - My Project

    <modules>
        <module>app</module>
        <module>data</module>
    </modules>
    
    <dependencies>
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>19.0</version>
        </dependency>
    </dependencies>
    
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.apache.commons</groupId>
                <artifactId>commons-lang3</artifactId>
                <version>3.9</version>
            </dependency>
        </dependencies>
    </dependencyManagement>
    

    Child POM - data module

    <dependencies>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
        </dependency>
    </dependencies>
    

    Child POM - app module (has no extra dependency, so leaving dependencies empty)

     <dependencies>
    </dependencies>
    

    On running mvn dependency:tree command, we get following result

    Scanning for projects...
    ------------------------------------------------------------------------
    Reactor Build Order:
    
    MyProject
    app
    data
    
    ------------------------------------------------------------------------
    Building MyProject 1.0-SNAPSHOT
    ------------------------------------------------------------------------
    
    --- maven-dependency-plugin:2.8:tree (default-cli) @ MyProject ---
    com.iamvickyav:MyProject:pom:1.0-SNAPSHOT
    \- com.google.guava:guava:jar:19.0:compile
    
    ------------------------------------------------------------------------
    Building app 1.0-SNAPSHOT
    ------------------------------------------------------------------------
    
    --- maven-dependency-plugin:2.8:tree (default-cli) @ app ---
    com.iamvickyav:app:jar:1.0-SNAPSHOT
    \- com.google.guava:guava:jar:19.0:compile
    
    ------------------------------------------------------------------------
    Building data 1.0-SNAPSHOT
    ------------------------------------------------------------------------
    
    --- maven-dependency-plugin:2.8:tree (default-cli) @ data ---
    com.iamvickyav:data:jar:1.0-SNAPSHOT
    +- org.apache.commons:commons-lang3:jar:3.9:compile
    \- com.google.guava:guava:jar:19.0:compile
    

    Google guava is listed as dependency in every module (including parent), whereas the apache commons is listed as dependency only in data module (not even in parent module)

    0 讨论(0)
提交回复
热议问题