Search code examples
gradlegretty

gradle: failure when plugin task `dependsOn` another plugin task


testing gradle as replacement for maven, we have a build.gradle file that contains the following plugins

buildscript {
    repositories {
        jcenter()
        mavenCentral()
    }
    dependencies {
        classpath "com.moowork.gradle:gradle-grunt-plugin:0.6"
        classpath 'org.akhikhl.gretty:gretty:+'
    }
}

apply plugin: 'scala'
apply plugin: "com.moowork.grunt"
apply plugin: 'war'
apply plugin: 'org.akhikhl.gretty'

./gradlew appStart and ./gradlew grunt_dev run fine from the console.

However when adding the line

appStart.dependsOn grunt_dev

to the script, ./gradlew appStart fails with

Could not find property 'appStart' on root project 'blah'.

Why is the appStart task visible from the gradle wrapper and not inside the script ?

Documentation on gretty appStart

UPDATE

Following @Opal explanation below, the following allowed hooking the tasks together

//Tasks defined in plugins are added after all projects are evaluated
//We have to hook after the evaluation to prevent an evaluation failure
project.afterEvaluate {
    project.tasks.appStart.dependsOn grunt_dev
}

Solution

  • When the following piece of code is added to build.gradle:

    project.tasks.each { println it.name }

    it can be seen that appStart is not in the list. Why? Probably the task isn't created at the moment of applying 'org.akhikhl.gretty' (build script evaluation) but later on, during runtime.

    Will try to check that in a moment.

    EDIT

    And here's the explanation. Tasks defined in gretty plugin are added after all projects are evaluated (read about gradle's lifecycle). This piece of code (at the very end of GrettyPlugin.groovy is responsible for such behavior:

    project.afterEvaluate {
      addRepositories(project)
      addDependencies(project)
      addTasks(project)
      afterAfterEvaluate(project)
    }