Vas Vas - 1 month ago 7
Groovy Question

Keep defs in a separate file

I have several defs in my main build.gradle file. Is it possible to move them into a separate file, and then just reference them?
Something like this (don't mind the syntax):

foobar.gradle

def foo = "foo"
def bar = "bar"
def baz = "baz"


main.gradle

println $foo
println $bar
println $baz

Vas Vas
Answer

Finally figured it out.

In foobar.gradle, set up the defs like this:

ext.foo = "foo"
ext.bar = "bar"
ext.baz = "baz"

Then in main.gradle, make sure to include this line:

apply from: 'foobar.gradle'

Now variables can be used as freely as if they are part of the main.gradle file.

Comments