美文网首页Maven
(X)依赖管理

(X)依赖管理

作者: SonyaBaby | 来源:发表于2018-06-22 19:11 被阅读0次

通过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的方式进行依赖配置导入。

相关文章

  • (X)依赖管理

    通过Maven聚合和继承章节中POM可继承的元素我们看到有dependencies,说明依赖是会被继承的。Mave...

  • Gradle 排除排除本地项目 部分依赖

    1. 查看依赖树 符号的含义:x.x.x () 该依赖已经有了,将不再重复依赖。x.x.x -> x.x.x 该依...

  • SpringBoot Actuator 服务监控与管理

    SpringBoot Actuator 服务监控与管理 Maven 依赖 Spring Boot2.x中,默认只开...

  • Maven

    Maven是一个构建依赖管理工具。POM(Project Object Model):是一个xml文件(pom.x...

  • Jetpack Navigation的使用

    背景 Navigation组件是Android X 依赖库中的一个导航组件,它能接替管理多Fragment之间的关...

  • 手写SpringMVC:项目框架搭建(骨架)

    项目框架搭建(骨架) 依赖管理工具Gradle 优点: 自动化管理依赖 解决依赖冲突 不仅仅管理依赖,更管理项目 ...

  • Nacos-SpringCloud

    依赖和相关配置 添加依赖注意:版本 0.2.x.RELEASE 对应的是 Spring Boot 2.x 版本,版...

  • PHP 资源大全

    依赖管理 依赖和包管理库 Composer/Packagist:一个包和依赖管理器Composer Install...

  • [Gradle中文教程系列]-跟我学Gradle-5.0:依赖-

    什么是依赖管理 通常而言,依赖管理包括两部分,对依赖的管理以及发布物的管理;依赖是指构建项目所需的构件(jar包等...

  • CocoaPods的基本使用

    CocoaPods 是什么? CocoaPods 是开发 OS X 和 iOS 应用程序的一个第三方库的依赖管理工...

网友评论

    本文标题:(X)依赖管理

    本文链接:https://www.haomeiwen.com/subject/puqlyftx.html