Gradle is running tasks that shouldn't be on the task graph


(cblistserv) #1

Running gradle-1.0-milestone-5-20110913092800+0200

I’ve got a toy project in progress to demo Gradle.

I noticed that some tasks I’ve written are being executed when I run ‘gradle compile’, even though compile doesn’t depend on any of these tasks.

In a file called minimal.gradle I define this task:

task showDependencyReport {

reportFile = new File(‘build/reports/project/dependencies.txt’)

println reportFile.text }

And then I import that file in build.gradle like this: apply from: ‘build-minimal.gradle’

When I run anything from build.gradle, the task showDependencyReport always gets executed.

Can anyone explain why, or how I’d debug this?


(cblistserv) #2

I have figured out why this happened… the task declaration should have been ‘task showDependencyReport << { … }’ or one of the other forms.

But I still don’t understand why Gradle didn’t reject my syntax, and it created a task that always runs.

Can someone explain what actually happened there?


(Luke Daley) #3

with the following syntax:

task someTask {
  // I'm configuration of the task object
}

The code block is considered configuration of the task, and is run during build evaluation. If you consider the Copy task…

task copyStuff(type: Copy) {
  from "someDir"
  into "someOtherDir"
}

You can see that here we are configuring the instance of Copy, which needs to happen during evaluation.

The syntax:

task someTask << {
  // stuff to do when the task is executed
}

is shorthand for:

task someTask {
  doLast {
    // stuff to do when the task is executed
  }
}

(cblistserv) #4

Makes sense, thanks.