How to set up a new groovy project in gradle

I was writing a groovy script today that is likely to grow into a larger project and that might need to be run by non-groovy programmers, which meant it was time to break out gradle.

Here’s the full recipe:

Install gvm, with the first command, the second is the command that the install script gives you for setting your environment variables.

curl -s | bash
source "~/.gvm/bin/"  # will vary depending on your Operating System

gvm install gradle

create a build.gradle file and put this line in to grab some project templates

apply from: ''
gradle createGroovyProject

This asks for a project name and some details and creates a directory with the same name as the project.

I found a nice script for bundling a Groovy script into a jar at so I replaced the <project_name>/build.gradle with that.  Also set the main class name to: .SomeScript

Next, I put my Groovy script (SomeScript.groovy) below the <project_name>/src/main/groovy directory.

gradle build

Now anyone with java installed can run the script

java -jar build/libs/<project_name>-1.0.jar

Which gave me my expected output. This worked with either a groovy class with a static void main method or simple script:

package mypackage
println "Hello World"

Last I added the gradle wrapper by adding a task to the build.gradle file so that anyone can check out the project  and build it right away by running gradlew or gradlew.bat

task wrapper(type: Wrapper) {
    gradleVersion = '1.5'

and running

gradle wrapper

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s