0

I have test build.gradle file as follows

task someTask(type: Sync) {
   def folder = new File('fold1/fold2/');
   if(!folder.exists()) {
        throw new GradleException('Folder Absent');
    }
    else {

    }
}

When I do gradle tasks --all it is running the task and throwing exception. I was thinking that only when this task is run that it will check for folder but it is actually running it for any task I run.

Can someone suggest workaround for this?

Thanks in advance.

jarus
  • 74
  • 7

1 Answers1

1

Your code is executed during the configuration phase and not during the execution phase. You need to put it in a doFirst or doLast block:

task someTask(type: Sync) {
    doLast {
        def folder = new File('fold1/fold2/');
        if (!folder.exists()) {
            throw new GradleException('Folder Absent');
        }
        else {

        }
    }
}

See also: Why is my Gradle task always running?

Community
  • 1
  • 1
Roland Weisleder
  • 9,668
  • 7
  • 37
  • 59