# Call tasks a few times with different condition

**URL:** <https://discuss.gradle.org/t/call-tasks-a-few-times-with-different-condition/26113>\
**Category:** Help/Discuss\
**Created:** [March 6, 2018, 11:51am UTC](https://discuss.gradle.org/t/call-tasks-a-few-times-with-different-condition/26113 "2018-03-06T11:51:12Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eredar](https://avatars.discourse-cdn.com/v4/letter/e/2bfe46/32.png) [@Eredar](https://discuss.gradle.org/u/Eredar)\
**Post date:** [March 6, 2018, 11:51am UTC](https://discuss.gradle.org/t/call-tasks-a-few-times-with-different-condition/26113/1 "2018-03-06T11:51:12Z")

</div>

Hi all!  
I tried to call task from another different tasks using ‘dependsOn’ a few times with different conditions.

Example:

> gradle.properies  
> `group=a,b,c`

> gradle.build  
> def array=group.split(‘,’)  
> def var  
> task A{  
> doLast{  
> println “taskA=”+var  
> }  
> }  
> task B{  
> doLast{  
> println “taskB=”+var  
> }  
> }  
> task C{  
> doLast{  
> println “taskC=”+var  
> }  
> }  
> array.eachWithIndex { letter, index →   
> task “newTask${index}” {  
> if (letter==‘a’) {  
> var = letter  
> dependsOn A  
> dependsOn B  
> } else {  
> if(letter==‘b’){  
> var = letter  
> dependsOn A  
> dependsOn C  
> } else {  
> if(letter==‘c’){  
> var = letter  
> dependsOn B  
> dependsOn C  
> }  
> }  
> }  
> }

newTask2.dependsOn newTask1  
newTask1.dependsOn newTask0

I would to get in result  
:newTask0  
Task A=a  
Task B=a

:newTask1  
Task A=b  
Task B=b

:newTask2  
Task A=c  
Task B=c

Please, tell me how I can realize my idea.  
Thank you

---

<div class="post-metadata">

**Author:** ![jjustinic](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/jjustinic/32/9005_2.png) [@jjustinic](https://discuss.gradle.org/u/jjustinic)\
**Post date:** [March 8, 2018, 6:20am UTC](https://discuss.gradle.org/t/call-tasks-a-few-times-with-different-condition/26113/2 "2018-03-08T06:20:59Z")

</div>

You cannot have the same task run multiple times in the same build. Instead of creating ad-hoc tasks for `A`, `B`, and `C` , you should create custom task types for the work done by `A`, `B`, and `C`. You will then create multiple tasks of this type, configured with the correct variable. See [Gradle Guide on Writing Gradle Tasks](https://guides.gradle.org/writing-gradle-tasks/).

Your `A` task could look something like this:

```groovy
class TaskA extends DefaultTask {
    String var

    @TaskAction
    void taskAction() {
        println "taskA=${var}"
    }
}

```

Your logic would then decide which two task types to create for each letter, set the `var` to that letter, and then add any task dependencies that you want.

---

<div class="post-metadata">

**Author:** ![ddimitrov](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/ddimitrov/32/141_2.png) [@ddimitrov](https://discuss.gradle.org/u/ddimitrov)\
**Post date:** [March 9, 2018, 1:42pm UTC](https://discuss.gradle.org/t/call-tasks-a-few-times-with-different-condition/26113/3 "2018-03-09T13:42:41Z")

</div>

Agree about the task-per-invocation. If the tasks are similar enough, you may consider using a [task rules](https://docs.gradle.org/current/userguide/more_about_tasks.html#sec:task_rules) and ad-hoc task (rather than implementing full fledged custom task - see the example in the link).

---

<div class="post-metadata">

**Author:** ![Eredar](https://avatars.discourse-cdn.com/v4/letter/e/2bfe46/32.png) [@Eredar](https://discuss.gradle.org/u/Eredar)\
**Post date:** [March 13, 2018, 8:30am UTC](https://discuss.gradle.org/t/call-tasks-a-few-times-with-different-condition/26113/4 "2018-03-13T08:30:24Z")

</div>

Thank you for your response!

---

<div class="post-metadata">

**Author:** ![Eredar](https://avatars.discourse-cdn.com/v4/letter/e/2bfe46/32.png) [@Eredar](https://discuss.gradle.org/u/Eredar)\
**Post date:** [March 13, 2018, 8:35am UTC](https://discuss.gradle.org/t/call-tasks-a-few-times-with-different-condition/26113/5 "2018-03-13T08:35:56Z")

</div>

Thank you for your response!) It’s may to work correctly, but solution above is easier for using to my project.
