多项目构建与 settings 文件
使用 Gradle 多项目构建、settings.gradle 文件以及跨子项目共享的配置,组织更大型的代码库。
多项目构建与 settings 文件 是 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 节课。
本课时的部分内容尚未翻译,以英文显示。
Why Multi-Project Builds?
Real applications often split into modules — an API, a core library, a CLI. Gradle multi-project builds let one build coordinate many subprojects, sharing configuration and dependencies.
The settings.gradle File
The root settings.gradle defines the build: its name and which subprojects belong to it. Without an include entry, Gradle ignores a directory.
rootProject.name = 'my-app'
include 'core', 'api', 'cli'Directory Layout
Each included project lives in its own folder with its own build.gradle. The root build can configure them all from one place.
The Root build.gradle
Use allprojects and subprojects blocks in the root build to apply shared settings, avoiding repetition.
subprojects {
apply plugin: 'java'
repositories { mavenCentral() }
}Project Dependencies
One subproject can depend on another using the project() notation, so Gradle builds them in the right order.
dependencies {
implementation project(':core')
}The Build Order
Gradle builds a directed graph of project dependencies and compiles each module before the ones that need it. You never order things by hand.
Running Tasks Across Projects
From the root, gradle build builds every subproject. Target one with the project path prefix.
gradle :api:testConfiguration on Demand
For large builds, enabling configuration on demand tells Gradle to configure only the projects relevant to the requested task, speeding up startup.
org.gradle.configureondemand=trueSharing with buildSrc
The special buildSrc directory holds shared build logic (custom tasks, plugins) compiled once and available to every subproject automatically.
Composite Builds
For independent repositories, a composite build with includeBuild lets you wire separate Gradle builds together without publishing artifacts.
includeBuild '../shared-lib'Parallel Execution
Independent subprojects can build at the same time. Enable parallel execution to use all CPU cores, cutting build time on large multi-project setups.
org.gradle.parallel=trueQuick Check
Test your multi-project knowledge.
Recap
You learned to structure larger builds:
- Multi-project builds coordinate many modules
settings.gradleincludes subprojectssubprojects/allprojectsshare config from the rootproject(:name)declares inter-module dependenciesbuildSrcand composite builds promote reuse
常见问题解答
「多项目构建与 settings 文件」课时是免费的吗?
是的 — 「多项目构建与 settings 文件」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Groovy & Gradle: JVM Automation and Build Engineering 课程的其余内容,请升级到 CoddyKit PRO。 Groovy & Gradle: JVM Automation and Build Engineering 课程共包含 4 节课。
「多项目构建与 settings 文件」这节课中我会学到什么?
使用 Gradle 多项目构建、settings.gradle 文件以及跨子项目共享的配置,组织更大型的代码库。 你通过在浏览器中直接运行的动手代码来练习 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 节。
「多项目构建与 settings 文件」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Groovy & Gradle: JVM Automation and Build Engineering 课中编写并运行代码吗?
能。每节 Groovy & Gradle: JVM Automation and Build Engineering 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- Gradle 安装与 CLI
- Gradle 项目结构
- 任务与构建生命周期
- 多项目构建与 settings 文件