这里有一个示例Spring项目这里,它包含两个模块。
其中一个模块的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,那么在每个模块中重复这个版本是不方便的,而且容易出错。
类似地,我可能希望在这两个模块中添加一个对commons-io的依赖,并确保它们始终都依赖于相同版本的commons-io。
如何避免在每个build.gradle文件中重复版本号?
发布于 2019-05-28 10:17:04
参见这个分级文档:Gradle中的一个良好实践是在一个地方配置共享公共特性的子项目,例如在root project的构建脚本中(或者使用自定义插件)。
编辑这里提出的解决方案不再被认为是来自Gradle团队的良好实践(上面的链接甚至在最近的Gradle版本文档中都没有提到subproject块);谢谢您@buggy的警告。
在您从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')
}Notes
plugins {} DSL (不需要旧的buildscript块)。io.spring.dependency-management版本,它将从Spring插件继承发布于 2019-05-28 10:04:46
您可以将ext{}块移动到一个新文件,并通过apply from:语句在项目的apply from:文件中引用它。
// project/versions.gradle
ext {
springBootVersion = '2.1.4.RELEASE'
}
// project/build.gradle
buildscript {
apply from: 'versions.gradle'
}
// module/build.gradle
dependencies {
implementation "some.dependency:dependency:$springBootVersion"
}现在,您只需在一个地方定义依赖版本。
通常,除了特定于模块的build.gradle文件之外,项目还将有一个项目级别的build.gradle文件.但是,您共享的回购没有项目级别的构建脚本。这就是为什么在每个模块的构建脚本中定义ext{}块的原因。这可能不是最优的,我建议您看看其他repos,看看不同的开发人员是如何解决这个问题的。
https://stackoverflow.com/questions/56338999
复制相似问题