Make a custom task/plugin out of a build.gradle

Hi All,

I’m a newb in gradle, so the question maybe very silly… I created a build.gradle which is going to be reused in many other projects. I feel it’s bit stupid to deploy that build.gradle in every (sub)project. The file structure will be the same, all subtasks will be the same, so it’s a lot of similarities. There maybe tiny configuration changes (either to run one of tasks in that build.gradle) across projects.

What is the best way to proceed from here to make it more supportable, to avoid copying the build.gradle into each project? I think about custom plugin/task maybe? Not sure, what the proper way is.

You should move the logic into a plugin. The API is exactly the same as in your build script, so you can just copy it. Then you deploy your plugin to either the Plugin Portal or your company’s repository and apply it from the other projects.

Thank you for the prompt reply. I started moving into this direction but have another conceptual question…

My initial gradle.build had a lot of utility tasks to copy and prepare files. Do I need to declare all of them with project.task('xxx', type:...)?

I can’t really give you an answer to that. What are you trying to do? If these tasks make sense in all of your projects, then yes.

Also just to be clear: You mean different projects in different repsitories, right? If you just mean subprojects in the same build. then all you need is to wrap your logic in:

subprojects {
  //logic applied to all subprojects
}

Thank you, Stefan,

So i have my build logic implemented with a help of several utility tasks to copy files, modify their content, zip them, etc, but then I have 2 high level tasks: deploy and register, which depend on the utility tasks chain.

As for your second question: right now they are subprojects, but the plugin will be used in different projects later

Thank you