brown elephant on green grass field during daytime

使用 Gradle 和 Apache Groovy

本文探讨了 Gradle,一个用于 Apache Groovy 项目的强大构建和文档工具。 了解如何…
首页 » 博客 » 使用 Gradle 和 Apache Groovy

本文将引导您了解如何使用多功能的构建工具 Gradle 构建 Apache Groovy 应用程序文件夹。我们将逐步引导您完成整个过程,确保您的项目拥有顺畅开发体验所需的所有基本组件。这种方法是对官方 Gradle 文档 的补充,为设置 Groovy 项目提供了另一种方法。

我们正接近这个系列的尾声,但您可以从任何地方开始,只要您已安装了 Groovy

在本文中,我们将使用 Gradle 构建工具创建一个 Groovy 应用程序文件夹,并包含所有必要的位和片段。

以下是我们开始的方式

$ mkdir Groovy24
$ cd Groovy24
$ gradle -version

------------------------------------------------------------
Gradle 7.4.2
------------------------------------------------------------

Build time:   2022-03-31 15:25:29 UTC
Revision:     540473b8118064efcc264694cbcaa4b677f61041

Kotlin:       1.5.31
Groovy:       3.0.9
Ant:          Apache Ant(TM) version 1.10.11 compiled on July 10 2021
JVM:          1.8.0_292 (Oracle Corporation 25.292-b10)
OS:           Linux 6.2.0-25-generic amd64

clh@montpellier:~/Dropbox/MyStuff/Writing/GroovySeries/Groovy24$ gradle init
Starting a Gradle Daemon (subsequent builds will be faster)

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

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

Split functionality across multiple subprojects?:
  1: no - only one application project
  2: yes - application and library projects
Enter selection (default: no - only one application project) [1..2] 1

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

Generate build using new APIs and behavior (some features may change in the next minor release)? (default: no) [yes, no] 
Project name (default: Groovy24):     
Source package (default: Groovy24): org.opensource

> Task :init
Get more help with your project: https://docs.gradle.org.cn/7.4.2/samples/sample_building_groovy_applications.html

BUILD SUCCESSFUL in 2m 36s
2 actionable tasks: 2 executed
$

回顾以上内容,我们创建了一个名为 org.opensource.Groovy24 的 Groovy 应用程序。以下是 Gradle 在 Groovy24 目录中创建的内容

app/src/main/.../org/opensource 目录中,我们看到了 App.groovy,这是我们的启动应用程序。

同时,在 app/src/test/… 目录中,我们可以看到 AppTest.groovy,这是我们应用程序的 Spock 测试规范。

app/src/build.gradle 是 Gradle 构建脚本。

最后,倒数第三个是 gradlew shell 脚本,它将管理所有构建、测试和相关任务。 运行该脚本

$ ./gradlew tasks

我们收到了 Gradle 可以代表我们执行的所有任务的摘要,包括运行应用程序、构建应用程序、分发应用程序、记录应用程序和运行测试,以及各种帮助工具。

让我们尝试几个任务。 首先,让我们使用 build 任务来组装和构建项目

$ ./gradlew build

我们看到很多中间过程(简短地)发生,最后收到此消息

BUILD SUCCESSFUL in 8s 7 actionable tasks: 7 executed

这很简洁但令人放心! 如果构建或测试失败,我们将收到有关检测到的问题的更多信息。

让我们运行应用程序

$ ./gradlew run

> Task :app:run
Hello World!

BUILD SUCCESSFUL in 720ms
2 actionable tasks: 1 executed, 1 up-to-date

咦,“Hello World!”,真是令人惊讶! 让我们看看 App.groovy 文件

     1	/*
     2	 * This Groovy source file was generated by the Gradle 'init' task.
     3	 */
     4	package org.opensource
       
     5	class App {
     6	    String getGreeting() {
     7	        return 'Hello World!'
     8	    }
       
     9	    static void main(String[] args) {
    10	        println new App().greeting
    11	    }
    12	}

考虑到代码,这个结果看起来是合理的。

此时,我们已准备好继续前进,使我们的应用程序成为现实。 鉴于 Gradle 已经为我们设置了启动器 AppTest.groovy,现在可能是开始沿着 测试驱动开发方法论 的思路进行思考的好时机。

还有必要查看 build.gradle 文件,当我们发现需要引入其他依赖项(例如非标准库)时,我们将需要增强该文件。

1   /*
2   * This file was generated by the Gradle 'init' task.
3   * This generated file contains a sample Groovy application project
4   * For more details, read 'Building Java & JVM projects' chapter 
5   * in https://docs.gradle.org.cn/7.4.2/userguide
6   * /building_java_projects.html
7   */
8   plugins {
9    // Apply the groovy Plugin to add support for Groovy.
10    id 'groovy'
11    // application plugin adds support for building a CLI
12    id 'application'
13   }
14   repositories {
15	    // Use Maven Central for resolving dependencies.
16	    mavenCentral()
17	}
18   dependencies {
19   // Use the latest Groovy version for building this library
20   // implementation 'org.codehaus.groovy:groovy-all:3.0.9'
21   // This dependency is used by the application.
22   // implementation 'com.google.guava:guava:30.1.1-jre'
23   // Use Spock testing and specification framework even with Java
24   testImplementation 'org.spockframework:spock-core:2.0-groovy-3.0'
25   testImplementation 'junit:junit:4.13.2'
26   }
27   application {
28     // Define the main class for the application.
29     mainClass = 'org.opensource.App'
30   }
31   tasks.named('test') {
32   // Use JUnit Platform for unit tests.
33   useJUnitPlatform()
34   }

请注意上面的“dependencies”节。 此外,如果我们决定为我们的应用程序使用比“App”更好的名称,除了重命名 App.groovyAppTest.groovy 之外,我们还需要在 build.gradle 文件中进行更改。

结论

Gradle 是一个用于各种项目(包括 Groovy)的强大构建和文档工具。 它与 Maven Central(在上一篇关于 Grape 的文章中介绍)和更广泛的 Maven 生态系统无缝集成。

虽然对于简单的 Groovy 脚本,我可能不会总是选择 Gradle,这似乎很轻量级,但它是构建 Grails Web 应用程序的关键组件。 在这种情况下,Gradle 是一项宝贵的资产。

作者

如果您喜欢这篇文章,您可能也会喜欢这些