How to create a method in a file used in "apply from: 'myfile'"

(Jason Zwolak) #1

I have

apply from: "my_file.gradle"
println myvar
// my_file.gradle
def hello() {
  println "this fails"
  project.ext {
  myvar = "this works"

and I get the error:

Could not find method hello() for arguments [] on root project ‘methods’.

How can I define a method in an included file?

(Peter Niederwieser) #2

Methods declared in an applied script aren’t visible outside that script. However, you can instead use an extra property:

ext.hello = { println "this works" }
apply from: "some/script.gradle"
hello() // shorthand for ''

(Jason Zwolak) #3

Fantastic. Now I understand why the error message said consider using a closure.