When putting << into task I get an error


#1

Gradle Snippet

 task dist << {
dependsOn 'cobertura'
dependsOn 'zipAll'
}

 task init (type: JavaCompile) << {

ant.path(id: 'cobertura.classpath.id') {
	//resource ('tasks.properties')
	fileset(dir: project.'cobertura.dir', includes: 'cobertura-2.1.1.jar, lib/**/*.jar')
	fileset(dir: project.'junit.dir', includes: 'junit-4.8.2.jar')
	println 'path cobertura'
  }

}

When I added << to all my tasks I receive this error. my Script runs fine with out << but they all get executed during the config phase.


(Chris Doré) #2

Remove << from the dist task.

<< is a shortcut for calling Task.doLast {}. doLast{} adds an action to the task, which will be performed during the task execution phase. Task dependencies are resolved before the task execution phase begins, which is why you get an error saying that you cannot change the task dependencies.


#3

Okay, So when ever I have a task that dependsOn another task it cannot have do.Last, correct?


(Stefan Oehme) #4

doLast is for adding task actions (what your task should do when it executes).

All configuration needs to happen in the configuration phase.


#5

Okay for this snippet, When I have the dolast function the script does not compile. The ones with it will do what it is written. Why is that? if I remove the << it also compiles.

task EnabledUser(type: Zip) << {
from (‘hdm/function’)
include 'cloudUIEnabledUserTag.*'
archiveName 'cloudUIEnabled.zip’
destinationDir file(‘dist/hdm/function’)
}

task FirmwareMatch(type: Zip) {
from (‘hdm/function’)
include 'factoryResetOnFirmwareMatch.*'
archiveName 'factoryResetOnFirmwareMatch.zip’
destinationDir file(‘dist/hdm/function’)

}

task CloudUI(type: Zip)<< {
from (‘hdm/function/4.0.2’)
include 'cloudUIEnabledUserTag.*'
archiveName 'cloudUIEnabled.zip’
destinationDir file(‘dist/hdm/function/4.0.2’)

}


#6

Why is it when I use doLast it never goes into execution phase?


(Chris Doré) #7

My recommendation is to stop using <<. It ultimately just causes people confusion.

The general structure of a task definition looks like this:

task nameOfTask {
    // configuration time code
    println 'this is happening at configuration time'

    // one of the many methods available in all task classes is dependsOn
    dependsOn someOtherTask
    // available methods are determined by the task's type

    // one of the other methods available is doLast
    doLast {
        println 'this is happening at execution time'
    }
}

I recommend spending some time going through the user guide, as I think it will help a lot. The user guide uses << a lot, but again I recommend avoiding it, especially when learning Gradle. I suggest starting with sections 14 and 17 of https://docs.gradle.org/current/userguide/pt03.html if you want to learn more about tasks.

Just keep in mind that

task nameOfTask << {
    println 'during execution'
}

is equivalent to

task nameOfTask {
    doLast {
        println 'during execution'
    }
}

Chris