Gradle Tip #3-task Order

Source: Internet
Author: User

Original link

I noticed that most of the problems I encountered when using gradle were related to the order in which the task was executed. Obviously if my build will work better if my task is executed at the right time. Let's take a closer look at how to change the order in which tasks are executed.

DependsOn

I think the most straightforward way to illustrate the way your task executes depends on other tasks is to use the DependsOn method.
For example, in the following scenario, task a already exists, and we want to add a task B, which must be executed after a executes:
A "title=" ">
This is a very simple scenario, assuming that A and B are defined as follows:

Task A << {println ' Hello from A '}task b << {println ' Hello from B '}

Just a simple call to B.dependson a, you can.
This means that as long as I execute task b,task a will execute first.

paveldudka$ Gradle B:ahello from A:bhello to B

Alternatively, you can declare its dependencies in the configuration area of the task:

Task A << {println ' Hello from a '}task B {dependsOn a dolast {println ' Hello from B '}}

What if we want to insert our task into a task dependency that already exists?

Process and just like that. Assume that the following task dependencies already exist:

Task A << {println ' Hello from A '}task b << {println ' Hello from b '}task C << {println ' Hello from C '}b. DependsOn Ac.dependson B

Join our new Task

Task B1 << {println ' Hello from B1 '}b1.dependson Bc.dependson B1

Output:

paveldudka$ Gradle C:ahello from A:bhello to B:b1hello from B1:chello from C

Note that DependsOn adds a task to the dependent collection, so it is no problem to rely on multiple tasks.

Task B1 << {println ' Hello from B1 '}b1.dependson Bb1.dependson Q

Output:

paveldudka$ Gradle B1:ahello from A:bhello to B:qhello from Q:b1hello from B1

Mustrunafter

Now suppose I have another task, which depends on the other two tasks. Here I use a real scene, I have two tasks, a unit test task, and a task for the UI test. There is also a task that runs all of the tests, and it relies on the previous two tasks.

Task Unit << {println ' Hello from unit tests '}task UI << {println ' Hello from UI tests '}task tests << {p Rintln ' Hello from all tests! '} Tests.dependson Unittests.dependson UI

Output:

paveldudka$ gradle Tests:uihello from UI Tests:unithello from unit Tests:testshello from all tests!

Although Unitest and UI test are executed before the test task, the order in which the two tasks of the unit and UI are executed is not guaranteed. Although it is now performed in alphabetical order, this is dependent on the implementation of Gradle, which is absolutely not dependent on the order of your code.
Because the UI test time is much longer than the unit test, I want the unit test to execute first. One solution is to make the UI task dependent on the unit task.
Unit "title=" ">

Task Unit << {println ' Hello from unit tests '}task UI << {println ' Hello from UI tests '}task tests << {p Rintln ' Hello from all tests! '} Tests.dependson Unittests.dependson Uiui.dependson unit//<--I added this dependency

Output:

paveldudka$ gradle Tests:unithello from unit Tests:uihello from UI Tests:testshello from all tests!

Unit test is now executed before the UI test.
But here's a nasty question, and my UI test doesn't really depend on unit test. I want to be able to execute the UI test separately, but every time I execute the UI test, I execute the unit test first.
We're going to use the mustrunafter here. Mustrunafter does not add a dependency, it simply tells the gradle to perform the priority if two tasks exist at the same time. For example, we can specify the Ui.mustrunafter unit here so that if both the UI task and the unit task are present, Gradle executes the unit test first, and if the Gradle UI is executed only, the unit task is not executed.

Task Unit << {println ' Hello from unit tests '}task UI << {println ' Hello from UI tests '}task tests << {p Rintln ' Hello from all tests! '} Tests.dependson Unittests.dependson uiui.mustrunafter Unit

Output:

paveldudka$ gradle Tests:unithello from unit Tests:uihello from UI Tests:testshello from all tests!

Dependency relationships such as:

Mustrunafter is still an experimental function in Gradle2.4.

Finalizedby

Now that we have two task,unit and UI, assuming that the two tasks will output the test report, now I want to combine the two test reports into one:

Task Unit << {println ' Hello from unit tests '}task UI << {println ' Hello from UI tests '}task tests << {p Rintln ' Hello from all tests! '} Task Mergereports << {println ' merging test reports '}tests.dependson Unittests.dependson uiui.mustrunafter Unitmergereports.dependson Tests

Now if I want to get the UI and unit test reports, execute task mergereports.

paveldudka$ gradle Mergereports:unithello from unit Tests:uihello from UI Tests:testshello from all Tests!:mergereportsme Rging Test Reports

This task is working, but it looks so stupid. Mergereports feeling is not particularly good from a user's point of view. I want to execute tests task to get the test report without knowing the existence of mergereports. Of course I can move the merge logic into the tests task, but I don't want to make the tests task too bloated, I continue to put the merge logic in the Mergereports task.  
Finalizeby to rescue. As the name implies, Finalizeby is the task to be executed after the task has finished executing. Modify our script as follows:

Task Unit << {println ' Hello from unit tests '}task UI << {println ' Hello from UI tests '}task tests << {p Rintln ' Hello from all tests! '} Task Mergereports << {println ' merging test reports '}tests.dependson Unittests.dependson uiui.mustrunafter Unitmergereports.dependson Teststests.finalizedby Mergereports

Now execute the tests task to get the test report:

paveldudka$ gradle Tests:unithello from unit Tests:uihello from UI Tests:testshello from all tests!:mergereportsmerging te St Reports

Note that Finalizedby is also an experimental function of Gradle2.4


Gradle Tip #3-task Order

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.