Execute task.dependsOn only on a Condition in Gradle
Asked Answered
S

1

10

I have two tasks Task-A and Task-B

This is my Task-A

task Task-A () {
  doLast {
    def fileName = _.property('fileName')
    if (fileName !=null) {
            println 'success'
   }
  }
}

My Task-B is dependent on Task-A and I should make it dependent only on the Condition that _.property('fileName') should exist and should not be null

So I wrote my Task-B this way

task Task-B () {
      doFirst {
        def fileName = _.property('fileName')
        if (fileName !=null) {
            dependsOn 'Task-A'
        }
       }
 }

It throws an Error

Cannot call Task.dependsOn(Object...) on task ':Task-B' after task has started execution.

How to execute dependsOn on a condition ?

Statuary answered 5/10, 2018 at 14:28 Comment(0)
P
11

You must set dependsOn directives during the configuration phase

try :

task Task-B () {
    def fileName = _.property('fileName')
    if (fileName !=null) {
        dependsOn 'Task-A'
    }
}
Povertystricken answered 5/10, 2018 at 14:32 Comment(1)
Lovely ... It worked like a Charm . Thank you very much !!Statuary

© 2022 - 2024 — McMap. All rights reserved.