Gradle 格拉德尔';未知属性';:在我的自定义插件中导入插件
我正在编写一个自定义插件,它有一个任务,可以进行HTTP-API调用 因此,在我的自定义插件的Gradle 格拉德尔';未知属性';:在我的自定义插件中导入插件,gradle,build.gradle,gradle-plugin,gradlew,Gradle,Build.gradle,Gradle Plugin,Gradlew,我正在编写一个自定义插件,它有一个任务,可以进行HTTP-API调用 因此,在我的自定义插件的build.gradle中,我包含了下面的plugins标签 plugins { id 'java-gradle-plugin' id 'groovy' id 'maven-publish' id 'io.github.http-builder-ng.http-plugin' version '0.1.1' } 我的自定义插件中的任务是 task makeRESTCal
build.gradle
中,我包含了下面的plugins
标签
plugins {
id 'java-gradle-plugin'
id 'groovy'
id 'maven-publish'
id 'io.github.http-builder-ng.http-plugin' version '0.1.1'
}
我的自定义插件中的任务是
task makeRESTCall() {
onlyIf {
!inputList.empty
}
doLast {
//println 'Successfully made REST Call'
//println inputList
def http = groovyx.net.http.HttpBuilder.configure {
request.uri = 'http://localhost:8080'
request.contentType = 'application/json'
request.uri.path = '/api/v1/validate'
}
http.post {
request.body = inputList.toString()
response.success {resp, json ->
println json
if (!json) {
throw new GradleException("Validation Failed")
}
}
}
}
}
我的自定义插件获取build属性,当我在另一个项目中包含该自定义插件时,当我执行任务makeRESTCall
时,我得到以下异常
任务“:api:makeRESTCall”的执行失败。
无法获取任务的未知属性“groovyx”:类型为org.gradle.api.DefaultTask的api:makeRESTCall
我在自定义插件中导入的
http插件
在我的项目中没有正确导入在自定义插件中,您正在使用库(groovyx.net.http.HttpBuilder
类),因此您需要在插件项目中配置对此库的依赖关系:
dependencies {
compile "io.github.http-builder-ng:http-builder-ng-core:1.0.3"
}
要进行快速测试,您可以在要应用插件的项目的buildSrc
目录中创建以下临时插件:
buildSrc/build.gradle
dependencies {
compile "io.github.http-builder-ng:http-builder-ng-core:1.0.3"
}
repositories {
mavenCentral()
}
buildSrc/src/main/groovy/com/mycompany/MyPlugin.groovy
package com.mycompany
import org.gradle.api.GradleException
import org.gradle.api.Plugin
import org.gradle.api.Project
class MyPlugin implements Plugin<Project> {
void apply(Project project) {
// ... your plugin login here, with 'inputList' definition
project.task ('makeRESTCall') {
onlyIf {
!inputList.empty
}
doLast {
//println 'Successfully made REST Call'
println inputList
def http = groovyx.net.http.HttpBuilder.configure{
request.uri = 'http://localhost:8080'
request.contentType = 'application/json'
request.uri.path = '/api/v1/validate'
}
http.post {
request.body = inputList.toString()
response.success {resp, json ->
println json
if (!json) {
throw new GradleException("Validation Failed")
}
}
}
}
}
}
注意:我认为您不需要应用plugin
id“io.github.httpbuilder ng.http plugin”version“0.1.1”
,除非您使用此插件公开的HTTPTask
,这只是一个围绕groovyx.net.http.HttpBuilder
的Gradle任务包装器,我包含了您在这里提到的依赖编译
内容。但它仍然没有被打包在我的插件中,仍然面临同样的错误。我的插件是一个独立的代码库,它从Nexus
分发。不在buildSrc
folderit中,它应该可以工作。请检查自定义插件生成的pom.xml
文件:它应该依赖于库groupId:org.gradle.sample.http,artifactId:http builder ng core,版本:1.0.3
。如果存在此依赖项,则它将自动添加为应用插件的项目中的可传递依赖项。请提供在我的插件中生成的插件pom.xml
Nopom.xml
的内容。我解压缩了插件jar文件,只看到三个文件夹。1.)我的groovy主类com.mycompany.CustomPlugin.class
2.)一个名为gradle
的文件夹,其中包含我的src/main/resources
文件夹下的所有支持gradle文件,3.)我使用gradlew clean build
命令生成插件jar文件,并生成一个jar文件
以及如何将这个插件jar包含到您的消费者项目中?
import com.mycompany.MyPlugin
apply plugin: MyPlugin