Extra properties

I have a external-build.gradle file containing

buildscript {
  ext{
    propa=''
  }
}

i have the project build.gradle containing

apply from: 'external-build.gradle'

buildscript {
  dependencies {
  classpath "group:artifact:$propa"
}

i) For some reason, propa is not getting resolved.
ii) When the same section is there in an external file and the project’s build.gradle, do they both get combined?

}

To answer your second question, blocks in different files simply execute the code on the specified object. This usually results in the same behavior as if the code was combined in the original file. However, the buildscript {...} block is special as it contributes to the classpath of the script before the remainder of it is executed.

You didn’t specify what exactly you’re trying to achieve, but if it is just to specify versions for plugins in an external file, this is possible. You would need to remove the buildscript {...} block from external-build.gradle and apply the file from inside of your build.gradle buildscript {...} block.

external-build.gradle

ext {
    propa = ''
}

build.gradle

buildscript {
    apply from: 'external-build.gradle'
    dependencies {
        classpath "group:artifact:$propa"
    }
}

I get what you’re saying. But the problem is then i need to have multiple external gradle files

  • one for buildscript versions etc
  • another for build related stuff like what plugins to use, customization, tasks etc.,

Interesting, I didn’t know about this feature!
Is it possible to re-use the information of the applied external file in tasks and project dependencies, too? I tried

task echo() {
    println "propa = $propa"
}

with your example and it actually did work but I don’t trust this thing yet… Then it would be possible to define, e.g. a file “versions.gradle” and apply this file in the buildscript section and I’d have a central point for any version property. Sounds to good to be true…

I don’t think you should have any problem with trusting this feature. I don’t know for sure that it has always worked this way, but I have seen it work since the early 1.x days.

The build scripts go through the two pass compilation/execution process, with the buildscript {...} part occurring first, and everything else occurring second with the addition of the appropriate classpath additions. It’s the same underlying Project, so applying the external file in the buildscript {...} block is just moving it up early enough to also be used in the buildscript {...} block as well.

The original comment about fixed references is true of the newer plugins {...} block, which is a little bit more special.

Thanks James, I will give it a try!