I'm playing around with the samples directory that comes with Gradle and trying to create a simple task that runs a groovy script which depends on another script.
The structure of the project looks like this:
.
├── build.gradle
└── src
├── main
│ ├── groovy
│ │ └── org
│ │ └── gradle
│ │ └── Person.groovy
│ └── resources
│ ├── resource.txt
│ └── script.groovy
└── test
├── groovy
│ └── org
│ └── gradle
│ └── PersonTest.groovy
└── resources
├── testResource.txt
└── testScript.groovy
I've added the following task in build.gradle
task runScript << {
new GroovyShell().run(file('src/main/resources/script.groovy'))
}
The error I'm getting is:
FAILURE: Build failed with an exception.
* Where:
Build file '/gradle/gradle-1.0/samples/groovy/quickstart/build.gradle' line: 13
* What went wrong:
Execution failed for task ':runScript'.
Cause: No such property: person for class: script
contents of script.groovy are:
person.name = person.name[0].toUpperCase() + person.name[1..-1]
contents of Person.groovy are:
package org.gradle
class Person {
String name
def Person() {
getClass().getResourceAsStream('/resource.txt').withStream {InputStream str ->
name = str.text.trim()
}
getClass().getResourceAsStream('/script.groovy').withStream {InputStream str ->
def shell = new GroovyShell()
shell.person = this
shell.evaluate(str.text)
}
}
}
Question
How can I add a task that would simply run script.groovy which makes use of another groovy class (Person)
You can use buildSrc folder where you can hold Groovy / Java sources that will be compiled before executing any task in your initial build script and added to the class path. That will make all those classes available to be used in the tasks. You can read more about it in: http://gradle.org/docs/current/userguide/organizing_build_logic.html
Regarding your script.grooovy I would just put the code into the task itself rather than calling it through GroovyShell. If you want to externalize the task you can use apply command.
apply from: 'script.gradle'
You can take a look at this question: How can I import one Gradle script into another?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With