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, http://gvmtool.net/ with the first command, the second is the command that the install script gives you for setting your environment variables.
curl -s get.gvmtool.net | bash
source "~/.gvm/bin/gvm-init.sh" # 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: 'http://www.tellurianring.com/projects/gradle-plugins/gradle-templates/apply.groovy' 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 http://scottfrederick.blogspot.com/2011/11/wrapping-groovy-script-with-gradle.html 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