Getting Started with Groovy Applications and the Gradle Init Plugin

When working with groovy, there are several ways to get started.  Most tutorials, including my own workshops, use the GroovyConsole as a way to write small scripts and then save them to run later.

What comes after that though? How can one build a slightly larger example without all the excess of a framework like Spring Boot or Grails?

With this article, I will show you how to use the Gradle init plugin to create a small runnable, testable Groovy application.

Before we start

you should have a local installation of Gradle 4.0+. If you don't have Gradle installed or your version is less than 4.0, I highly suggest you manage your Gradle installations with SDKMAN.

In a clean directory

run the following command: 

 gradle init --type groovy-application  

After running that command, you should see several new files.

The Gradle files are for building and running the project and your project code will be in the src directory.

To start working

open the project in an IDE such as IntelliJ IDEA. Click open and select the directory you created.  Then use the default settings like this:

If you open the project directory, you should see the main App class and the AppTest Spock test class.

If you open App.groovy you will see

 /*  
  * This Groovy source file was generated by the Gradle 'init' task.  
  */  
 class App {  
   String getGreeting() {  
     return 'Hello world.'  
   }  
   static void main(String[] args) {  
     println new App().greeting  
   }  
 }  

Just like with Java, the static void main function is what will run in this application.

To run the App

use the Gradle wrapper. On MacOS and Linux that's

 ./gradlew run

or for Windows, replace ./gradlew with gradlew or gradlew.bat depending on your setup. That command will print `Hello world.` in the console.

To test the App

use the test or check command.  The check task runs all the tests and any other checks you may have configured like linters, static analysis, code coverage reports, etc.

 /*  
  * This Spock specification was generated by the Gradle 'init' task.  
  */  
 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  
   }  
 }  

The test provided passes by default. If you wanted to make this test a little better, you could change the expected result and modify the greeting implementation to take different inputs.

Next Steps

Now that you have a basic application set up, you can change the code in App and start adding more classes to expand your example. But don't forget to also update the tests!

To see an in-depth example, check out the next post in the series: Creating a Basic Calculator that reads from Standard Input

Comments

  1. Great blog post to get you started on a new Gradle project! For anyone interested in generating a new project from the browser, check out Gradle Initializr: https://gradle-initializr.cleverapps.io. Doesn't even require installing the Gradle runtime.

    ReplyDelete

Post a Comment