Reuse buildscript across projects

We have a number of services and instead of copying the buildscript to each of the build.gradle files, I want to be able to extract all the common code / logic and properties in buildscript to an external file and reuse it. I did go through this post - How do I include buildscript block from external gradle script? and looks like it’s not possible. Wanted to know if this issue is fixed / what is the best way to accomplish this?

I’ve read through the forums looking for answer to this very question. And found all the “not supported” answers. In my case, I’m managing about 40 separate projects, and was hoping to apply the org.sonarqube plugin with a single line…

apply from: ''

This would contain a buildscript block, with the necessary repository and dependency declared, all followed by apply plugin: 'org.sonarqube'. Alas, right now it appears the repo and dependency still have to be declared in the root buildscript block.

Im not sure if this is obvious or will help anyone but given the limitations of apply from: to load centralized gradle files due to the need to repeat the buildscript block in each repo…heres what I did. In case this might help someone.

I created a git repo for my gradle build templates. I have templates for certain kinds of builds like for example microservice.gradle, webapp.gradle things like this. These gradle files use a file to configure them.

I make this repo a git submodule of all my project repos. This makes my gradle template repo a sub directory of each project I want to build. In each project there is a with the properties for that specific project to configure the template. The build.gradle in each project is actually a symlink to gradle-templates/microservice.gradle or whichever template its going to use from the submodule.

This is not the most terrible thing and avoids any build.gradle files residing in my projects and forces me to share configurable templates among my many, projects.

Again maybe obvious or simply a bad idea but this works for me without needing to hack on gradle with apply from: syntax.