Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute task.dependsOn only on a Condition in Gradle

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 ?

like image 840
Arun Avatar asked Oct 05 '18 14:10

Arun


People also ask

How do I exclude tasks in Gradle?

Using Command Line Flags Task :compileTestJava > Task :processTestResources NO-SOURCE > Task :testClasses > Task :test > ... To skip any task from the Gradle build, we can use the -x or –exclude-task option. In this case, we'll use “-x test” to skip tests from the build.

What is doLast in Gradle?

The doLast creates a task action that runs when the task executes. Without it, you're running the code at configuration time on every build. Both of these print the line, but the first one only prints the line when the testTask is supposed to be executed.

How do I define a task in Gradle?

Defining Tasks. Task is a keyword which is used to define a task into build script. Take a look at the following example which represents a task named hello that prints tutorialspoint. Copy and save the following script into a file named build.


1 Answers

You must set dependsOn directives during the configuration phase

try :

task Task-B () {
    def fileName = _.property('fileName')
    if (fileName !=null) {
        dependsOn 'Task-A'
    }
}
like image 157
ToYonos Avatar answered Oct 23 '22 12:10

ToYonos