Gradle: add plugin dependency from another plugin

2019-06-27 04:12发布

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条回答
乱世女痞
2楼-- · 2019-06-27 05:08

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')
    ...
    
查看更多
登录 后发表回答