在自定义gradle插件中获取classpath依赖项

时间:2015-12-01 11:12:41

标签: java gradle gradle-plugin

我必须在buildscript

中声明我的自定义插件的依赖项
buildscript {
    dependencies {
        classpath files (["project/path/to/compiled/classes"])

        classpath ("com.project.plugin:custom-plugin:${pluginVersion}")
    }
}

是否有可能以某种方式在插件类

中获取它们
public class PluginClass implements Plugin<Project> {

    @Override
    public void apply(Project project) {
        project.getExtensions().create("pluginConfig", PluginExtension.class);
        project.getTasks().create("plugin", PluginTask.class);
    }

}

或使用扩展程序传递它们?类似的东西:

porject.getDependencies().create(classpath_dependency)

1 个答案:

答案 0 :(得分:5)

您可以通过dependencies实例同时访问configurationsproject

buildscript {
  repositories {
    mavenCentral()
  }

  dependencies {
    classpath 'com.google.guava:guava:18.0'
  }
}

apply plugin: LolPlugin

class LolPlugin implements Plugin<Project> {
  public void apply(Project p) {
    p.buildscript.dependencies.each {
      println it
    }
    p.buildscript.configurations.classpath.each {
      println it
    }
  }
}