Example 39.1. A custom plugin - Examples Wrong

https://docs.gradle.org/current/userguide/custom_plugins.html

Shouldn’t it be:

import org.gradle.api.Project
import org.gradle.api.Plugin

class GreetingPlugin implements Plugin<Project> {
    void apply(Project project) {
        project.task('hello') << {
            println "Hello from the GreetingPlugin"
        }
    }
}

If I have the “apply” in there I get an error to the effect that GreetingPlugin is defined twice. Includes:

Example 39.1. A custom plugin
Example 39.2. A custom plugin extension
Example 39.3. A custom plugin with configuration closure

Also “39.5.4. Writing tests for your plugin” which reads:

class GreetingPluginTest {
    @Test
    public void greeterPluginAddsGreetingTaskToProject() {
        Project project = ProjectBuilder.builder().build()
        project.pluginManager.apply 'org.samples.greeting'

        assertTrue(project.tasks.hello instanceof GreetingTask)
    }
}

Last line should probably have a test for GreetingPlugin not GreetingTask.

The example assumes your are writing this in a build script so the plugin is defined and applied in the same place. Of course, if you were to use ‘buildSrc’ or a separate project you would do as you outlined above.

I’m not sure what you mean. The behavior of the plugin is that it creates a task named ‘hello’ of type GreetingTask. This test case simply validates that such a task is created when the plugin is applied.

OK, that makes sense.