Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle: add plugin dependency from another plugin

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.

like image 596
mrzodiak Avatar asked Sep 14 '16 14:09

mrzodiak


People also ask

How do I add plugins to Gradle?

To add a Gradle plugin with dependencies, you can use code similar to the following: Plugin DSL GA versions. Plugin DSL non GA versions. Legacy Plugin Application.

What is Buildscript block in Gradle?

buildscript: This block is used to configure the repositories and dependencies for Gradle. Note: Don't include dependencies here. ( those will be included in the module-level build.gradle) dependencies: This block in buildscript is used to configure dependencies that the Gradle needs to build during the project. Java.

What is subprojects in Gradle?

The subproject producer defines a task named buildInfo that generates a properties file containing build information e.g. the project version. You can then map the task provider to its output file and Gradle will automatically establish a task dependency.

What is apply false in Gradle?

The default behavior of the plugins {} block is to immediately resolve and apply the plugins. But, you can use the apply false syntax to tell Gradle not to apply the plugin to the current project and then use the plugins {} block without the version in subprojects' build scripts: Example 3.


1 Answers

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')
    ...
    
like image 148
mrzodiak Avatar answered Nov 15 '22 09:11

mrzodiak