平台用于确保项目中的所有依赖项与一套一致的版本对齐。
平台有助于你在不同模块或库之间管理和强制执行版本一致性,特别是在处理一组需要保持同步的相关依赖项时。
使用平台
平台是一种专门的软件组件,用于控制传递依赖的版本。通常,它包含依赖约束,这些约束可以推荐或强制执行特定版本。当你需要在多个项目之间共享一致的依赖版本时,平台特别有用。
在典型设置中,你会看到:
-
平台项目:定义用于不同子项目的依赖约束。
-
多个子项目:它们依赖于平台,并且在声明依赖时无需指定版本。
java-platform plugin
支持在 Java 生态系统中创建平台。平台通常也作为 Maven BOMs(物料清单)发布,Gradle 原生支持 BOM。
要使用平台,请使用 platform
关键字声明依赖项:
dependencies {
// get recommended versions from the platform project
api(platform(project(":platform")))
// no version required
api("commons-httpclient:commons-httpclient")
}
dependencies {
// get recommended versions from the platform project
api platform(project(':platform'))
// no version required
api 'commons-httpclient:commons-httpclient'
}
此表示法会自动执行以下几个操作:
-
将
org.gradle.category
属性设置为 platform,确保 Gradle 选择平台组件。 -
默认启用
endorseStrictVersions
行为,强制执行平台中定义的严格版本。
如果不需要强制执行严格版本,可以使用 doNotEndorseStrictVersions
方法禁用它。
创建平台
在 Java 项目中,可以将 java-platform
插件与依赖约束结合使用来创建平台:
plugins {
id("java-platform")
}
dependencies {
constraints {
api("com.google.guava:guava:30.1-jre")
api("org.apache.commons:commons-lang3:3.12.0")
}
}
这定义了一个自定义平台,其中包含 guava
和 commons-lang3
的特定版本,可以在其他项目中应用。
导入平台
Gradle 支持导入 BOMs,BOMs 是包含用于管理依赖版本的 <dependencyManagement>
部分的 POM 文件。
为了符合 BOM 的条件,.pom
文件需要将 pom 设置为打包类型。这意味着 POM 文件应在其元数据中明确指定 <packaging>pom</packaging>。
Gradle 将 BOM 的块中的所有条目视为类似于添加依赖约束。
常规平台
要导入 BOM,请使用 platform
依赖修饰符方法声明对其的依赖项:
dependencies {
// import a BOM
implementation(platform("org.springframework.boot:spring-boot-dependencies:1.5.8.RELEASE"))
// define dependencies without versions
implementation("com.google.code.gson:gson")
implementation("dom4j:dom4j")
}
dependencies {
// import a BOM
implementation platform('org.springframework.boot:spring-boot-dependencies:1.5.8.RELEASE')
// define dependencies without versions
implementation 'com.google.code.gson:gson'
implementation 'dom4j:dom4j'
}
在此示例中,Spring Boot BOM 提供了 gson
和 dom4j
的版本,因此无需显式指定版本。
强制平台
enforcedPlatform
关键字可用于覆盖依赖图中的任何版本,但应谨慎使用,因为它实际上是传递性的,并将强制版本导出到项目的所有消费者。
dependencies {
// import a BOM. The versions used in this file will override any other version found in the graph
implementation(enforcedPlatform("org.springframework.boot:spring-boot-dependencies:1.5.8.RELEASE"))
// define dependencies without versions
implementation("com.google.code.gson:gson")
implementation("dom4j:dom4j")
// this version will be overridden by the one found in the BOM
implementation("org.codehaus.groovy:groovy:1.8.6")
}
dependencies {
// import a BOM. The versions used in this file will override any other version found in the graph
implementation enforcedPlatform('org.springframework.boot:spring-boot-dependencies:1.5.8.RELEASE')
// define dependencies without versions
implementation 'com.google.code.gson:gson'
implementation 'dom4j:dom4j'
// this version will be overridden by the one found in the BOM
implementation 'org.codehaus.groovy:groovy:1.8.6'
}
当使用 enforcedPlatform
时,如果你的软件组件供他人使用,请务必谨慎。此声明是传递性的,会影响你的消费者的依赖图。如果他们不同意任何强制版本,则需要使用 exclude
。作为替代方案,如果你的可重用组件强烈倾向于特定的第三方依赖版本,可以考虑使用带有 strictly
的富版本声明。