您可以使用 支援 Gradle 的 IDE 開啟此範例。

本指南示範如何使用 Gradle 和 gradle init 建立 Groovy 應用程式。您可以按照指南逐步從頭開始建立新專案,或使用上方的連結下載完整的範例專案。

您將建立什麼

您將產生一個遵循 Gradle 約定的 Groovy 應用程式。

您需要什麼

建立專案資料夾

Gradle 內建一個名為 init 的任務,用於在空資料夾中初始化新的 Gradle 專案。init 任務使用(同樣內建的)wrapper 任務來建立 Gradle wrapper 腳本 gradlew

第一步是為新專案建立一個資料夾,然後將目錄變更到該資料夾。

$ mkdir demo
$ cd demo

執行 init 任務

從新專案目錄中,在終端機中使用以下命令執行 init 任務:gradle init。出現提示時,選擇 1: application 專案類型和 3: Groovy 作為實作語言。接下來,您可以選擇用於撰寫建置腳本的 DSL - 1 : Kotlin2: Groovy。對於其他問題,按 Enter 鍵使用預設值。

輸出結果如下

$ gradle init

Select type of build to generate:
  1: Application
  2: Library
  3: Gradle plugin
  4: Basic (build structure only)
Enter selection (default: Application) [1..4] 1

Select implementation language:
  1: Java
  2: Kotlin
  3: Groovy
  4: Scala
  5: C++
  6: Swift
Enter selection (default: Java) [1..6] 3

Enter target Java version (min: 7, default: 21):

Project name (default: demo):

Select application structure:
  1: Single application project
  2: Application and library project
Enter selection (default: Single application project) [1..2] 1

Select build script DSL:
  1: Kotlin
  2: Groovy
Enter selection (default: Kotlin) [1..2]

Generate build using new APIs and behavior (some features may change in the next minor release)? (default: no) [yes, no]

BUILD SUCCESSFUL
1 actionable task: 1 executed

init 任務使用以下結構產生新專案

├── gradle (1)
│   ├── libs.versions.toml (2)
│   └── wrapper
│       ├── gradle-wrapper.jar
│       └── gradle-wrapper.properties
├── gradlew (3)
├── gradlew.bat (3)
├── settings.gradle.kts (4)
└── app
    ├── build.gradle.kts (5)
    └── src
        ├── main
        │   └── groovy (6)
        │       └── demo
        │           └── App.groovy
        └── test
            └── groovy (7)
                └── demo
                    └── AppTest.groovy
├── gradle (1)
│   ├── libs.versions.toml (2)
│   └── wrapper
│       ├── gradle-wrapper.jar
│       └── gradle-wrapper.properties
├── gradlew (3)
├── gradlew.bat (3)
├── settings.gradle (4)
└── app
    ├── build.gradle (5)
    └── src
        ├── main
        │   └── groovy (6)
        │       └── demo
        │           └── App.groovy
        └── test
            └── groovy (7)
                └── demo
                    └── AppTest.groovy
1 為 wrapper 檔案產生的資料夾
2 產生的版本目錄
3 Gradle wrapper 啟動腳本
4 用於定義建置名稱和子專案的 Settings 檔案
5 app 專案的建置腳本
6 預設 Groovy 原始碼資料夾
7 預設 Groovy 測試原始碼資料夾

您現在已設定專案以建置 Groovy 應用程式。

檢閱專案檔案

settings.gradle(.kts) 檔案有兩個有趣的行

settings.gradle.kts
rootProject.name = "demo"
include("app")
settings.gradle
rootProject.name = 'demo'
include('app')
  • rootProject.name 為建置指定名稱,這會覆寫預設行為,即以目錄名稱命名建置。建議設定固定的名稱,因為如果專案被共享(例如作為 Git 儲存庫的根目錄),則資料夾可能會變更。

  • include("app") 定義建置包含一個名為 app 的子專案,其中包含實際程式碼和建置邏輯。可以透過額外的 include(…​) 語句新增更多子專案。

我們的建置包含一個名為 app 的子專案,代表我們正在建置的 Groovy 應用程式。它在 app/build.gradle(.kts) 檔案中配置

app/build.gradle.kts
plugins {
    groovy (1)
    application (2)
}

repositories {
    mavenCentral() (3)
}

dependencies {
    implementation(libs.groovy.all) (4)

    implementation(libs.guava) (5)

    testImplementation(libs.spock.core) (6)
    testImplementation(libs.junit)

    testRuntimeOnly("org.junit.platform:junit-platform-launcher")
}

application {
    mainClass = "demo.App" (7)
}

tasks.named<Test>("test") {
    useJUnitPlatform() (8)
}
app/build.gradle
plugins {
    id 'groovy' (1)
    id 'application' (2)
}

repositories {
    mavenCentral() (3)
}

dependencies {
    implementation libs.groovy.all (4)

    implementation libs.guava (5)

    testImplementation libs.spock.core (6)
    testImplementation libs.junit

    testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
}

application {
    mainClass = 'demo.App' (7)
}

tasks.named('test') {
    useJUnitPlatform() (8)
}
1 套用 groovy 外掛程式以新增對 Groovy 的支援。
2 套用 application 外掛程式以新增對在 Java 中建置 CLI 應用程式的支援。
3 使用 Maven Central 解析依賴關係。
4 使用最新的 Groovy 版本來建置此函式庫
5 應用程式使用此依賴關係。
6 即使使用 Java,也使用超棒的 Spock 測試和規格框架
7 定義應用程式的主要類別。
8 使用 JUnit Platform 進行單元測試。

檔案 src/main/groovy/demo/App.groovy 如下所示

產生的 src/main/groovy/demo/App.groovy
/*
 * This source file was generated by the Gradle 'init' task
 */
package demo

class App {
    String getGreeting() {
        return 'Hello World!'
    }

    static void main(String[] args) {
        println new App().greeting
    }
}

產生的測試,src/test/groovy/demo/App.groovy 如下所示

產生的 src/test/groovy/demo/AppTest.groovy
/*
 * This source file was generated by the Gradle 'init' task
 */
package demo

import spock.lang.Specification

class AppTest extends Specification {
    def "application has a greeting"() {
        setup:
        def app = new App()

        when:
        def result = app.greeting

        then:
        result != null
    }
}

產生的測試類別有一個單一的 Spock 測試。該測試實例化 App 類別,在其上調用一個方法,並檢查它是否返回預期的值。

執行應用程式

由於 application 外掛程式,您可以直接從命令列執行應用程式。run 任務告知 Gradle 執行分配給 mainClass 屬性的類別中的 main 方法。

$ ./gradlew run

> Task :app:run
Hello world!

BUILD SUCCESSFUL
2 actionable tasks: 2 executed
第一次執行 wrapper 腳本 gradlew 時,可能會有一段延遲,因為該版本的 gradle 正在下載並本地儲存在您的 ~/.gradle/wrapper/dists 資料夾中。

打包應用程式

application 外掛程式也會為您打包應用程式及其所有依賴關係。該封存檔也將包含一個腳本,用於使用單一命令啟動應用程式。

$ ./gradlew build

BUILD SUCCESSFUL in 0s
7 actionable tasks: 7 executed

如果您如上所示執行完整建置,Gradle 將為您產生兩種格式的封存檔:app/build/distributions/app.tarapp/build/distributions/app.zip

發布建置掃描

要深入了解您的建置在幕後執行的操作,最好的方法是發布 建置掃描。若要執行此操作,只需使用 --scan 標誌執行 Gradle。

$ ./gradlew build --scan

BUILD SUCCESSFUL in 0s
7 actionable tasks: 7 executed

Publishing a build scan to scans.gradle.com requires accepting the Gradle Terms of Service defined at https://gradle.com/terms-of-service.
Do you accept these terms? [yes, no] yes

Gradle Terms of Service accepted.

Publishing build scan...
https://gradle.com/s/5u4w3gxeurtd2

點擊連結並探索執行了哪些任務、下載了哪些依賴關係以及更多詳細資訊!

摘要

就是這樣!您現在已成功使用 Gradle 配置和建置 Groovy 應用程式專案。您已學會如何

  • 初始化一個產生 Groovy 應用程式的專案

  • 執行建置並檢視測試報告

  • 使用 application 外掛程式中的 run 任務執行 Groovy 應用程式

  • 將應用程式打包到封存檔中

後續步驟

若要深入了解如何進一步自訂 Groovy 應用程式專案,請查看以下使用者手冊章節