ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2019-12-10 22:20:44  阅读:382  来源: 互联网

标签:spring-boot gradle spring


有一个示例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引导插件继承

标签:spring-boot,gradle,spring
来源: https://codeday.me/bug/20191210/2104488.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有