41

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.

2
  • And what prevents you from backporting your script to work with groovy 1.8.x? Commented Jun 28, 2013 at 12:02
  • 5
    «Groovy 1.8.x cant parse large XML files» is completely false Commented Jun 28, 2013 at 13:16

2 Answers 2

81

You can create directory 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
Sign up to request clarification or add additional context in comments.

Comments

0

If you want to run scripts that uses @Grab you have to add ivy to compiler task

configurations {
    ivy
}

dependencies {
    ivy 'org.apache.ivy:ivy:2.4.0'
}

tasks.withType(GroovyCompile) {
    groovyClasspath += configurations.ivy
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.