Apply a gradle plugin (errorprone) from a custom Gradle Java plugin

need to apply a gradle plugin, in this case errorprone from a custom Gradle plugin.

My Plugin has a build.gradle that looks like this:

gradlePlugin {
    plugins {
        myErrorprone {
            id = 'my-errorprone'
            implementationClass = 'com.my.MyErrorpronePlugin'
        }
    }
}

And the plugin code is:

public class MyErrorpronePlugin implements Plugin<Project> {
  List<String> compilerArgs =
  Arrays.asList(
      "-XepExcludedPaths:.*/proto/.*|.*/protoGeneratedSrc/.*",
      "-XepDisableWarningsInGeneratedCode");

  @Override
  public void apply(Project project) {
project.getPluginManager().apply("net.ltgt.errorprone:");
for (JavaCompile task : project.getTasks().withType(JavaCompile.class)) {
  task.getOptions().setCompilerArgs(compilerArgs);
}
  }
}

Then, when in another project I apply this plugin (after getting the dependencies in the buildscript) like this:

apply plugin: 'my-errorprone'

A problem occurred evaluating root project 'my-project.
Failed to apply plugin [id 'my-errorprone'] Plugin with id 'net.ltgt.errorprone' not found.

And it only resolved if i add to buildscript classpath

classpath "net.ltgt.gradle:gradle-errorprone-plugin:0.0.16"

How can I make my plugin work in such way that the project that consumes my plugin will not have to add this direct dependency in the classpath in “net.ltgt.gradle:gradle-errorprone-plugin:0.0.16” ?

I suggest that you apply the plugin by class rather than by ID. This will force you to include the error prone dependency in your plugin’s dependency list as it won’t compile until you add the dependency

Eg:

import net.ltgt.gradle.errorprone.ErrorPronePlugin.class;

public class MyPlugin implements Plugin<Project> {
   public void apply(Project project) {
       project.getPluginManager().apply(ErrorPronePlugin.class);
       // custom logic here 
   } 
} 

Hey Lance, Thanks a lot!!
I understand the idea behind it,and i think it will work, but i am not sure what I should add to my custom plugin build.gradle file.
I tried few things non of them seemed to work.

Something like

repositories {
   maven {
      url "https://plugins.gradle.org/m2/"
   }
} 
dependencies {
   api "net.ltgt.gradle:gradle-errorprone-plugin:1.1.1"
}