Gradle通过io.spring.dependency-management插件实现类Maven的依赖管理方式

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/u013632755/article/details/100714691


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

引入插件

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

依赖管理配置

你有两个选项来配置依赖项管理:

  1. 使用插件的DSL来直接配置依赖项
  2. 导入一个或者多个已经存在的Maven bom文件

依赖项的管理默认被应用到所有的配置或者一个或者多个指定的配置中。

依赖项管理DSL

使用:分隔符的配置方式:

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

通过groupnameversion进行指定配置:

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

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

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

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

依赖集配置

如果对于多个模块具有相同的group和version的依赖配置,你可以使用依赖集进行配置。这样你就只需要指定统一的group和version,而依赖模块不需要再额外指定。如下所示:

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中进行排除:

扫描二维码关注公众号,回复: 7661535 查看本文章
dependencyManagement {
    dependencies {
        dependencySet(group:'org.springframework', version: '4.1.4.RELEASE') {
            entry('spring-core') {
                exclude group: 'commons-logging', name: 'commons-logging'
            }
        }
    }
}

导入Maven Bom

插件允许你导入已经存在的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 (*)

目前介绍了插件比较常用的用法,更多详细的使用介绍查看官方文档吧。

猜你喜欢

转载自blog.csdn.net/u013632755/article/details/100714691