Home > Mobile >  Create custom plugin and using it in another buildscript
Create custom plugin and using it in another buildscript

Time:09-23

Trying to create a custom plugin to share the buildscript logic across projects. I have added the buildscript classpath dependencies but it is still saying that the plugin can't be found. I do not want to manually it in the project that is using the custom plugin because I may have to change the version number in the future. Is there any solution for this?

DependencyManagementPlugin.java

public class DependencyManagementPlugin implements Plugin<Project> {

  @Override
  public void apply(Project project) {

    DependencyHandler dependencies = project.getBuildscript().getDependencies();
    dependencies.add("classpath", "org.springframework.boot:spring-boot-gradle-plugin:2.4.10");
    dependencies.add(
        "classpath",
        "io.spring.dependency-management:io.spring.dependency-management.gradle.plugin:1.0.11.RELEASE");

    PluginContainer plugins = project.getPlugins();
    plugins.apply(MavenPublishPlugin.class);
    plugins.apply(JavaPlugin.class);
    plugins.apply(JacocoPlugin.class);
    plugins.apply("org.springframework.boot");
    plugins.apply("io.spring.dependency-management");
  }
}

build.gradle

plugins {
    id 'groovy-gradle-plugin'
    id 'maven-publish'
}

gradlePlugin {
    plugins {
        dependencyManagementPlugin {
            id = 'com.example.dependency-management'
            implementationClass = 'com.example.DependencyManagementPlugin'
        }
    }
}

using the plugin in another build.gradle

plugins {
    id 'com.example.dependency-management'
}

...

Error message:

An exception occurred applying plugin request [id: 'com.example.dependency-management']
> Failed to apply plugin 'com.example.dependency-management'.
   > Plugin with id 'org.springframework.boot' not found.

CodePudding user response:

The solution is to add the dependency in the dependencies block of the build.gradle of the custom gradle plugin project.

https://docs.gradle.org/current/samples/sample_convention_plugins.html

  • Related