美文网首页
Gradle(3)-依赖管理

Gradle(3)-依赖管理

作者: 卡门001 | 来源:发表于2020-07-27 11:15 被阅读0次

概述

在maven里有 bom (bill of materials) 的功能,可以解决同一项目,不同版本依赖的问题。
gradle虽然是“下一代maven”替代品,但并没有原生的支持bom这一强大的功能。有两个方案可以搞定类似的需求:

Spring开发了一个类似Maven的依赖关系管理功能的Gradle插件(插件GitHub)。

这里主要介绍第三方插件方式

引入插件

plugins {
  id "io.spring.dependency-management" version "1.0.9.RELEASE"
}

依赖项管理DSL

分隔符的配置方式

dependencyManagement {
    dependencies {
        dependency 'org.springframework:spring-core:4.0.3.RELEASE'
    }
}

通过group,name,version进行指定配置:

dependencyManagement {
    dependencies {
        dependency group:'org.springframework', name:'spring-core', version:'4.0.3.RELEASE'
    }
}

通过上面两种方式中的任意一种进行了管理配置之后,在需要引入spring-core的包时,就可以通过如下方式配置依赖:

dependencies {
    compile 'org.springframework:spring-core'
}

这样一个依赖管理配置就完成了。

依赖集配置

可以使用Maven的Exclusios语义进行依赖项的排除。对单个依赖项进行排除:

dependencyManagement {
     dependencies {
          dependencySet(group:'org.slf4j', version: '1.7.7') {
               entry 'slf4j-api'
               entry 'slf4j-simple'
          }
     }
}

排除依赖(exclusion)

可以使用Maven的Exclusios语义进行依赖项的排除。对单个依赖项进行排除:

dependencyManagement {
    dependencies {
        dependency('org.springframework:spring-core:4.0.3.RELEASE') {
            exclude 'commons-logging:commons-logging'
        }
    }
}

也可以对依赖集的entry中进行排除:

dependencyManagement {
    dependencies {
        dependencySet(group:'org.springframework', version: '4.1.4.RELEASE') {
            entry('spring-core') {
                exclude group: 'commons-logging', name: 'commons-logging'
            }
        }
    }
}

导入Maven Bom

dependencyManagement {
     imports {
          mavenBom 'io.spring.platform:platform-bom:1.0.1.RELEASE'
     }
}

dependencies {
     compile 'org.springframework.integration:spring-integration-core'
}

这个配置会将Spring框架的Bom的依赖应用到当前的项目:

$ gradle dependencies --configuration compile
:dependencies

------------------------------------------------------------
Root project
------------------------------------------------------------

compile - Compile classpath for source set 'main'.
\--- org.springframework.integration:spring-integration-core: -> 4.0.2.RELEASE
     +--- org.springframework.retry:spring-retry:1.1.0.RELEASE
     |    \--- org.springframework:spring-context:4.0.3.RELEASE -> 4.0.6.RELEASE
     |         +--- org.springframework:spring-aop:4.0.6.RELEASE
     |         |    +--- aopalliance:aopalliance:1.0
     |         |    +--- org.springframework:spring-beans:4.0.6.RELEASE
     |         |    |    \--- org.springframework:spring-core:4.0.6.RELEASE
     |         |    |         \--- commons-logging:commons-logging:1.1.3
     |         |    \--- org.springframework:spring-core:4.0.6.RELEASE (*)
     |         +--- org.springframework:spring-beans:4.0.6.RELEASE (*)
     |         +--- org.springframework:spring-core:4.0.6.RELEASE (*)
     |         \--- org.springframework:spring-expression:4.0.6.RELEASE
     |              \--- org.springframework:spring-core:4.0.6.RELEASE (*)
     +--- org.springframework:spring-tx:4.0.5.RELEASE -> 4.0.6.RELEASE
     |    +--- org.springframework:spring-beans:4.0.6.RELEASE (*)
     |    \--- org.springframework:spring-core:4.0.6.RELEASE (*)
     +--- org.springframework:spring-messaging:4.0.5.RELEASE -> 4.0.6.RELEASE
     |    +--- org.springframework:spring-beans:4.0.6.RELEASE (*)
     |    +--- org.springframework:spring-context:4.0.6.RELEASE (*)
     |    \--- org.springframework:spring-core:4.0.6.RELEASE (*)
     +--- org.springframework:spring-context:4.0.5.RELEASE -> 4.0.6.RELEASE (*)
     \--- org.springframework:spring-aop:4.0.5.RELEASE -> 4.0.6.RELEASE (*)

配置build.gradle全文

// 引入插件
plugins {
    id "io.spring.dependency-management" version "1.0.3.RELEASE"
}
apply plugin: 'java'
apply plugin: 'application'
repositories {
    // 使用阿里云镜像加速
    maven { url 'http://maven.aliyun.com/nexus/content/groups/public' }
    mavenLocal()
}
// import bom
dependencyManagement {
    imports {
        mavenBom 'com.coder4.sbmvt:pom-parent:0.0.1'
    }
}
dependencies {
    // bom会帮忙填充版本
    compile 'org.springframework.boot:spring-boot-starter-web'
    // 你也可以自己指定版本,会根据两者取一个合理不冲突的值
    testCompile 'junit:junit:4.12'
}
// Define the main class for the application
mainClassName = 'App'

parent之pom.xml源文

<?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.coder4.sbmvt</groupId>
    <artifactId>pom-parent</artifactId>
    <version>0.0.1</version>
    <packaging>pom</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
        <spring.boot.version>1.5.6.RELEASE</spring.boot.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring.boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
      <dependency>
          <groupId>com.google.guava</groupId>
          <artifactId>guava</artifactId>
          <version>23.0</version>
      </dependency>
        </dependencies>
    </dependencyManagement>

    <distributionManagement>
        <repository>
            <id>nexus_coder4</id>
            <url>http://maven.coder4.com/nexus/content/repositories/releases/</url>
        </repository>
        <snapshotRepository>
            <id>nexus_coder4</id>
            <url>http://maven.coder4.com/nexus/content/repositories/snapshots/</url>
        </snapshotRepository>
    </distributionManagement>
</project>

相关文章

  • Gradle(3)-依赖管理

    概述 在maven里有 bom (bill of materials) 的功能,可以解决同一项目,不同版本依赖的问...

  • Gradle入门系列

    Gradle入门系列(1):简介Gradle入门系列(2):第一个Java项目Gradle入门系列(3):依赖管理...

  • Cocoapods原理总结

    CocoaPods是IOS项目的依赖管理工具,类似于Android的gradle,不过gradle不仅有依赖管理功...

  • Gradle

    Gradle for Android(一)基本配置、依赖管理 Gradle for Android(二)全局设置、...

  • 老项目使用 CocoaPods 遇到的 install 问题

    Android 目前开发中常使用 Gradle 管理依赖,久闻 iOS 使用 CocoaPods 管理依赖。 使用...

  • Grande for Android

    Gradle for Android(一)基本配置、依赖管理Gradle for Android(二)全局设置、自...

  • Gradle 依赖管理

    仓库 远程仓库:专门用来提供给开发者使用的依赖库gradle通过远程仓库帮助开发者管理依赖,gradle不会默认为...

  • Gradle依赖管理

    一、前言 在开发Android的过程中经常使用到Gradle依赖,这里就总结一下Gradle依赖管理中的一些用法。...

  • gradle 依赖管理

    原文链接 1.添加依赖包名 1.1 依赖类型 常见的依赖包含两种类型。(1) 一类是项目中所需要的库,包括本地/仓...

  • 管理Gradle依赖

    详见:管理Gradle依赖的三种不同方法:[https://juejin.cn/post/684490361534...

网友评论

      本文标题:Gradle(3)-依赖管理

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