Appearance
Gradle vs Maven
Gradle 和 Maven 是 Java 生态中最主流的两款构建工具。本文从多个维度进行对比,帮助你做出合适的技术选型。
快速对比
| 维度 | Gradle | Maven |
|---|---|---|
| 配置语言 | Groovy DSL / Kotlin DSL | XML |
| 构建速度 | 快(增量构建 + 缓存) | 慢 |
| 灵活性 | 高(可编程) | 低(约定驱动) |
| 学习曲线 | 中等 | 低 |
| 社区生态 | 活跃 | 成熟 |
| Android 支持 | 官方指定 | 不支持 |
| 多项目构建 | 优秀 | 一般 |
| 插件数量 | 较多 | 非常多 |
| 企业采用 | 增长中 | 广泛 |
配置语言对比
添加依赖
Maven (pom.xml):
xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>3.2.0</version>
<scope>test</scope>
</dependency>
</dependencies>Gradle (build.gradle.kts):
kotlin
dependencies {
implementation("org.springframework.boot:spring-boot-starter-web:3.2.0")
testImplementation("org.springframework.boot:spring-boot-starter-test:3.2.0")
}配置编译器
Maven:
xml
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>17</source>
<target>17</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>Gradle:
kotlin
java {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}
tasks.withType<JavaCompile> {
options.encoding = "UTF-8"
}多模块项目
Maven (父 pom.xml):
xml
<modules>
<module>core</module>
<module>web</module>
<module>api</module>
</modules>Gradle (settings.gradle.kts):
kotlin
include("core", "web", "api")自定义构建逻辑
Maven(需要编写插件或使用 Exec 插件):
xml
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<executions>
<execution>
<id>generate-code</id>
<phase>generate-sources</phase>
<goals><goal>exec</goal></goals>
<configuration>
<executable>python</executable>
<arguments><argument>scripts/generate.py</argument></arguments>
</configuration>
</execution>
</executions>
</plugin>Gradle(直接在脚本中编写):
kotlin
tasks.register<Exec>("generateCode") {
commandLine("python", "scripts/generate.py")
}
tasks.named("compileJava") {
dependsOn("generateCode")
}构建速度对比
Gradle 通常比 Maven 快 2-10 倍,主要得益于:
1. 增量构建
bash
# Gradle:只重新编译变更的文件
$ gradle compileJava
> Task :compileJava # 只编译修改的 3 个文件
# Maven:默认全量编译
$ mvn compile
[INFO] Compiling 247 source files # 全部重新编译2. 构建缓存
bash
# CI 环境中,Gradle 可从缓存服务器拉取上次构建结果
$ gradle build --build-cache
> Task :compileJava FROM-CACHE ← 直接使用缓存,0 秒
> Task :test FROM-CACHE ← 直接使用缓存,0 秒3. 并行构建
bash
# Gradle 并行执行无依赖关系的子项目
$ gradle build --parallel
# Maven 也支持,但效果不如 Gradle
$ mvn -T 4 install实测数据(大型项目参考)
| 场景 | Gradle | Maven |
|---|---|---|
| 无变更增量构建 | ~1s | ~30s |
| 单文件修改构建 | ~5s | ~45s |
| 全量构建(带缓存) | ~20s | ~90s |
| 全量构建(无缓存) | ~90s | ~120s |
生命周期对比
Maven 固定生命周期
validate → initialize → generate-sources → process-sources
→ generate-resources → process-resources → compile
→ process-classes → generate-test-sources → process-test-sources
→ generate-test-resources → process-test-resources → test-compile
→ process-test-classes → test → prepare-package → package
→ pre-integration-test → integration-test → post-integration-test
→ verify → install → deployGradle 灵活任务图
Gradle 没有固定生命周期,任务依赖关系由你决定:
clean → compileJava → processResources → classes
→ compileTestJava → testClasses → test
→ jar → assemble → build依赖范围对比
| Maven scope | Gradle configuration | 说明 |
|---|---|---|
compile | implementation | 编译和运行时 |
provided | compileOnly | 仅编译,运行时由容器提供 |
runtime | runtimeOnly | 仅运行时 |
test | testImplementation | 仅测试 |
compile(推荐) | api | 编译时且暴露给消费者 |
import (BOM) | platform() | 导入 BOM |
选择建议
选择 Gradle,如果你:
- 开发 Android 应用(必须用 Gradle)
- 追求构建速度,项目代码量大
- 需要灵活的自定义构建逻辑
- 使用 Kotlin 技术栈
- 构建多模块大型项目
- 接受 Kotlin DSL 的学习成本
选择 Maven,如果你:
- 团队熟悉 Maven,迁移成本高
- 项目较小,构建速度不是瓶颈
- 需要最广泛的插件生态支持
- 偏好约定大于配置的简洁配置
- 与遗留系统集成,要求稳定性
从 Maven 迁移到 Gradle
Gradle 提供了自动迁移工具:
bash
# 在 Maven 项目目录下执行
gradle init详细步骤参考:从 Maven 迁移到 Gradle
下一步
- 学习路线 - 规划 Gradle 学习路径
- Windows 安装 - 开始安装 Gradle