概述
在maven里有 bom (bill of materials) 的功能,可以解决同一项目,不同版本依赖的问题。
gradle虽然是“下一代maven”替代品,但并没有原生的支持bom这一强大的功能。有两个方案可以搞定类似的需求:
- DSL
- 使用第三方插件,如 Dependency management plugin
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>
网友评论