春季-如何避免在多模块Gradle项目中重复依赖版本?

前端之家收集整理的这篇文章主要介绍了春季-如何避免在多模块Gradle项目中重复依赖版本? 前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

有一个示例Spring Boot项目here,其中包含两个模块.

其中一个模块的build.gradle如下所示:

buildscript {
    ext { springBootVersion = '2.1.4.RELEASE' }
    repositories { mavenCentral() }
    dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") }
}

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

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

bootJar {
    baseName = 'gs-multi-module-application'
    version = '0.0.1-SNAPSHOT'
}
sourceCompatibility = 1.8

repositories { mavenCentral() }

dependencies {
    compile('org.springframework.boot:spring-boot-starter-actuator')
    compile('org.springframework.boot:spring-boot-starter-web')
    compile project(':library')
    testCompile('org.springframework.boot:spring-boot-starter-test')
}

另一个模块的build.gradle如下所示:

buildscript {
    repositories { mavenCentral() }
}

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

ext { springBootVersion = '2.1.4.RELEASE' }

apply plugin: 'java'
apply plugin: 'eclipse'

jar {
    baseName = 'gs-multi-module-library'
    version = '0.0.1-SNAPSHOT'
}
sourceCompatibility = 1.8

repositories { mavenCentral() }

dependencies {
    compile('org.springframework.boot:spring-boot-starter')
    testCompile('org.springframework.boot:spring-boot-starter-test')
}

dependencyManagement {
    imports { mavenBom("org.springframework.boot:spring-boot-dependencies:${springBootVersion}") }
}

在两个模块中都声明了springBootVersion =’2.1.4.RELEASE’.对于一个2个模块的项目来说可能不是问题,但是如果我的项目有10个模块,并且我想确保所有模块始终依赖于同一版本的Spring Boot,那么重复此版本将很不方便且容易出错在每个模块中.

同样,我可能想向commons-io这两个模块添加依赖项,并确保它们始终都依赖于commons-io的相同版本.

如何避免在每个build.gradle文件中重复版本号?

最佳答案
请参阅this Gradle documentation:Gradle中的一个好习惯是在单个位置配置共享共同特征的子项目,例如在根项目的构建脚本中(或使用自定义插件)

在您从Spring引导文档中获取的示例中,此模式可以应用于将Spring引导和其他常见依赖版本集中在一个地方,但是您可以走得更远,还可以配置其他常见特征(Java插件配置,存储库等).

这是我将重新编写Spring示例以使其更加整洁和干燥的方法

根项目

/**
 * Add Springboot plugin into build script classpath (without applying it)
 * This is this only place where you need to define the Springboot version.
 *
 * See https://docs.spring.io/spring-boot/docs/current/gradle-plugin/reference/html/#managing-dependencies-using-in-isolation
 */
plugins {
    id "org.springframework.boot" version "2.1.4.RELEASE" apply false
}

// Set version for dependencies share between subprojects
ext {
    commonsIoVersion = "2.6"
}

subprojects {
    // common config for all Java subprojects
    apply plugin: "java"
    apply plugin: "eclipse"
    sourceCompatibility = 1.8
    repositories { 
        mavenCentral() 
    }

    // apply Spring Boot's dependency management plugin
    apply plugin: "io.spring.dependency-management"
}

图书馆子项目

// no need for additional plugins

jar {
    baseName = 'gs-multi-module-library'
    version = '0.0.1-SNAPSHOT'
}

dependencies {
    implementation('org.springframework.boot:spring-boot-starter')
    implementation "commons-io:commons-io:${commonsIoVersion}"

    testCompile('org.springframework.boot:spring-boot-starter-test')
}

dependencyManagement {
    imports {
        mavenBom org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES 
    }
}

应用子项目

plugins {
    id "org.springframework.boot"
}

bootJar { 
    baseName = 'gs-multi-module-application'
    version = '0.0.1-SNAPSHOT'
}

dependencies {
    implementation  project(':library')

    implementation ('org.springframework.boot:spring-boot-starter-actuator')
    implementation ('org.springframework.boot:spring-boot-starter-web')
    implementation "commons-io:commons-io:${commonsIoVersion}"

    // could also be configured in root project.
    testCompile('org.springframework.boot:spring-boot-starter-test')
}

笔记

>此解决方案仅使用新的插件{} DSL(不需要旧的buildscript块)
>不应明确配置io.spring.dependency-management版本,它将从Spring引导插件继承

原文链接:https://www.f2er.com/spring/531624.html

猜你在找的Spring相关文章