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

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

概述有一个示例Spring Boot项目here,其中包含两个模块.其中一个模块的build.gradle如下所示:buildscript { ext { springBootVersion = '2.1.4.RELEASE' } repositories { mavenCentral() } dependencies { classpa

有一个示例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.8repositorIEs { 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.8repositorIEs { 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 subprojectsext {    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 pluginsjar {    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引导插件继承 总结

以上是内存溢出为你收集整理的春季-如何避免在多模块Gradle项目中重复依赖版本? 全部内容,希望文章能够帮你解决春季-如何避免在多模块Gradle项目中重复依赖版本? 所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/langs/1230438.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-06
下一篇 2022-06-06

发表评论

登录后才能评论

评论列表(0条)

保存