Adam ,

Thanks for your quick reply and suggestions.

>Not yet. These sorts of things (run some task before any other task, run some 
>task after all other tasks) we want to make easy to declare through the DSL.

Is there a Jira issue for this, that we might vote for? If not, do you want me 
to create one?

-Steinar


From: Adam Murdoch [mailto:[email protected]]
Sent: 26. mai 2011 21:08
To: [email protected]
Subject: Re: [gradle-user] Performing a clean that depends only on a property


On 26/05/2011, at 9:24 PM, 
<[email protected]<mailto:[email protected]>> 
<[email protected]<mailto:[email protected]>> wrote:


Hi,

I have written a simple task that performs a clean if the main version changes. 
This is not something that happens very often (4 times a year), but it can be a 
timeconsuming hassle if a developer forgets to do it locally. In addition, we 
have many different bamboo plans that are affected (x2, since they're running 
on 2 different servers), which today needs to be manually cleaned.

The task currently looks like this:

task cleanWhenMainVersionChange {
    inputs.properties(["mainVersion": mainVersion])
    outputs.files file("$buildDir/cleanWhenMainVersionChange.txt")
    doLast {
        clean.execute()
    }
}

To work properly, this code needs to be used in 4 different multiprojects, with 
a varying number of tasks being performed for each sub-project. We want to 
ensure that this task is the first to run.

The only way I am aware of to do this, is by setting dependencies. However, 
since there are several different tasks that have the potential to be first, we 
have to set dependencies from all of them to the "cleanWhenMainVersionChange" 
task.

Is there a simpler way to do this, preferably one where we don't need to know 
about all the tasks that have the potential to be executed first?

Not yet. These sorts of things (run some task before any other task, run some 
task after all other tasks) we want to make easy to declare through the DSL.

One option to the moment is to have all tasks depend on your clean task:

tasks.matching { it != cleanWhenMainVersionChanges }.all {
    dependsOn cleanWhenMainVersionChanges
}

Another option is to use the task graph whenReady() hook:

gradle.taskGraph.whenReady {
    cleanWhenMainVersionChanges.execute()
}

However, this uses an internal API which might go away. We don't recommend 
executing tasks by calling execute().





Finally; a question regarding outputs:
If I don't declare any outputs, the task is executed every time. Running with 
the debug flag reveals that gradle considers any task with no outputs declared 
to never be up-to-date.

However, as you can see, I don't need to actually create the file - only the 
output declaration. It seems to work as intended if I don't create the file; 
i.e. it is only performed if the mainVersion property is changed.

Is this intended? If so, why do we need to specify the outputs declaration here?

The incremental build feature is currently intended for tasks that create 
things, not tasks that destroy things. If you don't declare something about the 
outputs, Gradle assumes that the task either doesn't build anything or will 
take care of incremental-ness on it's own.

You can tell Gradle that a task doesn't produce any output like this:

task cleanWhenMainVersionChanges {
    outputs.upToDateWhen { true }
}


--
Adam Murdoch
Gradle Co-founder
http://www.gradle.org
VP of Engineering, Gradleware Inc. - Gradle Training, Support, Consulting
http://www.gradleware.com

Reply via email to