Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Gradle's terms evaluation and execution

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?

like image 468
Xelian Avatar asked Apr 17 '13 21:04

Xelian


People also ask

What is project Evaluation in Gradle?

Typically we use “configuration” in the context of the configuration phase which is Gradle evaluating all your build scripts, plugins, etc just before beginning execution. We say that a project is evaluated when the configuration for that project has been evaluated.

How Gradle tasks are executed?

Gradle has different phases, when it comes to working with the tasks. First of all, there is a configuration phase, where the code, which is specified directly in a task's closure, is executed. The configuration block is executed for every available task and not only, for those tasks, which are later actually executed.

What is afterEvaluate in Gradle?

afterEvaluate , which runs after a particular project has been configured, there is also a gradle.

What does Gradle use to determine the order in which tasks can be run?

Gradle determines the subset of the tasks, created and configured during the configuration phase, to be executed. The subset is determined by the task name arguments passed to the gradle command and the current directory. Gradle then executes each of the selected tasks.


1 Answers

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"
      }
    }
  }
}
like image 58
Jocce Nilsson Avatar answered Oct 06 '22 01:10

Jocce Nilsson