Maven POM中的dependencyManagement

dependencyManagement

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

<?xml version="1.0" encoding="UTF-8"?>
<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.gp</groupId>
    <artifactId>test</artifactId>
    <version>framework-develop</version>
    <packaging>pom</packaging>
    <description>定义公共信息及依赖版本管理</description>
    
    <modules>
        <module>core</module>
        <module>dao</module>
        <module>service</module>
        <module>example</module>
        <module>search</module>
    </modules>

    <properties>
        <url.nexus.server>http://123.57.51.101:8081</url.nexus.server>
       
        <!--test -->
        <junit.version>4.12</junit.version>
        <mockito.version>1.10.19</mockito.version>
    </properties>

    <dependencies>
        <!--test 此处为引入,并默认被所有的子项目继承,可以不指定版本号-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency> 
    </dependencies>

    <dependencyManagement>
        <dependencies>

            <!--test   此处只申明不引入-->
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>${junit.version}</version>
                <scope>test</scope>
            </dependency>
          
        </dependencies>
    </dependencyManagement>
<!--分发构件至远程仓库-->
    <distributionManagement>
        <repository>
            <id>nexus-releases</id>
            <url>${url.nexus.prefix}/repositories/releases</url>
        </repository>
        <snapshotRepository>
            <id>nexus-snapshots</id>
            <url>${url.nexus.prefix}/repositories/snapshots</url>
        </snapshotRepository>
    </distributionManagement>

</project>


这样做的好处就是:如果有多个子项目都引用同一样依赖,则可以避免在每个使用的子项目里都声明一个版本号,这样当想升级或切换到另一个版本时,只需要在顶层父容器里更新,而不需要一个一个子项目的修改 ;另外如果某个子项目需要另外的一个版本,只需要声明version就可。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值