Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load Gradle Plugin (with its depenecies) into build.gradle?

Tags:

I have a project that has two gradle files: build.gradle and myPlugin.gradle

The myPlugin.gradle implemented the Plugin Interface. The plugin also has a dependency on osdetector-gradle-plugin

I added the two gradle files beside each other then I tried to apply myPlugin into build.gradle as follows:

apply from: 'myPlugin.gradle'

However, I have got the following error in myPlugin.gradle file:

Plugin with id 'com.google.osdetector' not found

Here is the code for myPlugin.gradle file:

apply plugin: 'groovy'
apply plugin: 'maven'

  repositories {
     mavenCentral()
     mavenLocal()
  }
  dependencies {
      compile   'com.google.gradle:osdetector-gradle-plugin:1.4.0'
  }

import org.gradle.api.tasks.TaskAction
import org.gradle.api.DefaultTask
import org.gradle.api.Plugin
import org.gradle.api.Project

apply plugin: 'com.google.osdetector'
apply plugin: HostingMachineOSPlugin

class HostingMachineOSPlugin implements Plugin<Project>{
    void apply(Project project){
        project.plugins.apply("com.google.osdetector");
        //project.configurations.files('com.google.osdetector')
        println project.osdetector.os

        /* Extend the project property to have the class HostingMachineOS */
        project.ext.HostingMachineOS = HostingMachineOS
    }
}

public class HostingMachineOS {

    static family = "Unkown"

    static def setFamilyName(name){
        family = name
    }

    static def isLinux (){
        family == "linux"
    }

    static def isWindows (){
        family == "windows"
    }

    static def isMacOS(){
        family == "osx"
    }
}

HostingMachineOS.setFamilyName(osdetector.os)

in build.gradle file: I am just doing something like this:

//define buildScript repositories and dependencies then

apply from: 'myPlugin.gradle'

task dummy{
  println HostingMachineOS.isMacOS()
  println HostingMachineOS.isLinux()
  println HostingMachineOS.isWindows()
}

How can I solve the Plugin with id 'com.google.osdetector' not found?

like image 951
Wael Showair Avatar asked Jun 28 '16 16:06

Wael Showair


People also ask

How do you add Gradle dependency in build Gradle?

To add a dependency to your project, specify a dependency configuration such as implementation in the dependencies block of your module's build.gradle file.

How do I add plugins to build Gradle?

Put the source of the plugin directly into the build. gradle file. One big advantage of this approach is that the class is automatically compiled and added to the classpath of the build script without you configuring anything. The disadvantage is that the plugin cannot be used in another module.

How do I sync Gradle with dependencies?

Perform a Gradle sync/reload Android Studio: Run the “Sync Project with Gradle Files” action (via ctrl / cmd + shift + A ), or click the elephant + arrow icon in the toolbar.


1 Answers

This is a common pitfall, to add a plugin to build.gradle file you need to add a dependency for the build script itself - not for the project. The following piece of code (added in the file where you apply the plugin) should solve the problem:

buildscript {

  repositories {
     mavenCentral()
     mavenLocal()
  }

  dependencies {
     classpath 'com.google.gradle:osdetector-gradle-plugin:1.4.0'
  } 

}

EDIT

Please have a look here - it seems that if you need to apply from third-party script you need to use the full class name (with package). So the files should be defined as follows:

build.gradle

apply from: 'myPlugin.gradle'

task dummy{
  println HostingMachineOS.isMacOS()
  println HostingMachineOS.isLinux()
  println HostingMachineOS.isWindows()
}

myPlugin.gradle

buildscript {
  repositories {
    mavenCentral()
  }
  dependencies {
    classpath 'com.google.gradle:osdetector-gradle-plugin:1.4.0'
  }
}

apply plugin: 'groovy'
apply plugin: 'maven'
apply plugin: com.google.gradle.osdetector.OsDetectorPlugin
apply plugin: HostingMachineOSPlugin

class HostingMachineOSPlugin implements Plugin<Project>{
    void apply(Project project){
        project.plugins.apply(com.google.gradle.osdetector.OsDetectorPlugin);
        //project.configurations.files('com.google.osdetector')
        println project.osdetector.os

        /* Extend the project property to have the class HostingMachineOS */
        project.ext.HostingMachineOS = HostingMachineOS
    }
}

public class HostingMachineOS {

    static family = "Unkown"

    static def setFamilyName(name){
        family = name
    }

    static def isLinux (){
        family == "linux"
    }

    static def isWindows (){
        family == "windows"
    }

    static def isMacOS(){
        family == "osx"
    }
}

HostingMachineOS.setFamilyName(osdetector.os)
like image 182
Opal Avatar answered Sep 28 '22 02:09

Opal