Gradle 'Unknown Property' : 在我的自定义插件中导入插件

Gradle 'Unknown Property' : Importing Plugin within my Custom Plugin

我正在编写一个自定义插件,该插件的任务是进行 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,出现以下异常

Execution failed for task ':api:makeRESTCall'. Could not get unknown property 'groovyx' for task ':api:makeRESTCall' of type org.gradle.api.DefaultTask.

我在我的自定义插件中导入的 http-plugin 没有在我的项目中正确导入

在您的自定义插件中,您正在使用 HTTP-Builder-NG 库 (groovyx.net.http.HttpBuilder class),因此您需要在您的插件项目中配置对这个库的依赖:

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 围绕 groovyx.net.http.HttpBuilder

的任务包装器