Search code examples
gradlebuild.gradlegradle-plugingradlew

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


I am writing a custom Plugin that has a task which makes HTTP-API Calls.

Hence within my custom plugin's build.gradle, I have included the below plugins tag

plugins {
    id 'java-gradle-plugin'
    id 'groovy'
    id 'maven-publish'
    id 'io.github.http-builder-ng.http-plugin' version '0.1.1'
}

The task within my custom-plugin is this

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")
                }
            }
        }
    }
}

My custom-plugin gets built property and when i include the custom-plugin in another project and when I execute the task makeRESTCall, i get the below exception

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

the http-plugin that I import within my custom-plugin is not getting imported properly in my Project


Solution

  • In your custom plugin, you are using HTTP-Builder-NG library (groovyx.net.http.HttpBuilder class), so you need to configure a dependency to this library in your plugin project:

    dependencies {
        compile "io.github.http-builder-ng:http-builder-ng-core:1.0.3"
    }
    

    To make a quick test you could create the following temporary plugin in the buildSrc directory of the project you want to apply the plugin to:

    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
    

    Note : I don't think you need to apply plugin id "io.github.http-builder-ng.http-plugin" version "0.1.1", unless you are using the HTTPTask that this plugin exposes, which is just a Gradle Task wrapper around groovyx.net.http.HttpBuilder