Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
250 views
in Technique[技术] by (71.8m points)

Running Groovy scripts from Gradle

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.

question from:https://stackoverflow.com/questions/17360719/running-groovy-scripts-from-gradle

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...