Refactoring code using apply from '...' changes behavior compared to inline code in build.gradle


(Xavier Ducrohet) #1

Should I expect side effects when putting code into additional gradle files?

For instance I’m running against an issue where my root project’s build.gradle contained something like this:

FileCollection getFoo() { … }

This function used to be accessible from the sub projects. Now I’m moving this in misc.gradle and doing

apply from ‘misc.gradle’

in my root project’s build.gradle file, but the function is not available anymore in my sub projects.

I would expect this to have no impact. This is with Gradle 1.9.

thanks!


(Peter Niederwieser) #2

There are a few known limitations where it is necessary to adapt the code. To answer your particular question, in order to make a method declared in an applied build script available outside that script, you’ll have to refactor it into a closure property attached to the project (‘ext.foo = { … }’). The call sites remain unchanged.


(Xavier Ducrohet) #3

Are the limitations documented somewhere? I’ve been refactoring a lot of our build code into files and I want to make sure I don’t miss anything. thanks!


(Peter Niederwieser) #4

I’m not aware of any such docs. One other limitation I can think of is that ‘buildscript’ blocks need to stay in the applying file, rather than being moved into the applied file.


(Xavier Ducrohet) #5

ok, thanks.