Dependencies in Dependency Management vs Dependencies in Maven versions plugin

烈酒焚心 提交于 2019-12-21 06:04:11

问题


When I use Maven versions:display-dependency-updates to check dependencies updates, I get two sections of result.

1st:

The following dependencies in Dependency Management have newer versions:

2nd:

The following dependencies in Dependencies have newer versions:

What's the difference between these two?


回答1:


The Dependency section of the POM defines the artifacts (jars, zip etc) upon which your project depends. i.e. the artifacts that it requires to compile, run etc.

The Dependency Management section of the POM is used to manage dependency information.

So for example, in the following pom the JUnit dependency is defined completely in the dependencyManagement section of the POM with version=4.11 and scope = test.

In the dependency section you simply need to define the JUnit dependency using the groupId and artifactId and maven automatically picks up the version and scope from the dependencyManagement section.

<?xml version="1.0" encoding="utf-8"?>
<project>

    ...
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.11</version>
                <scope>test</scope>
            </dependency>
        </dependencies>
     <dependencyManagement>

     <dependencies>
         <dependency>
             <groupId>junit</groupId>
             <artifactId>junit</artifactId>
         </dependency>
     <dependencies>
</project>

Usually you would define the dependencyManagement section in a parent POM, where you define the version and scope for all dependencies. Then in the child module's you simply need to define the dependencies using the groupId and artifactId. This allows you to centrally manage versions and means you only have to update them in one place.

All of this is far better explained in the maven documentation: https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html

The Versions Maven Plugin is simply listing the versions found in each of these sections, as it is possible in the dependencies section to override the version that was defined in the dependencyManagement section.



来源:https://stackoverflow.com/questions/31585387/dependencies-in-dependency-management-vs-dependencies-in-maven-versions-plugin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!