19

I am new to Gradle build tool and now I am reading the User Guide, but can't understand fully the difference between evaluation and execution phases.

In configuration phase project objects are configured and DAG is created, but we have afterEvaluate, so what is evaluate here? Evaluation of the tasks dependencies or what?

Whymarrh
  • 13,139
  • 14
  • 57
  • 108
Xelian
  • 16,680
  • 25
  • 99
  • 152

1 Answers1

30

As you have seen in documentation, there are three phases: Initialization, Configuration and Execution. Each step is traversed from root project down to subprojects for multi project builds. The afterEvaluate is useful in the root gradle file of a multi project build when you want to configure specific items based on the configuration made in subprojects.

Say you want to add a task for all subprojects that have a specific plugin defined. If you add to your root project:

subprojects {subProject ->
  if ( subProject.plugins.hasPlugin('myplugin')){
    subProject.task('newTask')<<{
      println "This is a new task"
    }
  }
}

This task will never be added since the root project is configured before the subprojects. Adding afterEvaluate will solve this for you:

subprojects {subProject ->
  afterEvaluate{
    if ( subProject.plugins.hasPlugin('myplugin')){
      subProject.task('newTask')<<{
        println "This is a new task"
      }
    }
  }
}
Jocce Nilsson
  • 1,658
  • 14
  • 28
  • 3
    So **afterEvaluate** can configure some items after the rest configuration has been done? Something like doLast, but not for alone tasks,**afterEvaluate** put its config after other configuration and before execution. So evaluation executes after a the end of configuration and before execution? Am I right? – Xelian Apr 19 '13 at 16:02
  • 15
    Yes you are right. Besides `project.afterEvaluate`, which runs after a particular project has been configured, there is also a `gradle.projectsEvaluated` hook that runs after all projects have been configured. – Peter Niederwieser Apr 20 '13 at 03:12
  • "this tasl will never be added since the root project is configured before the subprojects". I can't understand how that statement is relevant. – Fernando Dec 02 '21 at 18:36
  • It's all about timing. If you try to configure things before they exist, there will be problems. – Jocce Nilsson Dec 02 '21 at 18:40