大佬教程收集整理的这篇文章主要介绍了春季-如何避免在多模块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,那么重复此版本将很不方便且@R_804_10197@在每个模块中.
同样,我可能想向commons-io这两个模块添加依赖项,并确保它们始终都依赖于commons-io的相@R_596_11197@.
在您从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 @R_801_7000@n 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引导插件继承
以上是大佬教程为你收集整理的春季-如何避免在多模块Gradle项目中重复依赖版本? 全部内容,希望文章能够帮你解决春季-如何避免在多模块Gradle项目中重复依赖版本? 所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。