Understanding apply from: <file>

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Understanding apply from: <file>

matthias
Hey there,

I am running into a few issues when using apply from: <file>, and I just don't understand why these things happen. What I want to do is make my build scripts a little cleaner by separating code out into files that I include using "apply from".

However, two things in particular struck me as not working anymore:

1) Within the included file, I cannot apply plugin: <plugin-id> anymore. I have to spell out the fully qualified class name of the plug-in for it to work. Example:

// build.gradle
apply from: 'included.gradle'

// included.gradle
buildscript {
  // define repose and build script dependencies
}
apply plugin: 'plugin-id' // FAILS with "no plugin with that ID"
apply plugin: com.example.Plugin // works

However, if I keep the buildscript setup in the included file, and move the apply call from included.gradle to build.gradle, then applying the plugin by ID works! Why is that?

2) I don't understand why methods defined in an included file are not visible in the file that includes it:
// included.gradle
def helper() {}

// build.gradle
apply from: 'included.gradle'
helper() // fails: no such method

Again, why is that? My understanding was that any "global" definition or call is delegated to the project instance. So wouldn't that mean that helper is both defined and called on the project object (well, apparently it's not, but just trying to figure out what's happening here).

Thanks for you input.
Reply | Threaded
Open this post in threaded view
|

Re: Understanding apply from: <file>

Ladislav Thon
2) I don't understand why methods defined in an included file are not
visible in the file that includes it:
// included.gradle
def helper() {}

// build.gradle
apply from: 'included.gradle'
helper() // fails: no such method

This looks like Groovy hassle to me. If you do

def a = 1

then it's local to the script. However, if you write

a = 1

then it's stored in the binding and is "global". For methods, I'm not sure, but writing it as a closure should work:

helper = { -> ... }

That said, I'm not sure if Gradle doesn't work differently (e.g., executing each applied script in its own context), but that would prevent another stuff that works now, so I guess not.

LT
Reply | Threaded
Open this post in threaded view
|

Re: Understanding apply from: <file>

matthias
the helpers-via-closures approach works great, thanks.

still, does anyone have an answer to 1) ? How exactly is a file processed that you include via apply?
Reply | Threaded
Open this post in threaded view
|

Re: Understanding apply from: <file>

phasmal
This post has NOT been accepted by the mailing list yet.
I still have this (#1) issue - I have a custom plugin that works fine from a normal build.gradle, but when run from a build that is included using 'apply from' then the plugin id is not found.  Any progress on this?
Reply | Threaded
Open this post in threaded view
|

Re: Understanding apply from: <file>

Peter Niederwieser
In reply to this post by matthias
This list is no longer active. Please use http://forums.gradle.org instead.