Marty Neal
Marty Neal

Reputation: 9563

How do I run a task created by a plugin in gradle

I've written a gradle plugin that adds a custom task called generateTestDocs, which depends on the task groovydoc, which itself is created by the groovy plugin.

//MyPlugin.groovy
@Override
void apply(Project project) {
    project.apply(plugin: 'groovy')
    project.task(type: GenerateTestDocsTask, dependsOn: ':groovydoc', 'generateTestDocs')
}

project.tasks.groovydoc.doFirst {
    println "I should see this message but I don't"
}

I'm trying to test this plugin by running the task generateTestDocs

@Test
void testRunGenerateTestDocs() {
    Project project = ProjectBuilder.builder().build()
    project.apply(plugin: 'my.gradle.plugin')
    project.tasks.generateTestDocs.actions*.execute(project.tasks.generateTestDocs)
}

For the last line in my test, I'd like to instead just say project.task.generateTestDocs.execute() and have it run the task with all of its dependencies, but that doesn't seem to work. The documentation for writing gradle plugins only shows assertions like assertTrue(project.tasks.hello instanceof GreetingTask) which shows the task is added to the project, but doesn't show how to run that task.

Upvotes: 3

Views: 2330

Answers (2)

mickeyyang
mickeyyang

Reputation: 465

I faced the same question, I solved this by add

apply plugin: CustomPluginName

in current gradle.build :

apply plugin: 'groovy'
...
// in this case Plugin name is MyPlugin
apply plugin: MyPlugin 

Upvotes: 0

Peter Niederwieser
Peter Niederwieser

Reputation: 123960

ProjectBuilder is only meant for unit tests. To run a build as part of a test, you'll need to use the Gradle tooling API, or a third-party plugin such as nebula-test (which builds upon the tooling API).

Upvotes: 0

Related Questions