Running Groovy scripts from Gradle

前端 未结 1 1715
夕颜
夕颜 2020-12-04 23:57

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

相关标签:
1条回答
  • 2020-12-05 00:26

    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
    
    0 讨论(0)
提交回复
热议问题