(X)依赖管理

通过Maven聚合和继承章节中POM可继承的元素我们看到有dependencies,说明依赖是会被继承的。Maven的dependencyManagement元素既能让子模块继承父模块的依赖配置,又能保证子模块依赖使用的灵活性。

dependencyManagement元素下的依赖声明不会引入实际的依赖,不过可以约束dependencies下的依赖使用。
我们在parent模块中添加dependencyManagement配置:

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <springframework.version>5.0.5.RELEASE</springframework.version>
</properties>
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${springframework.version}</version>
        </dependency>
        <!-- Unit Test -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
  • 在properties中声明springframework.version,可以同时为springframework组中相应构件提供一致的版本信息,减少了重复。
  • dependencyManagement声明的依赖不会给parent模块引入依赖,也不会给它的子模块引入依赖。但是这段配置是会被继承的。

调整子模块hello-maven-web:

<project xmlns="http://maven.apache.org/POM/4.0.0">
    <parent>
        <artifactId>hello-maven-parent</artifactId>
        <groupId>com.play.myMaven</groupId>
        <version>1.0-SNAPSHOT</version>
        <relativePath>../hello-maven-parent</relativePath>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>hello-maven-web</artifactId>

    <properties>
        <javax.mail.version>1.4.1</javax.mail.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>mail</artifactId>
            <version>${javax.mail.version}</version>
        </dependency>
    </dependencies>
</project>
  • 可以看到子模块只需要配置简单的groupId和artifactId就可以获得对应的依赖信息。(我们在parent模块中已经声明了完整的依赖信息。)可以有效降低依赖冲突的问题。
  • 子模块没有声明父POM中dependencyManagement已声明的依赖,是不产生任何实际效果的。例如没有声明junit

依赖范围scope为import的依赖只在dependencyManagement元素下才有效果,使用该范围的依赖通常指向一个POM,作用是将目标POM中的dependencyManagement配置导入并合并到当前POM的dependencyManagement元素中。

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>com.play.myMaven</groupId>
            <artifactId>hello-maven-parent</artifactId>
            <version>1.0-SNAPSHOT</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
  • type为pom,由于import范围的特殊性,一般都是指向打包类型为pom的模块。

可见,我们除了使用继承/复制配置,也可以使用scope为import的方式进行依赖配置导入。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,312评论 19 139
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 47,022评论 6 342
  • 简介 概述 Maven 是一个项目管理和整合工具 Maven 为开发者提供了一套完整的构建生命周期框架 Maven...
    闽越布衣阅读 9,804评论 6 39
  • 前言什么是 POMQuick Overview POM 常用元素 pom.xml 完整注释 参考 0 前言 什么是...
    阿父阅读 14,318评论 1 36
  • 孩子的成长,父母的修行,亲爱的家长朋友们,大家晚上好!我是『雅天』邓老师!! 给大家分享个故事: 某公司老板每天公...
    雅天幼儿园阅读 3,846评论 0 2