Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running Groovy scripts from Gradle

Tags:

gradle

groovy

I am using Gradle 1.6 which comes with Groovy 1.8.6 and here comes the problem, I want to execute groovy script which need Groovy 2+, but Gradle is running this script with his own groovy (1.8.6) and my custom task is failing.

like image 209
IowA Avatar asked Jun 28 '13 08:06

IowA


People also ask

Does Gradle use Groovy?

gradle is a Groovy script. Thus it can execute arbitrary code and access any Java library, build-specific Gradle DSL and the Gradle API.

What is Groovy plugin in Gradle?

The Groovy plugin extends the Java plugin to add support for Groovy projects. It can deal with Groovy code, mixed Groovy and Java code, and even pure Java code (although we don't necessarily recommend to use it for the latter).


1 Answers

You can create src/main/groovy, put your script called 'myscript.groovy' in there:

println "hello world from groovy version ${GroovySystem.version}" 

Then, have a build.gradle file in your project root directory:

apply plugin: 'groovy'  repositories {     mavenCentral() }  dependencies {     compile 'org.codehaus.groovy:groovy-all:2.0.5' }  task runScript (dependsOn: 'classes', type: JavaExec) {     main = 'myscript'     classpath = sourceSets.main.runtimeClasspath } 

Then, you can execute your script (with output)

hw@hbook:ex $ gradle runScript :compileJava UP-TO-DATE :compileGroovy :processResources UP-TO-DATE :classes :runScript hello world from groovy version 2.0.5  BUILD SUCCESSFUL  Total time: 6.118 secs 
like image 74
Hans Westerbeek Avatar answered Sep 29 '22 08:09

Hans Westerbeek