主题
界面
主题
本仓库用 Maven(《Maven 进阶:依赖管理与多模块》)。本节回答三个问题:
| 问题 | 答案 |
|---|---|
| Gradle 是什么? | 另一个主流构建工具(Android 官方、Kotlin/Spring 官方项目多用)✓ |
| 与 Maven 的本质区别? | Maven 是"声明式约定",Gradle 是"可编程构建" ★★ |
| 该学吗? | 会 Maven 后 30 分钟能上手 Gradle ✓ ——概念是通用的 ✓ |
build.gradle.kts(Kotlin DSL)★;| 场景 | 需要 |
|---|---|
| 看 Android / Kotlin 项目 | ✅ 几乎都是 Gradle ✓ |
| 换团队/换项目 | ✅ 30 分钟概念迁移 ✓ |
| 理解"构建工具在解决什么问题" | ✅ 两个工具对比着看最清楚 ★ |
| 概念 | Maven | Gradle |
|---|---|---|
| 构建文件 | pom.xml | build.gradle.kts(Kotlin DSL)✓ |
| 坐标 | groupId:artifactId:version | group:name:version(一样)✓ |
| 仓库 | Maven Central / 阿里云 | Maven Central(兼容同一套 jar)✓ |
| 依赖声明 | <dependency> | implementation(...) ★ |
| 版本管理 | <dependencyManagement> | implementation(platform(...)) ★(BOM 也支持 ✓) |
| scope | compile / runtime / test / provided | implementation / runtimeOnly / testImplementation / compileOnly ✓ |
| 多模块 | <modules> + <parent> | settings.gradle.kts 的 include(...) ✓ |
| 生命周期 | 固定阶段(validate→compile→test→package)✓ | Task 图(可自定义任务与依赖)★ |
| 插件 | <plugin> | plugins { id("...") } ✓ |
关键映射(Maven scope → Gradle configuration):
| Maven | Gradle | 语义 |
|---|---|---|
| compile | implementation | 编译+运行 ✓ |
| provided | compileOnly | 只编译不打包(《Maven 进阶:依赖管理与多模块》 的 5.5)✓ |
| runtime | runtimeOnly | 只运行 ✓ |
| test | testImplementation | 测试 ✓ |
| — | api | ★ Gradle 特有:会把依赖"传递给使用者"(见 5.4) |
Maven 版(《Maven 进阶:依赖管理与多模块》):
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>Gradle 版(Kotlin DSL):
dependencies {
implementation("org.springframework.boot:spring-boot-starter-web") // ★ 版本来自 platform(BOM)
runtimeOnly("org.postgresql:postgresql") // = runtime scope ✓
testImplementation("org.springframework.boot:spring-boot-starter-test")
}
// 版本统一(= dependencyManagement)★
dependencies {
implementation(platform("org.springframework.boot:spring-boot-dependencies:3.4.1"))
}读起来几乎是"同一种语言" ✓ ——概念完全互通 ✓
| 优势 | 说明 |
|---|---|
| ① 增量构建极强 ★★ | 按"输入输出"判断任务要不要重跑 ——没变就跳过 ✓(比 Maven 的模块级增量更细) |
| ② 可编程 | 构建逻辑就是代码(Kotlin/Groovy)——能做 Maven 做不了的复杂事 ✓ |
| ③ 多语言 | Java / Kotlin / Android / 前端混编一个工程 ✓ |
★ 增量构建举例:改了一个注释,compileJava 会跳过(输入没变)✓ ——大项目里收益显著 ✓
| 代价 | 说明 |
|---|---|
| ① 灵活 = 不一致 ✗ | 每个项目的构建脚本都可能"长得很不一样" ——Maven 的约定让人"一看就懂" ✗(这是双刃剑 ⚠️) |
| ② 学习曲线更陡 | 出问题时要懂 Groovy/Kotlin 与 Gradle 内部模型 ✗ |
api vs implementation 的区别(Gradle 特有,值得理解):
dependencies {
api("com.example:my-utils:1.0") // 使用者**也能看到** my-utils 的类型(传递)✓
implementation("com.example:my-internal:1.0") // 使用者**看不到**(实现细节)✓
}
api类似"暴露在接口签名里";implementation是"纯内部使用" ✓默认用
implementation(减少传递,《Maven 进阶:依赖管理与多模块》 的"不要依赖传递")✓
| 维度 | Maven | Gradle |
|---|---|---|
| 约定 vs 灵活 | 约定(简单可预测)✓ | 灵活(强大但易乱)⚠️ |
| 增量构建 | 模块级 | 任务级(更强) ✓ |
| Android/Kotlin | ❌ | ✅ 必须 ✓ |
| 生态默认 | Java 传统项目 ✓ | 新项目 / 多语言 |
| 团队上手 | 快 ✓ | 慢 ⚠️ |
决策树:
做 Android/Kotlin? ──是──→ Gradle(没有选择)✓
│否
团队已用哪个? ──沿用──→ ✓(迁移成本 > 收益)
│都没用
新项目、想要强增量?──是──→ Gradle 可以 ✓
│
└──否──→ Maven ✓(**本仓库的选择**:约定清晰、资料最多 ✓)★ 一条重要原则:
"团队统一"比"哪个更好"重要一万倍 ✓✓
一半 Maven 一半 Gradle 的多模块项目是灾难 ✗✗
plugins { java }
repositories { mavenCentral() }
dependencies { testImplementation("org.junit.jupiter:junit-jupiter:5.11.4") }
tasks.test { useJUnitPlatform() }见 5.2 ✓
// ✗ 在 build.gradle.kts 里写"发布前改数据库"
tasks.register("publish") {
doLast {
val conn = DriverManager.getConnection("jdbc:mysql://prod...")
// ✗✗ 构建脚本里做生产操作
}
}问题:构建逻辑与运维逻辑混杂 ✗ ——应该用 CI/CD 工具或独立脚本(《Git Flow 与团队协作》)✓
| 命令 | Maven | Gradle |
|---|---|---|
| 构建 | mvn compile | gradle build(含测试 ✓) |
| 只编译 | mvn compile | gradle compileJava |
| 测试 | mvn test | gradle test |
| 打包 | mvn package | gradle jar / bootJar |
| 清理 | mvn clean | gradle clean |
| 跳过测试 | -DskipTests | -x test ✓ |
| 指定模块 | -pl xxx -am | gradle :module:build ✓ |
gradle build --dry-run ★:看"将会执行哪些 task"而不执行 ✓(与 《Shell 脚本基础》 的 --dry-run 思路一致)✓;gradle dependencies:= Maven 的 dependency:tree ✓;gradle build --info:看增量判断("UP-TO-DATE"就是跳过的任务)★。| # | 错误 | 说明 |
|---|---|---|
| 1 | 把所有逻辑塞进构建脚本 | 见 6.3 ✗ |
| 2 | Maven 与 Gradle 混用 | 两个构建系统并存 = 灾难 ✗✗ |
| 3 | 不锁插件版本 | 每次构建行为可能变 ✗ |
| 4 | 把 api 当 implementation 用 | 传递依赖泄漏 ✗ |
labs-start)用 Gradle 重写一遍(在临时目录)——体会"概念迁移" ✓;mvn compile 与 gradle compileJava,观察谁跳过了编译 ✓;settings.gradle.kts 与模块列表 ✓。扩展任务:写一份"Maven ↔ Gradle 命令与概念对照表"(10 行以内)——这是最好的学习输出 ✓
runtime 对应 Gradle 的 ______;Gradle 特有的"传递给使用者"的 configuration 是 ______。runtimeOnly;api ✓这与 《Git Flow 与团队协作》 的"团队统一分支模型"、《Linux 文件、用户与权限管理》 的"统一部署账号"是同一条原则:统一性本身就是一种质量 ✓
build.gradle.kts 的依赖声明;api 与 implementation 的区别;企业开发的常规路线到此完成。接下来是测试专项 B:
暂无