"method not found" after moving it to separate gradle file

After upgrading to 2.1, I don’t seem to be able to use a method defined in a separate gradle file using the “apply from” statement like:

apply plugin: 'java'
apply from: 'utils.groovy'
  version = "1.0.0${stage() == 'release' ? '' : '-SNAPSHOT'}"

and utils.groovy looks something like:

def stage() {
    ...
    ...
    stage
}

has this ability been removed in 2.1?

This was never possible. You’ll have to change the definition to ‘ext.stage = { … }’.

It looks like this stopped working with 2.0. I’m not sure, but I’m guessing this is related to the Groovy 2.x upgrade. You can work around it using a project extension closure:

ext.stage = {
    ...
    stage
}

I don’t think it ever worked.

Thanks for the quick reply guys!!

Peter - it works perfectly fine in gradle 1.12. i’ve no problem with changing the syntax but there is still something fishy with this. If I define a method

def stage() {
   do stuff
}

in the build.gradle file, I can still use it BUT if i move it to another file and import (apply from) I have to convert it to a closure.

Apparently, 1.12 is the only version ever where this worked (I tried 1.0 and everything from 1.9 up). I don’t think this was intentional (don’t recall this feature being announced, and our tests would have caught a regression). ‘ext.stage = { … }’ has always been the official solution (also taught in our training). In general, I agree that it would be nice to have this feature, and it would be interesting to know why it works in 1.12.

No problem. I appreciate the quick responses from you and gary. while thinking about this, i actually refactored the code into a simple plugin which ends up being much cleaner.

Thanks!!