O.O.
O.O.

Reputation: 2013

Run Task in Gradle Plugin after check

I've written a Gradle Plugin in Groovy under buildSrc as:

package test

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

class SamplePlugin implements Plugin<Project> {
    @Override
    void apply(Project project) {
        println "This line prints" //Just for Configuration. This prints
        def sample = project.tasks.create("sample") {
            doLast {
                println "This line does not print" 
            }
        }
        project.configure(project) {
            sample.mustRunAfter('check')
        }
    }
}

Here, I'm trying to run the sample task at the end of my build, so I have it run after check

I now try to call it in my projects build.gradle file that looks like:

buildscript {
    repositories {
        mavenLocal()
        mavenCentral()
    }
}

apply plugin: 'java'
apply plugin:'application'
apply plugin: test.SamplePlugin

repositories {
    mavenLocal()
    mavenCentral()
}

mainClassName = "test.Widget"

Unfortunately, I don't see that it runs i.e. the code in the doLast does not appear in the output, but the configuration code does:

:buildSrc:compileJava NO-SOURCE
:buildSrc:compileGroovy
:buildSrc:processResources UP-TO-DATE
:buildSrc:classes
:buildSrc:jar
:buildSrc:assemble
:buildSrc:compileTestJava NO-SOURCE
:buildSrc:compileTestGroovy NO-SOURCE
:buildSrc:processTestResources NO-SOURCE
:buildSrc:testClasses UP-TO-DATE
:buildSrc:test NO-SOURCE
:buildSrc:check UP-TO-DATE
:buildSrc:build
This line prints
:compileJava UP-TO-DATE
:processResources NO-SOURCE
:classes UP-TO-DATE
:jar UP-TO-DATE
:startScripts UP-TO-DATE
:distTar UP-TO-DATE
:distZip UP-TO-DATE
:assemble UP-TO-DATE
:compileTestJava NO-SOURCE
:processTestResources NO-SOURCE
:testClasses UP-TO-DATE
:test NO-SOURCE
:check UP-TO-DATE
:build UP-TO-DATE

BUILD SUCCESSFUL in 1s
5 actionable tasks: 5 up-to-date

I'd be grateful for any help or pointers

Edit: As M.Ricciuti commented below order matters, so I have moved the test.SamplePlugin after the plugin java. Otherwise, please follow lu.koerfers solution of using the pluginManager.

Upvotes: 2

Views: 6056

Answers (2)

M.Ricciuti
M.Ricciuti

Reputation: 12126

In your plugin you are creating a new task 'sample' and set a constraint "sample must run after check": but this does not include the sample task in the task graph . It just says: "if sample and check tasks are both executed , then check task must be executed first". So if you just execute 'gradle build', this will not trigger execution of task "sample".

Try to execute directly "gradle sample" : you will see it will trigger its execution, and make the execution of "check" task first in respect of the contraint you have defined in plugin.

If you want to make "sample" task execute each time you execute "build" task, then just set a "dependsOn" constraint between "build" and "sample" tasks, in your plugin:

class SamplePlugin implements Plugin<Project> {
    @Override
    void apply(Project project) {
        println "This line prints" //Just for Configuration. This prints
        def sample = project.tasks.create("sample") {
            doLast {
                println "This line does not print"
            }
        }
        project.configure(project) {
            sample.mustRunAfter('check')
            project.getTasks().findByName('build').dependsOn(sample) // <== set this contraint

        }
    }
}

EDIT : to avoid having to rely on plugin apply order, the task dependency declaration could be wrapped in a "afterEvaluate" block:

void apply(Project project) {
        // task 'sample' def ...
        // ... 

        project.configure(project) {
            project.afterEvaluate {
                sample.mustRunAfter('check')
                project.getTasks().findByName('build').dependsOn(sample)
            }
        }

Upvotes: 3

Lukas K&#246;rfer
Lukas K&#246;rfer

Reputation: 14543

The methods mustRunAfter and shouldRunAfter only define execution order, not causality. That means that they won't cause a task to be executed. But if both tasks are executed, the specified order will be taken into account.

To specify a task dependency, use dependsOn or finalizedBy:

project.pluginManager.withPlugin('java') {
    project.tasks.getByName('check').finalizedBy('sample');
}

This would cause sample to run everytime check runs and it ensures that it runs after check.

Upvotes: 3

Related Questions