Groovy & Gradle: JVM Automation and Build Engineering · 课时

解决版本冲突与依赖约束

使用依赖报告、解析策略、约束和强制版本,诊断并修复依赖版本冲突。

第 4 / 4 课13 个步骤

解决版本冲突与依赖约束 是 CoddyKit 上的免费 Groovy & Gradle: JVM Automation and Build Engineering 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Groovy & Gradle: JVM Automation and Build Engineering 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Groovy & Gradle: JVM Automation and Build Engineering 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

The Conflict Problem

Two libraries you depend on may each pull in a different version of the same transitive dependency. Gradle must pick one — and the wrong choice causes runtime errors. Managing this is a core skill.

Default: Newest Wins

By default Gradle resolves a conflict by choosing the highest requested version of a dependency across the graph. Usually safe, but not always.

Seeing the Dependency Tree

The dependencies task prints the full resolved graph, showing which version won and why.

gradle dependencies --configuration runtimeClasspath

Explaining a Single Dependency

The dependencyInsight task explains exactly how one dependency was resolved and who requested each version.

gradle dependencyInsight --dependency guava

Forcing a Version

You can force a specific version, overriding the conflict resolution, with a resolution strategy.

configurations.all {
    resolutionStrategy {
        force 'com.google.guava:guava:32.1.3-jre'
    }
}

Dependency Constraints

The modern approach uses constraints to declare an allowed version without adding the dependency directly — cleaner than force.

dependencies {
    constraints {
        implementation 'com.google.guava:guava:32.1.3-jre'
    }
}

Excluding Transitives

Sometimes a transitive dependency is unwanted entirely. Exclude it from the bringing dependency.

implementation('org.example:lib:1.0') {
    exclude group: 'commons-logging'
}

Rejecting Bad Versions

A resolution strategy can reject known-broken versions so Gradle never selects them.

resolutionStrategy {
    componentSelection {
        all { if (candidate.version == '1.2.0') reject('known bug') }
    }
}

Failing on Conflict

For strict reproducibility, tell Gradle to fail the build whenever a version conflict occurs, forcing you to resolve it explicitly.

resolutionStrategy { failOnVersionConflict() }

Locking Dependencies

Dependency locking writes resolved versions to a lockfile so builds are fully reproducible across machines and time.

gradle dependencies --write-locks

Platforms and BOMs

Import a published BOM with the platform notation to align many related dependencies on one tested version set, avoiding manual version juggling.

dependencies {
    implementation platform('org.springframework.boot:spring-boot-dependencies:3.2.0')
    implementation 'org.springframework.boot:spring-boot-starter-web'
}

Quick Check

Test your conflict resolution knowledge.

Recap

You learned to manage version conflicts:

  • Default resolution picks the highest version
  • Inspect with dependencies and dependencyInsight
  • Override with force or, preferably, constraints
  • exclude removes unwanted transitives; reject blocks bad versions
  • Dependency locking makes builds reproducible
免费开始

用 AI 导师学习 Groovy — 免费

在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。

课程
12
课程
48

常见问题解答

「解决版本冲突与依赖约束」课时是免费的吗?

是的 — 「解决版本冲突与依赖约束」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Groovy & Gradle: JVM Automation and Build Engineering 课程的其余内容,请升级到 CoddyKit PRO。 Groovy & Gradle: JVM Automation and Build Engineering 课程共包含 4 节课。

「解决版本冲突与依赖约束」这节课中我会学到什么?

使用依赖报告、解析策略、约束和强制版本,诊断并修复依赖版本冲突。 你通过在浏览器中直接运行的动手代码来练习 Groovy & Gradle: JVM Automation and Build Engineering,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Groovy & Gradle: JVM Automation and Build Engineering 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Groovy & Gradle: JVM Automation and Build Engineering 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「解决版本冲突与依赖约束」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Groovy & Gradle: JVM Automation and Build Engineering 课中编写并运行代码吗?

能。每节 Groovy & Gradle: JVM Automation and Build Engineering 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 声明项目依赖
  2. 依赖解析与缓存
  3. 自定义仓库与 BOM
  4. 解决版本冲突与依赖约束
← 返回 Groovy & Gradle: JVM Automation and Build Engineering