Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a task that runs before all other tasks in gradle

Tags:

gradle

init

task

I need to create an initialize task that will run before all other task when I execute it.

task A {
    println "Task A"
}

task initializer {
   println "initialized"
}

If I execute gradle -q A, the output will be:

>initialized

>Task A

Now if i'll add:

task B {
    println "Task B"
}

Execute gradle -q B, and I get:

>initialized

>Task B

So it doesn't matter which task I execute, it always get "initialized" first.

like image 448
Ori Wiesel Avatar asked Jul 19 '17 07:07

Ori Wiesel


People also ask

How do I run multiple tasks in Gradle?

Executing Multiple Tasks You can execute multiple tasks from a single build file. Gradle can handle the build file using gradle command. This command will compile each task in such an order that they are listed and execute each task along with the dependencies using different options.

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.

How do I skip a task in Gradle?

To skip any task from the Gradle build, we can use the -x or –exclude-task option. In this case, we'll use “-x test” to skip tests from the build. As a result, the test sources aren't compiled, and therefore, aren't executed.

How do I define a task type in Gradle?

We can define task types in the buildSrc folder which is located at the root project level. Gradle compiles everything that is inside and adds types to the classpath so our build script can use it. Our task type which we defined before ( PrintToolVersionTask) can be moved into the buildSrc/src/main/groovy/com/baeldung/PrintToolVersionTask.groovy.

What happens when--continue is used in Gradle?

If --continue is used, other tasks can continue running after it. Tasks that don’t respond to interrupts can’t be timed out. All of Gradle’s built-in tasks respond to timeouts in a timely manner. Example 23. Specifying task timeouts

How to create a simple Gradle task to print a welcome?

To create a straightforward Gradle task, we need to add its definition to our build.gradle file: task welcome { doLast { println 'Welcome in the Baeldung!' } } The main goal of the above task is just to print text “Welcome in the Baeldung!”. We can check if this task is available by running gradle tasks –all command:

Why doesn’t incremental build work in Gradle?

If they haven’t, Gradle can consider the task up to date and therefore skip executing its actions. Also note that incremental build won’t work unless a task has at least one task output, although tasks usually have at least one input as well.


3 Answers

You can make every Task who's name is NOT 'initializer' depend on the 'initializer' task. Eg:

task initializer {
   doLast { println "initializer" }
}

task task1() {
   doLast { println "task1" }
}

// make every other task depend on 'initializer'
// matching() and all() are "live" so any tasks declared after this line will also depend on 'initializer'
tasks.matching { it.name != 'initializer' }.all { Task task ->
    task.dependsOn initializer
}

task task2() {
   doLast { println "task2" }
}

Or you could add a BuildListener (or use one of the convenience methods eg: Gradle.buildStarted(...))

like image 80
lance-java Avatar answered Oct 20 '22 21:10

lance-java


Seems like you aim execution phase, and you want a task precursing each task or just run as a first task in the execution phase?

If you want a task to always execute in every project before each other task after its being evaluated you can add a closure to he main build.gradle:

allprojects {
  afterEvaluate {
    for(def task in it.tasks)
      if(task != rootProject.tasks.YourTask)
      task.dependsOn rootProject.tasks.YourTask
  }
}

or

tasks.matching {it != YourTask}.all {it.dependsOn YourTask}

You can also use the Task Execution Graph to define the lifecycle. There are few ways of achieving your goal, depending on your needs and a project structure.

like image 31
LazerBanana Avatar answered Oct 20 '22 21:10

LazerBanana


The previously suggested solution with dependsOn works fine, but I don't like about it that it changes and clutters the task dependencies. The first solution coming to my mind is using Gradle Initialization Scripts. They are really cool. But, the usage is a bit tedious: currently there is no way to have a default project-local Gradle init script. You have to either explicitly specify the script(s) on command line, or place them in USER_HOME/GRADLE_HOME.

So another solution (already briefliy mentioned by @lance-java) which can be used to run some initialization, like a init task/script, is "build listeners". Depending on how early/late the initialization code should run, I use one of these two:

gradle.afterProject {
    println '=== initialized in afterProject'
}

or

gradle.taskGraph.whenReady {
    println '=== initialized in taskGraph.whenReady'
}

Here the docs of the Gradle interface and of BuildListener.

Note that some of the events occur very early, and you probably can't use them because of that, like e.g. beforeProject and buildStarted (explanations here and there).

like image 31
t0r0X Avatar answered Oct 20 '22 21:10

t0r0X