Gradle not executing tasks in required order


(prachi sharma) #1

Hello,
I am a newbee to Gradle and finding it very difficult to execute the tasks in the order that I want.
The main purpose of my project is to build an ear according to user’s input… The code goes somewhat like this…

…//some text
task tgtEARPackaging << {
new File(BuildLogPath).mkdirs()
}

task BuildInitiate { //default task
def console = System.console()
if (console) {
ear = console.readLine('Do you want to run EAR packaging ? ')
if(ear == ‘y’) {
tgtEARPackaging.execute()
}
}
}
task MakeDir(dependsOn: tgtEARPackaging) {
mustRunAfter tgtEARPackaging
new File(templocation).mkdirs()
//creation of other required folders
}
//other tasks dependent on MakeDir

The logic says that execution of MakeDir should be dependent on the execution of tgtEarPackaging and tgtEarPackaging should execute only if user’s input is ‘y’. But the problem is that both tasks as well as there other dependents are executing even if the answer is ‘n’ which is opposite to the logic.
I have tried everything from execute(), dependsOn to mustRunAfter but nothing seems useful.

I would be very thankful if someone helps me with this as I have to submit my project in few days.
Thank you


(Csaba Sulyok) #2

Hi @prachi,

Gradle builds up the tasks he will execute based on your command-line arguments, so if “tgtEARPackaging” is there, it will execute it regardless of what you read from console. Generally, calling “.execute()” is disliked. You should let Gradle build up his own list of tasks he will run, and let him run it.

Suggestions:

  1. Use a system property (gradle MakeDir -PrunEarPackaging) and let a task run only if this property is set (in the ear packaging task, add onlyIf project.hasProperty(‘runEarPackaging’).
  2. If you really must have reading from console, you can set a property within the script. Something like this:
    project.ext.runEarPackaging = (console.readLine('Do you want to run EAR packaging ? ') == ‘y’). Then you can once again use onlyIf on the desired task (onlyIf project.ext.runEarPackaging). Just make sure to do the reading outside of any actual tasks, because which tasks are run are already decided at that point.

Good luck,
Csaba


(Stefan Oehme) #3

The most important question is: Why do you want to ask the user a question? This just seems overly complicated. What’s the use case? Why not just have an ear task that the user can call if he needs the ear.


(prachi sharma) #4

Thanku @Csaba_Sulyok,

The use of Systemproperty helped. But one more problem now is that even if “tgtEarPackaging” is skipped, its dependents, like MakeDir and others are still executing.
Can we use ‘onlyIf’ to impose task dependencies? So that if tgtEarPackaging is not executing, then its dependents(for e.g. MakeDir) is also skipped.?


(prachi sharma) #5

Hello @st_oehme,

Actually the project requires three important tasks i.e. ear creation, database creation and cashflow functionality. So, it is upto the user what he/she wants.


(Stefan Oehme) #6

That’s exactly what tasks are for. If the user wants to create an ear he runs ´gradle ear´. If he wants to update the database he runs ´gradle updateDatabase´.

Please have a look at the task basics in the
user guide before you continue.


(prachi sharma) #7

Thanx @st_oehme,

I’ll surely try that. But my current problem is to skip some tasks on the basis of successful or non successful execution of some previous task.
I hope you can help me with that too.


(Stefan Oehme) #8

You are asking for a solution without having described your problem. Of course I could tell you how to do it, but I would not be doing you a favor with that. I strongly believe in finding the problem first and then arriving at a simple and elegant solution.

So please describe the use case you are trying to solve from a user’s perspective and then I can give you an example on how you would do that.


(prachi sharma) #9

Ok,
The scenario is that our customers have different requirements at different times. Sometimes they only need the ear, the other time they only need to create/update the database or only need the cash flow functionality. Our current product is build on apache ant and I have to convert it into gradle.
The code goes somewhat like this:
task buildInitiate { //default task
def console = System.console()
if (console) {
ear = console.readLine('Do you want to run EAR packaging ? ')
if(ear == ‘y’) {
//run earPackaging task
}
db = console.readLine('Do you want to run DB packaging ? ')
if(db == ‘y’) {
//run dbPackaging task
}
cff = console.readLine('Do you want cff ? ')
if(cff == ‘y’) {
//run cffPackaging task
}
}

task earPackaging {
//some code
}
task dbPackaging {
//some code
}
task cffPackaging {
//some code
}

So, according to the input appropiate task should be invoked. Like there is ‘antcall’ task in Ant.

This was first, second problem is that consider if customer selected for ear packaging, then it should execute only those tasks which are dependent on it and skip other tasks.


(Stefan Oehme) #10

The user can just call gradle earPackaging or gradle dbPackaging etc. The user selects the task to run on the command line. He can find out which tasks are available by running gradle tasks.

That’s Gradle’s default behavior.


(prachi sharma) #11

Ok, Thanks a lot. Its working now.