Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run groovy script from within gradle

What's the best way to create a gradle task, which runs a groovy script? I realize that gradle build files are groovy, so I would think it would be possible to do something like this:

task run << {
    Script app = new GroovyShell().parse(new File("examples/foo.groovy"))
    // or replace .parse() w/ a .evalulate()?
    app.run()
}

I get all kinds of whacky errors when I try this if bar.groovy is using @Grab annotations or even doing simple imports. I want to create a gradle task to handle this, so that I can hopefully reuse the classpath definition.

Would it be better to move the examples directory into the src directory somewhere? What's a best practice?

like image 713
macattack Avatar asked Mar 02 '10 02:03

macattack


People also ask

Does Gradle use Groovy?

Dependency management Because Gradle's build language is based on Groovy, and parts of Gradle are implemented in Groovy, Gradle already ships with a Groovy library. Nevertheless, Groovy projects need to explicitly declare a Groovy dependency. This dependency will then be used on compile and runtime class paths.

What is Buildscript in Gradle?

The "buildscript" configuration section is for gradle itself (i.e. changes to how gradle is able to perform the build). So this section will usually include the Android Gradle plugin. Follow this answer to receive notifications.


1 Answers

Or you could do:

new GroovyShell().run(file('somePath'))
like image 71
Hans Dockter Avatar answered Oct 20 '22 15:10

Hans Dockter