2016-06-28 128 views
2

我有两个gradle这个文件的项目:build.gradlemyPlugin.gradle如何将Gradle Plugin(及其依赖)加载到build.gradle中?

myPlugin.gradle实现的插件接口。该插件还对osdetector-gradle-plugin

我加了对方旁边两个gradle这个文件的依赖性,然后我试图申请为myplugin为build.gradle如下:

apply from: 'myPlugin.gradle' 

不过,我有以下错误myPlugin.gradle文件:

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

这里是myPlugin.gradle文件的代码:

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) 

build.gradle文件:我只是做这样的事情:

//定义buildScript库和依赖然后

apply from: 'myPlugin.gradle' 

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

我怎样才能解决这个插件ID为“com.google.osdetector”不发现了什么?

+0

听起来像是你的'myPlugin.gradle'找不到插件ID为' com.google.osdetector',也许你应该在'myPlugin.gradle'中添加一个buildscript部分(带有适当的仓库部分)? – RaGe

+0

我在myPlugin.gradle中添加了buildscript部分,但仍然有相同的错误?主build.gradle和myPlugin.gradle之间的依赖关系是否存在关系? –

回答

0

这是一个常见的错误,为build.gradle文件添加一个插件需要为构建脚本本身添加一个依赖项 - 不适用于项目。下面的代码(如果您申请的插件文件中添加)片就可以解决这个问题:

buildscript { 

    repositories { 
    mavenCentral() 
    mavenLocal() 
    } 

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

} 

编辑

请看看here - 看来,如果你需要apply from第三-party脚本你需要使用完整的类名(包)。因此,如下的文件应该被定义为:

的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) 
+0

你的意思是我应该在myPlugin.gradle或build.gradle中添加这段代码?我已经添加到myPlugin.gradle中,并且仍然在应用插件的文件中出现同样的错误 –

+0

@WaelShowair,就我看到的是myPlugin.gradle而言 - 请注意,依赖项的范围是“classpath”而不是'compile'。 – Opal

+0

我将paste代码复制到myPlugin.gradle中,但我仍然有同样的错误,有什么建议吗?顺便说一句:myPlugin工作正常,如果我将buildpath中的classpath添加到buildscript部分 - > dependencies –