Is it possible to write a plugin that applies third-party plugins?


(jbisotti) #1

We have a number of microservices which all use several third-party plugins – spring-boot-gradle-plugin, gradle-git-properties, nebula-project-plugin, etc. This means in each project’s build.gradle, there is a duplicated buildscript closure which specifies a few repositories and a few dependencies. I would like to write a custom plugin that would take care of setting up those buildscript closure repositories and dependencies; it would be the only plugin applied to each of our projects.

So far, I’ve come up with this…

class CommonBuildPlugin implements Plugin<Project> {

void apply(final Project project) {
    addPluginRepositories project
    addPluginDependencies project

private static void addPluginRepositories(final Project project) {
    final ScriptHandler buildscript = project.buildscript

    final RepositoryHandler repositories = buildscript.repositories

    repositories.maven(new Action<MavenArtifactRepository>() {
        public void execute(final MavenArtifactRepository mavenArtifactRepository) {


private static void addPluginDependencies(final Project project) {
    final ScriptHandler buildscript = project.buildscript

    final DependencyHandler dependencies = buildscript.dependencies

    final String gradleGitPluginVersion = '1.4.16'



However, when I replace the various third-party plugins, in one of our projects, with this one the build fails with:

> Failed to apply plugin [class 'CommonBuildPlugin']
   > Cannot change dependencies of configuration ':classpath' after it has been resolved.

Is what I’m trying to do possible?
If so, any help would be appreciated.



(Marcin Zajączkowski) #2

There is more straightforward approach to achieve that. Just add those plugins as your plugin’s buildscript dependencies (in its build.gradle). Then you would be able to just apply them in your plugin code (as there are already on a (plugin) classpath).