maven工程<dependencyManagement>与<dependencies>区别

来源:互联网 发布:nbiot网络架构 编辑:程序博客网 时间:2024/06/04 23:55

Maven 使用dependencyManagement 元素来提供了一种管理依赖版本号的方式。通常会在一个组织或者项目的最顶层的父POM 中看到dependencyManagement 元素。使用pom.xml 中的dependencyManagement 元素能让
所有在子项目中引用一个依赖而不用显式的列出版本号。Maven 会沿着父子层次向上走,直到找到一个拥有dependencyManagement 元素的项目,然后它就会使用在这个dependencyManagement 元素中指定的版本号。

  • stackoverflow

    In the parent POM, the main difference between the and is this:
    Artifacts specified in the section will ALWAYS be included as a dependency of the child module(s).
    Artifacts specified in the section, will only be included in the child module if they were also specified in the section of the child module itself. Why is it good you ask? because you specify the version and/or scope in the parent, and you can leave them out when specifying the dependencies in the child POM. This can help you use unified versions for dependencies for child modules, without specifying the version in each child module.

  • e.g

<!-- ParentProj pom --><project>  <dependencyManagement>    <dependencies>      <dependency> <!-- not much benefit defining alpha here, as we only use in 1 child, so optional -->        <groupId>alpha</groupId>        <artifactId>alpha</artifactId>        <version>1.0</version>        <exclusions>          <exclusion>            <groupId>zebra</groupId>            <artifactId>zebra</artifactId>          </exclusion>        </exclusions>      </dependency>      <dependency>        <groupId>charlie</groupId> <!-- not much benefit defining charlie here, so optional -->        <artifactId>charlie</artifactId>        <version>1.0</version>        <type>war</type>        <scope>runtime</scope>      </dependency>      <dependency> <!-- defining betaShared here makes a lot of sense -->        <groupId>betaShared</groupId>        <artifactId>betaShared</artifactId>        <version>1.0</version>        <type>bar</type>        <scope>runtime</scope>      </dependency>    </dependencies>  </dependencyManagement></project><!-- Child Proj1 pom --><project>  <dependencies>    <dependency>      <groupId>alpha</groupId>      <artifactId>alpha</artifactId>  <!-- jar type IS DEFAULT, so no need to specify in child projects -->    </dependency>    <dependency>      <groupId>betaShared</groupId>      <artifactId>betaShared</artifactId>      <type>bar</type> <!-- This is not a jar dependency, so we must specify type. -->    </dependency>  </dependencies></project><!-- Child Proj2 --><project>  <dependencies>    <dependency>      <groupId>charlie</groupId>      <artifactId>charlie</artifactId>      <type>war</type> <!-- This is not a jar dependency, so we must specify type. -->    </dependency>    <dependency>      <groupId>betaShared</groupId>       <artifactId>betaShared</artifactId>       <type>bar</type> <!-- This is not a jar dependency, so we must specify type. -->    </dependency>  </dependencies></project>
阅读全文
0 0
原创粉丝点击