4

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
}
Bruno Grieder
  • 28,128
  • 8
  • 69
  • 101

1 Answers1

5

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)
}
Opal
  • 81,889
  • 28
  • 189
  • 210
  • Thanks for you help. That would be a good explanation since `gradle tasks` however, does return `appStart`. – Bruno Grieder Jan 22 '15 at 10:57
  • Yes, `gradle tasks` command returns `appStart`. But it's on the list while processing the `build.gradle` script. Add the code I provided at the end of script, then run just `gradle` and investigate the output. – Opal Jan 22 '15 at 10:59
  • OK. makes sense. I will investigate how I can make the `dependsOn` happen anyway; I guess by hooking to an `afterEvaluate` event if that exists – Bruno Grieder Jan 22 '15 at 11:06
  • Yes, that's the way to go. Find task in afterEvaluate and define dependency. – Opal Jan 22 '15 at 11:07