gradle
Gradle 8+Gradle CLI 命令快速參考:專案 task、依賴管理、多專案建置、wrapper、configuration cache、build scan——附常用參數與實作範例。
30 條命令
Tasks
gradle tasks列出當前建置定義的所有 task,依類別分組。`--all` 也顯示內部/隱藏 task。
--all;--group=<group>;--quiet
gradle tasks --allgradle help印出指定 task 的說明:描述、群組、宣告的輸入/輸出。
gradle help --task buildgradle :<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 testgradle clean刪除建置目錄(`build/`),讓下次建置從頭執行所有 task。
--no-rebuild 不要自動觸發預設生命週期
gradle clean buildgradle assemble編譯並打包所有產物,但略過驗證/測試 task。只需要 jar 時可加速回饋。
gradle assemblegradle check執行所有驗證 task(`test`、`lint` 等),但不產生 jar。CI 中作為合併前的關卡。
--continue 即使某 task 失敗也繼續
gradle check --continuegradle 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 slf4jgradle dependencyInsight --dependency <id>說明某個依賴為何會落在特定版本——解決版本衝突時很有用。
--dependency <group:artifact>;--configuration <name>
gradle dependencyInsight --dependency com.fasterxml.jackson.core:jackson-databindgradle buildEnvironment印出已解析的建置腳本 classpath——建置本身用到的所有 plugin/輔助函式庫。
gradle buildEnvironmentgradle refresh-dependencies強制重新解析動態版本與 snapshot 依賴。
--refresh-keys
gradle build --refresh-dependencies多專案
gradle projects列出反應器中的所有專案(根 + 子專案)及其專案路徑與描述。
gradle projectsgradle :<module>:<task>依 Gradle 路徑(以 `:` 分隔)在某個子專案中執行 task。
-p <dir> 從子目錄啟動
gradle :order-service:test :web:assemblegradle <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-cachegradle --configuration-cache <task>Gradle 8.x——快取 configuration 階段,把大型多專案建置的啟動時間降到次毫秒級。
--no-configuration-cache
gradle test --configuration-cachegradle --no-daemon <task>停用 Gradle daemon——適合低記憶體的 CI,或用來診斷只在 daemon 下出現的 bug。
gradle --no-daemon testgradle --stop停止所有執行中的 Gradle daemon。如果快取狀態可疑,可與乾淨重建搭配。
gradle --stop && gradle clean build診斷
gradle --scan <task>執行 task 後發佈 build scan(結束時印出連結)。需先同意 Build Scan 條款。
--scan -Dscan.tag.<name>=<value>
gradle assemble --scangradle --info <task>啟用 INFO 等級日誌——task 開始/結束、最新狀態檢查、依賴解析。
--debug / -d 更詳細
gradle test --info | tee gradle-info.loggradle --stacktrace <task>任何失敗時印出完整堆疊追蹤。
--warning-mode=all 顯示所有棄用警告
gradle build --stacktraceWrapper
./gradlew <task>透過 Gradle Wrapper 執行 task——使用 `gradle/wrapper/gradle-wrapper.properties` 中鎖定的 Gradle 版本。
./gradlew clean buildgradle 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 publishToMavenLocalgradle publishToMavenLocal把建置結果安裝到本地 Maven 儲存庫(`~/.m2/repository`),讓隔壁的 Maven 建置可以依賴它。
gradle publishToMavenLocal雜項
gradle --version印出 Gradle/JVM/OS 資訊,含 daemon 狀態。
gradle --versiongradle properties列出所有專案屬性(ext.*、gradle.*、system)——排查覆寫值時很有用。
gradle properties | grep versiongradle init引導新 Gradle 專案:選擇 DSL(Groovy/Kotlin)、挑選專案類型、可選地掛上 Git 遠端。
--type basic;--type java-application;--type java-library;--dsl groovy|kotlin
gradle init --type java-library --dsl kotlingradle :<module>:tasks --group <name>把 task 清單過濾為單一群組(例如 `build`、`verification`、`documentation`、`help`)。
gradle tasks --group verification相關命令速查
商標與使用聲明
本批命令列工具速查手冊為開源社群參考資料。文中涉及的各工具的名稱、標識、商標(如 Git™、Docker™、Kubernetes®、kubectl、PostgreSQL®、MySQL®、Redis™、MongoDB®、Linux™、PowerShell™、Vim™ 等)均歸對應權利人所有,此處僅為識別與參考之目的使用。
本速查手冊的全部內容均為獨立編寫的簡化自包含參考,**與任何官方文件不存在隸屬、被認可或引用關係**。命令列表、參數說明、範例均為作者原創簡化表達;如需權威資訊,請參閱各工具的官方文件。
命令語法與參數可能在不同工具版本中有所差異。所示版本標籤對應穩定發佈版本;其他版本行為可能有所不同。本速查手冊**不代表**商標權利人的任何偏好或推薦。
如商標或版權方認為本站部分內容存在權益侵害,請發送電郵至[email protected],我們在收到有效權屬證明後,會在合理期限內修改或移除相關內容。
本網站不對因使用本站資訊而產生的任何直接或間接損失承擔法律責任。
關於 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