Discussion:
Understanding apply from: <file>
matthias
2011-08-10 07:57:31 UTC
Permalink
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.

--
View this message in context: http://gradle.1045684.n5.nabble.com/Understanding-apply-from-file-tp4684988p4684988.html
Sent from the gradle-user mailing list archive at Nabble.com.
Ladislav Thon
2011-08-10 08:53:23 UTC
Permalink
Post by matthias
2) I don't understand why methods defined in an included file are not
// 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
matthias
2011-08-16 08:15:49 UTC
Permalink
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?

--
View this message in context: http://gradle.1045684.n5.nabble.com/Understanding-apply-from-file-tp4684988p4703726.html
Sent from the gradle-user mailing list archive at Nabble.com.
Peter Niederwieser
2013-08-05 22:27:22 UTC
Permalink
This list is no longer active. Please use http://forums.gradle.org instead.



--
View this message in context: http://gradle.1045684.n5.nabble.com/Understanding-apply-from-file-tp4684988p5711603.html
Sent from the gradle-user mailing list archive at Nabble.com.

Loading...