1

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)

Omnipresent
  • 29,434
  • 47
  • 142
  • 186
  • There are multiple problems with this 'solution'. Can you give the bigger picture of what you are trying to achieve? – Peter Niederwieser Jan 05 '12 at 13:13
  • I'm just starting out with gradle. So if the question just doesn't make sense, I apologize. All I want to do is place some code in `script.groovy` and run it with my new task. If I change contents of `script.groovy` to simply `println "test"` then it works but If I change it to `person = new Person()` then it says `unable to resolve class Person` . So I want to know, how to load classpath in `build.gradle` such that it recognizes `Person` as a class – Omnipresent Jan 05 '12 at 13:22
  • The way you do it right now this becomes a pure Groovy question. You'll have to configure GroovyShell so that 1. it finds Person.groovy 2. it automatically imports org.gradle.Person (or you add the import to your script). You cannot possibly add Person.groovy to Gradle's build script class path because it hasn't been compiled at the time the build script executes. I have a feeling that there is a fundamental problem with your approach (build.gradle uses script.groovy uses Person.groovy uses script.groovy?!). That's why I asked for the bigger picture. – Peter Niederwieser Jan 05 '12 at 13:31

1 Answers1

1

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?

Community
  • 1
  • 1
Andrey Adamovich
  • 20,285
  • 14
  • 94
  • 132