brown elephant on green grass field during daytime

Use Gradle with Apache Groovy

This article explores Gradle, a powerful build and documentation tool for Apache Groovy projects. Learn how…
Home » Blog » Use Gradle with Apache Groovy

This article takes you on a guided tour of building an Apache Groovy application folder using Gradle, the versatile build tool. We’ll walk you through the process step-by-step, ensuring your project has all the essential components for a smooth development experience. This approach complements the official Gradle documentation, offering an alternative take on setting up your Groovy project.

We’re nearing the end of this series, but you can start anywhere, as long as you have installed Groovy.

In this article, we’ll use the Gradle build tool to create a Groovy application folder and include all the necessary bits and pieces.

Here’s how we start:

$ 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/7.4.2/samples/sample_building_groovy_applications.html

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

Reviewing the above, we created a Groovy application called org.opensource.Groovy24. Here’s what Gradle created in that Groovy24 directory:

In the app/src/main/.../org/opensource directory, we see App.groovy, which is our starter application.

In parallel, in the app/src/test/… directory we can see AppTest.groovy, which is the Spock test specification for our application.

The app/src/build.gradle is the Gradle build script.

Finally, third from the bottom is the gradlew shell script that will manage all the build, test and related tasks. Run that script:

$ ./gradlew tasks

We receive a summary of all the tasks that Gradle can execute on our behalf, including running the application, building it, distributing it, documenting it, and running tests, as well as various help facilities.

Let’s try a couple of tasks. First, let’s use the build task to assemble and build the project:

$ ./gradlew build

We see a lot of intermediate stuff going on (briefly) and finally receive this message:

BUILD SUCCESSFUL in 8s 7 actionable tasks: 7 executed

This is brief but reassuring! If the build or the test failed, we would have received more information on the problems detected.

Let’s run the application:

$ ./gradlew run

> Task :app:run
Hello World!

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

Huh, “Hello World!”, what a surprise! Let’s look at the App.groovy file:

     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	}

That result looks plausible, given the code.

At this point, we are ready to move ahead to make our application real. Given that Gradle has set us up with a starter AppTest.groovy, this could be a good moment to start thinking along the lines of Test Driven Development methodology.

It’s also worth having a look at the build.gradle file, which we will need to enhance as we find we need to bring in other dependencies such as non-standard libraries:

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/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   }

Note the “dependencies” stanza above. Also, if we decide we want a better name than “App” for our application, besides renaming App.groovy and AppTest.groovy, we need to make changes in the build.gradle file.

Conclusion

Gradle is a powerful build and documentation tool for various projects, including Groovy. It seamlessly integrates with Maven Central (covered in the previous article on Grape) and the broader Maven ecosystem.

While I might not always reach for Gradle for simple Groovy scripts, which can seem lightweight, it’s a crucial component for building Grails web applications. In that context, Gradle is an invaluable asset.

Author

If you enjoyed this post, you might also enjoy these