您可以使用 IntelliJ 原生匯入器Eclipse Buildship 在 IDE 中開啟此範例。

本指南示範如何使用 gradle init 以 Gradle 建立 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 : Groovy2: Kotlin。對於其他問題,請按 Enter 使用預設值。

輸出結果會如下所示

$ gradle init

Select type of project to generate:
  1: basic
  2: application
  3: library
  4: Gradle plugin
Enter selection (default: basic) [1..4] 1

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

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

Project name (default: demo):
Source package (default: demo):


BUILD SUCCESSFUL
2 actionable tasks: 2 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 設定檔,用於定義建置名稱和子專案
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 定義應用程式的 main 類別。
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 應用程式專案,請查看下列使用者手冊章節