Gradle newbie here again. I’m trying to understand the difference between defining a method and a task and how to decide which one should be used when?
The difference I see between a task and a method is that I can pass parameter/argument when calling a method and also return values from a method. I can’t do that with task. And, when I need to run the build script, I can’t call any method. I can only do that via a task.
Secondly, how do I run/call taskB from taskA? I’m not referring to dependency or run before/after type of scenario.
Please let me know if my question is not clear or need more details.
I suggest you do a web search for “call a gradle task from another task” and look at the results. In general, it’s not a good idea to directly execute tasks from another task, but it’s apparently possible, whether it will do what is truly expected is hard to say.
If you are creating a Gradle build then you should define your logic as tasks, not methods. Gradle configures and executes tasks, not methods. Also, like David mentioned, you should never directly call one task from another. Gradle should be responsible for executing your tasks, so tasks that need to run in a particular order should be described as such with dependencies. Typically if you feel you need to call one task from another, you probably just need to split that task into multiple tasks.
If you give a bit more context as to what you are trying to accomplish we can probably give you some better guidance.
Thanks David and Mark for your suggestions. I had already searched on this and saw a couple of posts about discouraging excuting one task from another task. I think I’m just having trouble organizing the build logic into individual tasks.
Here’s what I’m trying to do in general. The ultimate goal is to use the gradle script that will execute a command line utility
- Access an ATOM feed that contains some schedule 2. For each schedule:
a. Get schedule title
b. Extract some info from schedule description field
c. Update a properties file using the info received in previous step/task
d. Copy this updated properties file into a network share (this will vary depending on the schedule’s title)
e. Run a command line utility tool (this will receive a parameter based on the schedule’s title)
- Execute a java program (jar file)
a. Copy the output of the java program into various locations on the network
In a nutshell, that’s what I’m trying to achieve. I pretty much know how to do different pieces but struggling to put it together.
Right now I have a task, which completes step#1 by calling a method and it returns xml data as text. I then parse the xml content in that same task to extract schedule title and other info using another method (step #2b) and update a properties file (step #2c) in that same task. Now for step 2c, I have a separate task of type Copy. But I wasn’t sure how to call that task from the main task.
And the reason I used methods was to get data returned to the main task (i.e. return xml feed of the schedule, extracting certain info from description field, etc.)
I do feel like what I have so far is not vary… for lack of a better term, Gradly
If I use task dependency, I feel like it can get complex and not easily traceable. And I still don’t know how I’m going to pass info from one task to another (i.e. from step #1 to step #2)
Ok, it’s good that you described what you’re trying to do.
Frankly, it seems to me like you don’t need Gradle for this. If you just need to execute a complex series of procedural steps, just write a Groovy script to do this.
Each of these line items should be a task. Like David said, this is very procedural so the dependencies are simply, each step must depend on the one previous to it. The easiest way to pass data from one task to another is to create an output, typically in the form of a file. So step one would be to download the feed to an XML file, the next step would then consume that file, and so on.
To David’s point, if what you are trying to accomplish isn’t part of a “build” per-se, then perhaps writing a script would be simpler. However, Gradle is an automation tool, and can be used for all sorts of purposes and with many advantages like incremental support (if you declare inputs/outputs property) and dependency management.
Thanks again David and Mark for your suggestions.
This particular item is not necessarily a “build” project, but I’ll be working on converting some existing build projects written in ANT. And, since I’m just learning Gradle, I decided to start off with something that sounded easier at first. That’s why I choose to do this particular automation task similar to a gradle build.
I’ll try out declaring inputs/outputs property and chain dependency. Thanks again.