Gradle: add plugin dependency from another plugin

夙愿已清 提交于 2019-12-10 15:24:03

问题


I'm creating gradle custom plugin and one of my tasks needs to be sure that another plugin applied to same project. Because it will operate on top of it. I want for users of my plugin to avoid setting up an explicit dependency to another plugin - I want to do it inside my plugin.

So, I want to have this plugin (https://plugins.gradle.org/plugin/org.hidetake.ssh) applied. It's my dependency.

The way how I create plugin - I just create a class code on groovy, put it in buildSrc\src\main\groovy and apply groovy plugin in project. So my custom plugin is visible to gradle on build phase. It works, I have few other plugins done this way for same project, so it's fine for now.

I've looked through other topics and google for same question, but I can not make this work for me. This how I apply the code:

void apply(Project project) {

    project.buildscript {
      repositories {
        maven {
          url "https://plugins.gradle.org/m2/"
        }
      }
      dependencies {
        classpath "org.hidetake:gradle-ssh-plugin:1.1.3"
      }
    }

    project.apply plugin: "org.hidetake.ssh"
    ...

The error message I got: Plugin with id 'org.hidetake.ssh' not found. I tried to do it via gradle api also using project.repositories.mavenCentral() and project.dependencies.add and project.apply(plugin:'org.hidetake.ssh') then - doesn't work also - same error message. Tried to use long notation in project.dependencies.add("myConfig",[group:'org.hidetake', name:'gradle-ssh-plugin', version:'1.1.3']) - no result.

Appreciate if someone can guide to the correct syntax\way to make it work.


回答1:


Ok, finally I got it. To solve the issue you need to do the following:

  1. Place build.gradle in your buildSrc directory.
  2. Declare dependency for the plugin as runtime. Like this:

    repositories {
        jcenter()
    }
    dependencies {
        runtime 'org.hidetake:gradle-ssh-plugin:2.6.0'
    }
    
  3. Apply plugin explicitly in your own plugin definition. Like this:

    void apply(Project project) {
        project.pluginManager.apply('org.hidetake.ssh')
    ...
    


来源:https://stackoverflow.com/questions/39493502/gradle-add-plugin-dependency-from-another-plugin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!