Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle executes all tasks?

Tags:

gradle

I have a very simple build script like so

task hello{     println("hello World") }  task bye {     println("bye") } 

On the command line I run gradle hello and I get the following output:

hello World bye :hello UP-TO-DATE 

Why is it executing the task "bye" (I'm assuming it gets executed since "bye" gets printed)? Thanks.

like image 384
user3240644 Avatar asked May 06 '14 02:05

user3240644


People also ask

Does Gradle build run all 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.

Which are the tasks are performed by Gradle?

The work that Gradle can do on a project is defined by one or more tasks. A task represents some atomic piece of work which a build performs. This might be compiling some classes, creating a JAR, generating Javadoc, or publishing some archives to a repository.

What is Gradle execution?

In Gradle terms this means that you can define tasks and dependencies between tasks. Gradle guarantees that these tasks are executed in the order of their dependencies, and that each task is executed only once. These tasks form a Directed Acyclic Graph.


2 Answers

It's a common pitfall:

task hello {     println("Any code in here is about *configuring* the\     task. By default, all tasks always get configured.")     doLast {         println("Any code in here is about *executing* the task.\         This code only gets run if and when Gradle decides to execute the task.")     } } 

The distinction between configuration phase and execution phase is probably the single most important concept to understand in Gradle. It can be confusing at first, and may go away in the future. A kind of analogue in the Ant/Maven world is that these tools first parse XML build scripts and build an object model (perhaps resolving some properties along the way), and only then execute the build.

like image 195
Peter Niederwieser Avatar answered Sep 20 '22 18:09

Peter Niederwieser


Adding to Peter answer, If you want to execute all task , you can specify the defaultTasks list.

defaultTasks 'clean', 'run'  task clean {     doLast {         println 'Default Cleaning!'     } }  task run {     doLast {         println 'Default Running!'     } }  task other {     doLast {         println "I'm not a default task!"     } } 

Output

Output of gradle -q > gradle -q Default Cleaning! Default Running! 

More details can be found here https://docs.gradle.org/current/userguide/tutorial_using_tasks.html

like image 20
Jafar Karuthedath Avatar answered Sep 19 '22 18:09

Jafar Karuthedath