execution order of tasks and methods in gradle
I am bit confused about the execution order of tasks and methods in gradle file.
I have below gradle script
task Hello {
doFirst{
println "Hello First"
if(sayHello()) {
tasks['World'].execute()
}
}
doLast{
println "Hello Last"
}
}
task World {
doFirst{
println "World First"
}
doLast{
println "World Last"
}
}
def sayHello(){
println "Hello from sayHello"
return false
}
When I run gradle Hello
on command line it shows below output -
C:> gradle Hello
> Task :Hello
Hello First
Hello from sayHello
Hello Last
This was expected as sayHello() method returns false and it did not execute the World task.
I have confusion when sayHello() method returns true. Output -
C:> gradle Hello
> Task :World
World First
World Last
> Task :Hello
Hello First
Hello from sayHello
Hello Last
but I was expecting something like this -
C:> gradle Hello
> Task :Hello
Hello First
Hello from sayHello
> Task :World
World First
World Last
Hello Last
Another thing I read was we should not use execute on tasks.
What are other ways in which we can implement similar logic without using execute on task?
I read about dependsOn but can we add condition on dependsOn so that it would be executed when some flag is true/false?
I want to extract the logic checking part to method as it would be used at many places and I don't want to write the same logic in every task again and again.
Is it okay to call methods from gradle tasks or its not recommended? If not, what could be other ways to achieve the same ?
gradle
add a comment |
I am bit confused about the execution order of tasks and methods in gradle file.
I have below gradle script
task Hello {
doFirst{
println "Hello First"
if(sayHello()) {
tasks['World'].execute()
}
}
doLast{
println "Hello Last"
}
}
task World {
doFirst{
println "World First"
}
doLast{
println "World Last"
}
}
def sayHello(){
println "Hello from sayHello"
return false
}
When I run gradle Hello
on command line it shows below output -
C:> gradle Hello
> Task :Hello
Hello First
Hello from sayHello
Hello Last
This was expected as sayHello() method returns false and it did not execute the World task.
I have confusion when sayHello() method returns true. Output -
C:> gradle Hello
> Task :World
World First
World Last
> Task :Hello
Hello First
Hello from sayHello
Hello Last
but I was expecting something like this -
C:> gradle Hello
> Task :Hello
Hello First
Hello from sayHello
> Task :World
World First
World Last
Hello Last
Another thing I read was we should not use execute on tasks.
What are other ways in which we can implement similar logic without using execute on task?
I read about dependsOn but can we add condition on dependsOn so that it would be executed when some flag is true/false?
I want to extract the logic checking part to method as it would be used at many places and I don't want to write the same logic in every task again and again.
Is it okay to call methods from gradle tasks or its not recommended? If not, what could be other ways to achieve the same ?
gradle
can we add condition on dependsOn: yes. It's a method. You can call this method from inside an if block if you want to. what could be other ways to achieve the same ? the same as what? You posted an unrealistic example. If you ant the best solution for your actual problem, then describe what your actual problem is.
– JB Nizet
Nov 23 '18 at 9:07
@JBNizet I want to execute another task from one task based on some conditional logic. That logic I am trying to encapsulate in method. But I read we should not call execute on tasks like in my example. More appropriate approach I found was dependsOn.. but did not know we can provide condition for dependsOn.
– Kuldeep Singh
Nov 23 '18 at 9:16
add a comment |
I am bit confused about the execution order of tasks and methods in gradle file.
I have below gradle script
task Hello {
doFirst{
println "Hello First"
if(sayHello()) {
tasks['World'].execute()
}
}
doLast{
println "Hello Last"
}
}
task World {
doFirst{
println "World First"
}
doLast{
println "World Last"
}
}
def sayHello(){
println "Hello from sayHello"
return false
}
When I run gradle Hello
on command line it shows below output -
C:> gradle Hello
> Task :Hello
Hello First
Hello from sayHello
Hello Last
This was expected as sayHello() method returns false and it did not execute the World task.
I have confusion when sayHello() method returns true. Output -
C:> gradle Hello
> Task :World
World First
World Last
> Task :Hello
Hello First
Hello from sayHello
Hello Last
but I was expecting something like this -
C:> gradle Hello
> Task :Hello
Hello First
Hello from sayHello
> Task :World
World First
World Last
Hello Last
Another thing I read was we should not use execute on tasks.
What are other ways in which we can implement similar logic without using execute on task?
I read about dependsOn but can we add condition on dependsOn so that it would be executed when some flag is true/false?
I want to extract the logic checking part to method as it would be used at many places and I don't want to write the same logic in every task again and again.
Is it okay to call methods from gradle tasks or its not recommended? If not, what could be other ways to achieve the same ?
gradle
I am bit confused about the execution order of tasks and methods in gradle file.
I have below gradle script
task Hello {
doFirst{
println "Hello First"
if(sayHello()) {
tasks['World'].execute()
}
}
doLast{
println "Hello Last"
}
}
task World {
doFirst{
println "World First"
}
doLast{
println "World Last"
}
}
def sayHello(){
println "Hello from sayHello"
return false
}
When I run gradle Hello
on command line it shows below output -
C:> gradle Hello
> Task :Hello
Hello First
Hello from sayHello
Hello Last
This was expected as sayHello() method returns false and it did not execute the World task.
I have confusion when sayHello() method returns true. Output -
C:> gradle Hello
> Task :World
World First
World Last
> Task :Hello
Hello First
Hello from sayHello
Hello Last
but I was expecting something like this -
C:> gradle Hello
> Task :Hello
Hello First
Hello from sayHello
> Task :World
World First
World Last
Hello Last
Another thing I read was we should not use execute on tasks.
What are other ways in which we can implement similar logic without using execute on task?
I read about dependsOn but can we add condition on dependsOn so that it would be executed when some flag is true/false?
I want to extract the logic checking part to method as it would be used at many places and I don't want to write the same logic in every task again and again.
Is it okay to call methods from gradle tasks or its not recommended? If not, what could be other ways to achieve the same ?
gradle
gradle
asked Nov 23 '18 at 8:48
Kuldeep SinghKuldeep Singh
3944626
3944626
can we add condition on dependsOn: yes. It's a method. You can call this method from inside an if block if you want to. what could be other ways to achieve the same ? the same as what? You posted an unrealistic example. If you ant the best solution for your actual problem, then describe what your actual problem is.
– JB Nizet
Nov 23 '18 at 9:07
@JBNizet I want to execute another task from one task based on some conditional logic. That logic I am trying to encapsulate in method. But I read we should not call execute on tasks like in my example. More appropriate approach I found was dependsOn.. but did not know we can provide condition for dependsOn.
– Kuldeep Singh
Nov 23 '18 at 9:16
add a comment |
can we add condition on dependsOn: yes. It's a method. You can call this method from inside an if block if you want to. what could be other ways to achieve the same ? the same as what? You posted an unrealistic example. If you ant the best solution for your actual problem, then describe what your actual problem is.
– JB Nizet
Nov 23 '18 at 9:07
@JBNizet I want to execute another task from one task based on some conditional logic. That logic I am trying to encapsulate in method. But I read we should not call execute on tasks like in my example. More appropriate approach I found was dependsOn.. but did not know we can provide condition for dependsOn.
– Kuldeep Singh
Nov 23 '18 at 9:16
can we add condition on dependsOn: yes. It's a method. You can call this method from inside an if block if you want to. what could be other ways to achieve the same ? the same as what? You posted an unrealistic example. If you ant the best solution for your actual problem, then describe what your actual problem is.
– JB Nizet
Nov 23 '18 at 9:07
can we add condition on dependsOn: yes. It's a method. You can call this method from inside an if block if you want to. what could be other ways to achieve the same ? the same as what? You posted an unrealistic example. If you ant the best solution for your actual problem, then describe what your actual problem is.
– JB Nizet
Nov 23 '18 at 9:07
@JBNizet I want to execute another task from one task based on some conditional logic. That logic I am trying to encapsulate in method. But I read we should not call execute on tasks like in my example. More appropriate approach I found was dependsOn.. but did not know we can provide condition for dependsOn.
– Kuldeep Singh
Nov 23 '18 at 9:16
@JBNizet I want to execute another task from one task based on some conditional logic. That logic I am trying to encapsulate in method. But I read we should not call execute on tasks like in my example. More appropriate approach I found was dependsOn.. but did not know we can provide condition for dependsOn.
– Kuldeep Singh
Nov 23 '18 at 9:16
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53443300%2fexecution-order-of-tasks-and-methods-in-gradle%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53443300%2fexecution-order-of-tasks-and-methods-in-gradle%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
can we add condition on dependsOn: yes. It's a method. You can call this method from inside an if block if you want to. what could be other ways to achieve the same ? the same as what? You posted an unrealistic example. If you ant the best solution for your actual problem, then describe what your actual problem is.
– JB Nizet
Nov 23 '18 at 9:07
@JBNizet I want to execute another task from one task based on some conditional logic. That logic I am trying to encapsulate in method. But I read we should not call execute on tasks like in my example. More appropriate approach I found was dependsOn.. but did not know we can provide condition for dependsOn.
– Kuldeep Singh
Nov 23 '18 at 9:16