Reuse buildscript across projects

(sm) #1

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?

(1cd7b3 6d0575) #2

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.

(rendion) #3

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.