Trying to Write a Plugin in Gradle, I can't figure What I Need to Implement Plugin

jhell :

So this is my current build.gradle file:

/*
 * This file was generated by the Gradle 'init' task.
 *
 * This generated file contains a sample Java Library project to get you started.
 * For more details take a look at the Java Libraries chapter in the Gradle
 * User Manual available at https://docs.gradle.org/5.6.1/userguide/java_library_plugin.html
 */

apply plugin: 'groovy'
apply plugin: 'java-gradle-plugin'

repositories {
    google()
}

dependencies {
     compile gradleApi()
}

From everything I've read in the gradle documentation, I believe this is all I should need to actually create a plugin. By that I mean being able to import Plugin from the org.gradle package.

However, org.gradle cannot be found on my build path.

What else do I need to do?

Francisco Mateo :

Judging from the comment in the Gradle build you provided, I am assuming you have installed Gradle on your local machine and you used gradle init to generate what you have now. Except you generated a java-library build type when you should have generated Gradle plugin build type.

Since you have the groovy plugin applied, I assume you want a Groovy based Gradle plugin, so simply run gradle init again, but select groovy-gradle-plugin plugin type. For example, using Gradle 6.2.2 on my local machine, my terminal output for generating a Gradle plugin project using Groovy shows:

➜  gradle init

Select type of project to generate:
  1: basic
  2: application
  3: library
  4: Gradle plugin
Enter selection (default: basic) [1..4] 4

Select implementation language:
  1: Groovy
  2: Java
  3: Kotlin
Enter selection (default: Java) [1..3] 1

Select build script DSL:
  1: Groovy
  2: Kotlin
Enter selection (default: Groovy) [1..2] 1

Project name (default: example-groovy): 

Source package (default: example.groovy): 


> Task :init
Get more help with your project: https://guides.gradle.org?q=Plugin%20Development

BUILD SUCCESSFUL in 17s
2 actionable tasks: 2 executed

So now I have the following directory structure and files:

├── build.gradle
├── gradle
│   └── wrapper
│       ├── gradle-wrapper.jar
│       └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── settings.gradle
└── src
    ├── functionalTest
    │   └── groovy
    │       └── example
    │           └── groovy
    │               └── ExampleGroovyPluginFunctionalTest.groovy
    ├── main
    │   ├── groovy
    │   │   └── example
    │   │       └── groovy
    │   │           └── ExampleGroovyPlugin.groovy
    │   └── resources
    └── test
        ├── groovy
        │   └── example
        │       └── groovy
        │           └── ExampleGroovyPluginTest.groovy
        └── resources

And the generated Gradle build file contents:

/*
 * This file was generated by the Gradle 'init' task.
 *
 * This generated file contains a sample Gradle plugin project to get you started.
 * For more details take a look at the Writing Custom Plugins chapter in the Gradle
 * User Manual available at https://docs.gradle.org/6.2.2/userguide/custom_plugins.html
 */

plugins {
    // Apply the Java Gradle plugin development plugin to add support for developing Gradle plugins
    id 'java-gradle-plugin'

    // Apply the Groovy plugin to add support for Groovy
    id 'groovy'
}

repositories {
    // Use jcenter for resolving dependencies.
    // You can declare any Maven/Ivy/file repository here.
    jcenter()
}

dependencies {
    // Use the awesome Spock testing and specification framework
    testImplementation 'org.spockframework:spock-core:1.3-groovy-2.5'
}

gradlePlugin {
    // Define the plugin
    plugins {
        greeting {
            id = 'example.groovy.greeting'
            implementationClass = 'example.groovy.ExampleGroovyPlugin'
        }
    }
}

// Add a source set for the functional test suite
sourceSets {
    functionalTest {
    }
}

gradlePlugin.testSourceSets(sourceSets.functionalTest)
configurations.functionalTestImplementation.extendsFrom(configurations.testImplementation)

// Add a task to run the functional tests
task functionalTest(type: Test) {
    testClassesDirs = sourceSets.functionalTest.output.classesDirs
    classpath = sourceSets.functionalTest.runtimeClasspath
}

check {
    // Run the functional tests as part of `check`
    dependsOn(tasks.functionalTest)
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=344802&siteId=1