如何将Gradle插件(及其代理)加载到build.gradle中? [英] How to load Gradle Plugin (with its depenecies) into build.gradle?
问题描述
我有一个包含两个gradle文件的项目:build.gradle
和myPlugin.gradle
I have a project that has two gradle files: build.gradle
and myPlugin.gradle
myPlugin.gradle
实现了插件接口.该插件还依赖于 osdetector-gradle-plugin
The myPlugin.gradle
implemented the Plugin Interface. The plugin also has a dependency on osdetector-gradle-plugin
我将两个gradle文件彼此并排添加,然后尝试将myPlugin应用于build.gradle
,如下所示:
I added the two gradle files beside each other then I tried to apply myPlugin into build.gradle
as follows:
apply from: 'myPlugin.gradle'
但是,我在myPlugin.gradle
文件中遇到以下错误:
However, I have got the following error in myPlugin.gradle
file:
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
文件中:我只是在做这样的事情:
in build.gradle
file: I am just doing something like this:
//然后定义buildScript存储库和依赖项
//define buildScript repositories and dependencies then
apply from: 'myPlugin.gradle'
task dummy{
println HostingMachineOS.isMacOS()
println HostingMachineOS.isLinux()
println HostingMachineOS.isWindows()
}
如何解决ID为'com.google.osdetector'的插件?
How can I solve the Plugin with id 'com.google.osdetector' not found?
推荐答案
这是一个常见的陷阱,要将插件添加到build.gradle
文件,您需要为构建脚本本身而非项目添加依赖项.以下代码段(添加到应用插件的文件中)将解决此问题:
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'
}
}
编辑
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)
这篇关于如何将Gradle插件(及其代理)加载到build.gradle中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!