Unable to apply a plugin from my plugin code

I have many projects that repeat the content of plugins{} block in my build.gradle script. My idea was to write my plugin and apply a set of repeating plugins there. So, in the apply method, I call

project.getPlugins().apply("first-plugin-id");
project.getPlugins().apply("second-plugin-id");

Unfortunately, while the build.gradle script works fine

plugins {
    id("first-plugin-id")
    id("second-plugin-id")
}

applying from my plugins ends up with an error message:

Plugin with id 'first-plugin-id' not found

All these plugins are installed locally and configured in settings.gradle in the section pluginManagement

Any idea whats wrong?

There are two points.

  1. never use project.getPlugins() (see its JavaDoc), either use methods on project.getPluginManager() or methods directly on project like project.apply(...).
  2. You need to add the plugin as dependency to the project that builds the plugin.

Thank you for your response, appreciate it a lot!

never use project.getPlugins() (

You are, of course, right. I have tried all the alternatives, and this one just left me in the editor when I formulated my forum question. Normally, I use project.getPluginManager.

You need to add the plugin as dependency to the project that builds the plugin.

Should I define the following in my plugin project?

 dependencies {
     implements ("first:plugin:artifact")
     implements ("second:plugin:artifact")
}

I have no idea how it could help in my target projects when using my plugin because I am referencing the other plugins via ID and not using the plugin class.

What worked for me was the following definition in build.gradle.kt in my target project

buildscript {
    dependencies {
             classpath ("my:plugin:artifact")
             classpath ("first:other_plugin:artifact")
             classpath ("second:other_plugin:artifact")
    }
}

However, this would not make my job easier - instead of defining the plugins in the plugins{} block via ID, I do the definition in the buildscript{} block. I have a lot of projects where the plugins{} block in build.gradle contains approx. ten 3rd party plugins (never published, distributed only as a ZIP file by the framework author, do not ask why?!), and it repeats in all my projects dealing with the framework. My idea was to apply all these plugins from my plugin without the need for additional definitions.

BTW - I discovered that in MyPlugin.apply(), no repositories reference exists and this snipped reports nothing

project.getRepositories().forEach(repository -> {
      System.out.println("REPOSITORY: " + repository.getName());
});

I tried to add repositories in MyPlugin.apply() before calling project.getPluginManager().apply("..."), did not help.

        RepositoryHandler rh = project.getRepositories();
        rh.mavenCentral();
        rh.gradlePluginPortal();

        rh.maven(repo -> {
             repo.setUrl(new URI("file:///C://Path/To/3rd/Party/Maven/Repo"));
             repo.setName("....");
        });
//

I am giving up,

Should I define the following in my plugin project?

The typo aside yes, that’s what I said.

I have no idea how it could help in my target projects when using my plugin because I am referencing the other plugins via ID and not using the plugin class.

That’s irrelevant. To apply a plugin you have to add it to the classpath, no matter how you apply it. If you apply it within a plugins block of a build script that also adds it to the classpath. Declaring it in the plugins block of a build script with apply false also adds it to the classpath just does not apply it. Using buildscript { dependencies { classpath... } } also adds it to the classpath. And just the same depending on it from your plugin adds it to the classpath whenever your plugin is used. Latest when you try to configure the plugin, you need the classes on your compile classpath anyway. I personally would actually apply by classname in this case to gain some compile-time safety.

What worked for me was the following definition in build.gradle.kt in my target project

Yes, because it works-around your bug of missing dependency by adding it to the classpath manually.

BTW - I discovered that in MyPlugin.apply() , no repositories reference exists and this snipped reports nothing

I have no idea how you think this information should be relevant in any way for this thread’s topic, or why you should add the Gradle Plugin Portal to your target project, unless you build a Gradle plugin of course.

OK, now I see:

If you apply it within a plugins block of a build script that also adds it to the classpath

That’s what I’ve been missing! I appreciate your support.

1 Like