Overview
Sometimes in your build script you have to import some variables and methods from another build scripts.
Let’s assume, we have the following structure:
.
├── build.gradle
└── sub
├── file1.gradle
└── file2.gradle
the main build script (build.gradle file) will use files included in sub directory to import some variables and methods. To do this, we have to tell Gradle to have a look to these files. In the main script we have two tasks which list variables and methods available in other build scripts:
Then inside the sub/file1.gradle let’s define var1 variable and m1 closure in ext:
The sub/file2.gradle will look similar:
Gradle ext doesn’t allow to specify methods inside. However, thanks to Groovy language we can use closures instead of methods. To pass more than one parameter to the method -> in this case closure, we have to do this in this way:
and invoke it in this way: