自定义仓库与 BOM
配置自定义 Maven 或 Ivy 仓库,并使用物料清单(BOM)统一依赖版本。
自定义仓库与 BOM 是 CoddyKit 上的免费 Groovy & Gradle: JVM Automation and Build Engineering 课时。 这是第 3 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Groovy & Gradle: JVM Automation and Build Engineering 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Groovy & Gradle: JVM Automation and Build Engineering 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Beyond Maven Central
By default, Gradle looks for dependencies in Maven Central. But what if your dependencies aren't there?
Custom repositories allow you to fetch libraries from other locations, like:
- Your company's private artifact server.
- Specific public repositories (e.g., Google's Maven repo for Android).
- Local file system directories.
Adding a Maven Repository
To add a custom Maven repository, you declare it in the repositories block of your build.gradle file.
Gradle will check repositories in the order they are declared, stopping at the first match.
repositories {
mavenCentral()
maven {
url 'https://repo.spring.io/milestone'
}
}Example: Google Maven Repo
Many Android libraries are hosted on Google's Maven repository. You'd add it like this:
This is crucial for projects using Google-specific libraries.
repositories {
google() // Shortcut for Google's Maven repo
mavenCentral()
}Understanding Ivy Repositories
While Maven is very common, Gradle also supports Ivy repositories. Ivy has a more flexible layout for artifacts.
You might encounter Ivy repositories in older projects or specific corporate environments.
repositories {
ivy {
url "http://repo.mycompany.com/ivy"
layout "pattern", {
artifact "[organization]/[module]/[revision]/[artifact](-[classifier])-[revision].[ext]"
}
}
}Local Directory as Repo
For testing or internal use, you can even use a local directory as a repository. This is handy for sharing artifacts within a team without a dedicated server.
Just specify the path to your local folder.
repositories {
flatDir {
dirs 'libs' // Looks for JARs directly in the 'libs' folder
}
// Or a more structured Maven-like local repo
maven {
url uri('../my-local-maven-repo')
}
}Bill of Materials (BOMs)
A Bill of Materials (BOM) is a special Maven POM file that defines a curated list of dependency versions.
It helps manage transitive dependencies and ensures consistent versions across a multi-module project or when using a suite of related libraries.
Consistency with BOMs
BOMs solve a common problem: dependency version conflicts. If multiple libraries depend on different versions of the same transitive dependency, you can end up with unpredictable behavior.
With a BOM, you declare a single "source of truth" for versions, making your build more reliable.
Importing a BOM
To use a BOM in Gradle, you declare it as a dependency using the platform() or enforcedPlatform() function. This tells Gradle to use the versions specified in the BOM.
Notice how we don't specify versions for spring-core or spring-web; the BOM handles it!
dependencies {
implementation platform('org.springframework.boot:spring-boot-dependencies:2.7.5')
// These versions are now managed by the BOM
implementation 'org.springframework:spring-core'
implementation 'org.springframework:spring-web'
}Platform vs. EnforcedPlatform
There are two ways to import a BOM:
platform(): Suggests versions. Other modules can override these versions if explicitly declared.enforcedPlatform(): Strictly enforces versions. Any explicitly declared versions for dependencies in the BOM will be overridden by the BOM's version.
Use enforcedPlatform() for strong version consistency.
Check Your Knowledge
Let's test your understanding of custom repositories and BOMs!
Custom Repos & BOMs Recap
Great job! You've learned how to:
- Configure custom Maven and Ivy repositories in Gradle.
- Understand the importance of repository order.
- Use local directories as repositories.
- Leverage Bill of Materials (BOMs) for consistent dependency version management.
- Differentiate between
platform()andenforcedPlatform().
These techniques are vital for managing complex dependency landscapes in real-world projects!
常见问题解答
「自定义仓库与 BOM」课时是免费的吗?
是的 — 「自定义仓库与 BOM」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Groovy & Gradle: JVM Automation and Build Engineering 课程的其余内容,请升级到 CoddyKit PRO。 Groovy & Gradle: JVM Automation and Build Engineering 课程共包含 4 节课。
「自定义仓库与 BOM」这节课中我会学到什么?
配置自定义 Maven 或 Ivy 仓库,并使用物料清单(BOM)统一依赖版本。 你通过在浏览器中直接运行的动手代码来练习 Groovy & Gradle: JVM Automation and Build Engineering,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Groovy & Gradle: JVM Automation and Build Engineering 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Groovy & Gradle: JVM Automation and Build Engineering 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 4 节。
「自定义仓库与 BOM」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Groovy & Gradle: JVM Automation and Build Engineering 课中编写并运行代码吗?
能。每节 Groovy & Gradle: JVM Automation and Build Engineering 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 声明项目依赖
- 依赖解析与缓存
- 自定义仓库与 BOM
- 解决版本冲突与依赖约束