DefaultTask example

In all my previous post under Gradle, I have executed a task by entering the below command in the command line.

gradle taskName

where taskName is the name of the task to be executed

If we just type gradle, the output will be as shown below

Output

E:\Projects\GradleConcepts\package3>gradle
Starting a Gradle Daemon, 1 incompatible Daemon could not be reused, use –status for details

Task :help

Welcome to Gradle 4.3.

To run a build, run gradle …

To see a list of available tasks, run gradle tasks

To see a list of command-line options, run gradle –help

To see more detail about a task, run gradle help –task

BUILD SUCCESSFUL in 14s
1 actionable task: 1 executed

We can set certain task as default task. If set, entering gradle command (as shown below) in the command line, the default tasks are executed instead of above output

Will use the below gradle script for our example

build.gradle


1  defaultTasks 'task1', 'task2'
2  
3  task task1 {
4   doFirst {
5       println 'I am task1'
6   }
7  }
8  
9  task task2 {
10  doFirst {
11      println 'I am task2'
12  }
13 }
14 
15 task task3 {
16  doFirst {
17      println 'I am task3'
18  }
19 }
20 
21 task task4 {
22  doFirst {
23      println 'I am task4'
24  }
25 }

In the above script, at line 1, we set task1 and task2 as default tasks.

Now when we enter gradle command in the command line, the output will be as shown below

Output

E:\Projects\GradleConcepts\package4>gradle

Task :task1
I am task1

Task :task2
I am task2

BUILD SUCCESSFUL in 1s
2 actionable tasks: 2 executed

Leave a Reply