您可以在支持 Gradle 的 IDE 中打开此示例。 |
本指南演示了如何使用 gradle init
通过 Gradle 创建 Groovy 应用程序。您可以按照本指南一步一步从头创建一个新项目,或者使用上面的链接下载完整的示例项目。
您将构建什么
您将生成一个遵循 Gradle 约定的 Groovy 应用程序。
您需要什么
-
文本编辑器或 IDE - 例如 IntelliJ IDEA
-
Java 开发工具包 (JDK),版本 8 或更高 - 例如 AdoptOpenJDK
-
最新的 Gradle 发行版
创建项目文件夹
Gradle 带有一个名为 init
的内置任务,它在一个空文件夹中初始化一个新的 Gradle 项目。init
任务使用(同样内置的)wrapper
任务来创建 Gradle wrapper 脚本 gradlew
。
第一步是为新项目创建一个文件夹并进入该文件夹。
$ mkdir demo $ cd demo
运行 init 任务
在新项目目录中,在终端中使用以下命令运行 init
任务:gradle init
。当出现提示时,选择 1: application
项目类型和 3: Groovy
作为实现语言。接下来您可以选择用于编写构建脚本的 DSL - 1 : Kotlin
或 2: Groovy
。对于其他问题,按回车键使用默认值。
输出将如下所示
$ 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)
文件中有两行值得关注
rootProject.name = "demo"
include("app")
rootProject.name = 'demo'
include('app')
-
rootProject.name
为构建分配一个名称,这会覆盖以所在目录命名构建的默认行为。建议设置一个固定名称,因为项目共享时(例如作为 Git 仓库的根目录)文件夹可能会更改。 -
include("app")
定义了构建包含一个名为app
的子项目,该子项目包含实际代码和构建逻辑。可以通过额外的include(…)
语句添加更多子项目。
我们的构建包含一个名为 app
的子项目,它代表我们正在构建的 Groovy 应用程序。它在 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)
}
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 | 使用强大的 Spock 测试和规范框架,即使与 Java 一起使用 |
7 | 定义应用程序的主类。 |
8 | 使用 JUnit Platform 进行单元测试。 |
文件 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
接下来显示
/*
* 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.tar
和 app/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 应用程序项目,请查看以下用户手册章节