gradle

Gradle 8+

Gradle CLI 命令快速参考:项目任务、依赖管理、多项目构建、Wrapper、配置缓存,以及 build scan,并附带常用参数和实操示例。

30 条命令

任务

gradle tasks

列出当前构建中定义的所有任务,按类别分组。`--all` 同时显示内部 / 隐藏任务。

--all;--group=<group>;--quiet

gradle tasks --all
gradle help

打印某个任务的帮助文本:描述、分组、声明的输入 / 输出。

gradle help --task build
gradle :<module>:tasks

列出多项目构建中某个子项目的任务(把 `<module>` 替换为 Gradle 路径)。

gradle :web:tasks --all

生命周期

gradle build

运行标准 JVM 构建链:`compile → processResources → classes → jar → test → check → assemble`(配置了的话再加上 zip 发行版)。

-x <task> 排除任务;--parallel;--build-cache;--configuration-cache

gradle build -x test
gradle clean

删除构建目录(`build/`),让下一次构建从头执行所有任务。

--no-rebuild 不自动触发默认生命周期

gradle clean build
gradle assemble

编译并打包,但不跑验证 / 测试任务。只需要 jar 时能更快拿到反馈。

gradle assemble
gradle check

运行所有验证任务(`test`、`lint` 等),但不生成 jar。在 CI 中作为合并前门禁。

--continue 任务失败后继续

gradle check --continue
gradle test

运行单元测试(Test 任务)。用 `--tests <pattern>` 限定到具体类 / 方法。

--tests com.acme.UserServiceTest;--tests '*User*';--info

gradle test --tests 'com.acme.*ServiceTest'

依赖

gradle dependencies

打印某个配置的解析后依赖树(如 `compileClasspath`、`runtimeClasspath`、`testCompileClasspath`)。

--configuration <name>;--all 显示全部配置

gradle dependencies --configuration runtimeClasspath | grep -i slf4j
gradle dependencyInsight --dependency <id>

解释为什么某个依赖被解析到了特定版本——用于解决版本冲突。

--dependency <group:artifact>;--configuration <name>

gradle dependencyInsight --dependency com.fasterxml.jackson.core:jackson-databind
gradle buildEnvironment

打印构建脚本的解析后 classpath——构建自身用到的所有插件 / 辅助库。

gradle buildEnvironment
gradle refresh-dependencies

强制重新解析动态版本与 snapshot 依赖。

--refresh-keys

gradle build --refresh-dependencies

多项目

gradle projects

列出反应堆中的所有项目(根 + 子项目)及其项目路径与描述。

gradle projects
gradle :<module>:<task>

通过 Gradle 路径(`:` 作为分隔符)在某个子项目中运行任务。

-p <dir> 从子目录启动

gradle :order-service:test :web:assemble
gradle <task> --parallel

并行执行独立任务——对拥有独立测试套件的多模块构建很有用。

--max-workers=<N>;--no-parallel

gradle test --parallel --max-workers=4

性能

gradle --build-cache <task>

启用本地构建缓存(跨分支复用任务输出)。在 `settings.gradle` 中可搭配远程缓存用于 CI。

--no-build-cache

gradle assemble --build-cache
gradle --configuration-cache <task>

Gradle 8.x——缓存配置阶段,让大型多项目构建的启动达到亚毫秒级。

--no-configuration-cache

gradle test --configuration-cache
gradle --no-daemon <task>

禁用 Gradle daemon——适合低内存 CI 或排查只在 daemon 下出现的 bug。

gradle --no-daemon test
gradle --stop

停止所有正在运行的 Gradle daemon。若怀疑缓存状态可疑,请配合 clean 后重建。

gradle --stop && gradle clean build

诊断

gradle --scan <task>

运行任务后发布一个 build scan(结尾打印链接)。需要先同意一次 Build Scan 条款。

--scan -Dscan.tag.<name>=<value>

gradle assemble --scan
gradle --info <task>

启用 INFO 日志——任务开始 / 结束、up-to-date 检查、依赖解析。

--debug / -d 输出更详细

gradle test --info | tee gradle-info.log
gradle --stacktrace <task>

在任何失败时打印完整堆栈。

--warning-mode=all 显示所有弃用警告

gradle build --stacktrace

Wrapper

./gradlew <task>

通过 Gradle Wrapper 运行任务——使用 `gradle/wrapper/gradle-wrapper.properties` 中锁定的 Gradle 版本。

./gradlew clean build
gradle wrapper --gradle-version <v>

按指定 Gradle 发行版生成 / 刷新 wrapper 文件。

--distribution-type bin | all;--gradle-distribution-url <url>

gradle wrapper --gradle-version 8.7 --distribution-type bin

发布

gradle publish

将制品上传到 `build.gradle` 中 `publishing` 下声明的仓库。常见目标:通过 Sonatype 上 Maven Central,或私有 Nexus / Artifactory。

-Drelease.useAutomaticVersion=true;-PpublishTo=staging

gradle publishToMavenLocal
gradle publishToMavenLocal

把构建产物安装到本地 Maven 仓库(`~/.m2/repository`),方便相邻的 Maven 构建引用。

gradle publishToMavenLocal

杂项

gradle --version

打印 Gradle / JVM / OS 信息,含 daemon 状态。

gradle --version
gradle properties

列出每个项目属性(ext.*、gradle.*、system)——排查被覆盖的值时有用。

gradle properties | grep version
gradle init

引导一个新的 Gradle 项目:选择 DSL(Groovy / Kotlin)、挑选项目类型,可选绑定 Git remote。

--type basic;--type java-application;--type java-library;--dsl groovy|kotlin

gradle init --type java-library --dsl kotlin
gradle :<module>:tasks --group <name>

将任务列表过滤到单个任务分组(例如 `build`、`verification`、`documentation`、`help`)。

gradle tasks --group verification

相关命令速查

关于 Gradle

Gradle 是一款基于 JVM 的构建工具,2008 年首次发布(由 Hans Dockter 与 Gradleware / Gradle Inc 团队打造)。它的目标是把 Apache Ant 的表达能力与 Apache Ivy 的依赖管理结合起来,并包裹在一个最初基于 Groovy(如今也有 Kotlin DSL(`.kts`))的 DSL 之中。Gradle 驱动着 Android 构建(Android Studio 默认使用 Gradle)、Spring Boot starter、Kotlin Multiplatform 以及众多大型 JVM monorepo。当前稳定版本线是 Gradle 8.x(8.7+)。Gradle 在增量构建方面表现出色——基于任务输入 / 输出指纹识别、并行任务执行、构建缓存(本地 + 远程)以及配置缓存(8.x)。任务在 `build.gradle` / `build.gradle.kts` 中声明,并通过 `dependsOn` 串联成链;插件添加新任务(`java`、`java-library`、`application`、`org.springframework.boot`、`com.android.application`)。Gradle 不会上传源码——只上传 publishing 插件声明的制品。Wrapper(`gradlew`)打包了一个小启动器,会按 `gradle-wrapper.properties` 下载对应 Gradle 发行版,保证每一位开发者与 CI agent 都使用同一 Gradle 版本。

速查页版本 1.0.0