Gradle“未知属性”:在我的自定义插件中导入插件

时间:2018-10-10 12:28:19

标签: 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 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")
                }
            }
        }
    }
}

我的自定义插件获得了构建属性,当我将自定义插件包含在另一个项目中并且执行任务makeRESTCall时,出现以下异常

  

任务':api:makeRESTCall'的执行失败。    无法获得类型为org.gradle.api.DefaultTask的任务':api:makeRESTCall'的未知属性'groovyx'

我在自定义插件中导入的http-plugin无法正确导入到我的项目中

1 个答案:

答案 0 :(得分:1)

在您的自定义插件中,您正在使用HTTP-Builder-NG库(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")
                        }
                    }
                }
            }
        }
    }

build.gradle

import com.mycompany.MyPlugin
apply plugin: MyPlugin

注意:我认为您无需应用插件id "io.github.http-builder-ng.http-plugin" version "0.1.1",除非您使用的是该插件公开的HTTPTask,这只是Gradle Task包装器在groovyx.net.http.HttpBuilder

附近