gradle

Gradle 8+

Gradle CLI 命令快速參考:專案 task、依賴管理、多專案建置、wrapper、configuration cache、build scan——附常用參數與實作範例。

30 條命令

Tasks

gradle tasks

列出當前建置定義的所有 task,依類別分組。`--all` 也顯示內部/隱藏 task。

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

gradle tasks --all
gradle help

印出指定 task 的說明:描述、群組、宣告的輸入/輸出。

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

列出多專案建置中某個子專案的 task(把 `<module>` 換成 Gradle 路徑)。

gradle :web:tasks --all

生命週期

gradle build

執行標準 JVM 建置鏈:`compile → processResources → classes → jar → test → check → assemble`(如有設定也包含 zip 發行版)。

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

gradle build -x test
gradle clean

刪除建置目錄(`build/`),讓下次建置從頭執行所有 task。

--no-rebuild 不要自動觸發預設生命週期

gradle clean build
gradle assemble

編譯並打包所有產物,但略過驗證/測試 task。只需要 jar 時可加速回饋。

gradle assemble
gradle check

執行所有驗證 task(`test`、`lint` 等),但不產生 jar。CI 中作為合併前的關卡。

--continue 即使某 task 失敗也繼續

gradle check --continue
gradle test

執行單元測試(Test task)。使用 `--tests <pattern>` 限定特定類別/方法。

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

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

依賴

gradle dependencies

印出某個 configuration 的已解析依賴樹(例如 `compileClasspath`、`runtimeClasspath`、`testCompileClasspath`)。

--configuration <name>;--all 顯示所有 config

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——建置本身用到的所有 plugin/輔助函式庫。

gradle buildEnvironment
gradle refresh-dependencies

強制重新解析動態版本與 snapshot 依賴。

--refresh-keys

gradle build --refresh-dependencies

多專案

gradle projects

列出反應器中的所有專案(根 + 子專案)及其專案路徑與描述。

gradle projects
gradle :<module>:<task>

依 Gradle 路徑(以 `:` 分隔)在某個子專案中執行 task。

-p <dir> 從子目錄啟動

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

平行執行獨立的 task——適用於測試套件彼此獨立的多模組建置。

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

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

效能

gradle --build-cache <task>

啟用本地建置快取(跨分支/分支重用 task 輸出)。在 `settings.gradle` 中組合遠端快取可在 CI 使用。

--no-build-cache

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

Gradle 8.x——快取 configuration 階段,把大型多專案建置的啟動時間降到次毫秒級。

--no-configuration-cache

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

停用 Gradle daemon——適合低記憶體的 CI,或用來診斷只在 daemon 下出現的 bug。

gradle --no-daemon test
gradle --stop

停止所有執行中的 Gradle daemon。如果快取狀態可疑,可與乾淨重建搭配。

gradle --stop && gradle clean build

診斷

gradle --scan <task>

執行 task 後發佈 build scan(結束時印出連結)。需先同意 Build Scan 條款。

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

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

啟用 INFO 等級日誌——task 開始/結束、最新狀態檢查、依賴解析。

--debug / -d 更詳細

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

任何失敗時印出完整堆疊追蹤。

--warning-mode=all 顯示所有棄用警告

gradle build --stacktrace

Wrapper

./gradlew <task>

透過 Gradle Wrapper 執行 task——使用 `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 遠端。

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

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

把 task 清單過濾為單一群組(例如 `build`、`verification`、`documentation`、`help`)。

gradle tasks --group verification

相關命令速查

關於 Gradle

Gradle 是以 JVM 為基礎的建置工具,於 2008 年首次發佈(由 Hans Dockter 與 Gradleware/Gradle Inc 團隊)。它的目標是結合 Apache Ant 的表達能力與 Apache Ivy 的依賴管理,並以 Groovy DSL(現在也提供 Kotlin DSL,`.kts`)封裝。Gradle 是 Android 建置(Android Studio 預設使用)、Spring Boot starter、Kotlin Multiplatform,以及許多大型 JVM monorepo 的核心。目前穩定版線為 Gradle 8.x(8.7+)。Gradle 透過 task 輸入/輸出指紋機制、平行 task 執行、建置快取(本地 + 遠端)以及 configuration cache(8.x)實現卓越的增量建置。Task 宣告於 `build.gradle`/`build.gradle.kts`,並以 `dependsOn` 串接;plugin 會新增 task(`java`、`java-library`、`application`、`org.springframework.boot`、`com.android.application`)。Gradle 不會上傳源碼——只會發佈由發佈 plugin 宣告的構件。Wrapper(`gradlew`)是一個小啟動器,會下載 `gradle-wrapper.properties` 中鎖定的 Gradle 發行版,確保每位開發者與 CI 代理都使用相同的 Gradle 版本。

速查頁版本 1.0.0