How can I get a live list of all Upload tasks except those that have a mavenInstaller configured?
tasks.withType(Upload).matching { ??? }.all {
// do stuff }
Thanks for any help.
How can I get a live list of all Upload tasks except those that have a mavenInstaller configured?
tasks.withType(Upload).matching { ??? }.all {
// do stuff }
Thanks for any help.
Looking briefly at the code, I can see that upload task has ‘repositories’ of type RepositoryHandler. Check if the repositories contain the ‘mavenInstaller’ repo. Please paste the code when you got it working
Do not call mavenInstaller() on repositories because you’ll create the mavenInstaller
Hope that helps!
Hi Szczepan
The following works for me (I need to use introspection since the resolvers are in internal packages):
tasks.withType(Upload).all { Upload task ->
task.repositories.resolvers.findAll {
it.class.name.endsWith('MavenInstaller')
}.each { MavenResolver resolver ->
// resolver....
}
}
The same can be applied to get just the deployers (it.class.name.endsWith(‘MavenInstaller’)).
The way that is written doesn’t take advantage of the laziness capabilities of Gradle collections. So this code will only configure Upload tasks that exist at this point in time. A safer way to do this would be to write it so the configuration happens to all existing Upload tasks and future ones.
def uploadTasksWithMavenInstaller = tasks.withType(Upload).matching { Upload task ->
task.repositories.resolvers.findAll { it.class.name.endsWith('MavenInstaller') }
}
uploadTasksWithMavenInstaller.all { MavenResolver resolver ->
// resolver....
}
The uploadTasksWithMavenInstaller collection is dynamic, in that if any Upload tasks are created later that match the matching clause then they will “appear” in the collection. The .all() method will apply the closure to all existing items plus any that “appear” in the future when they appear.
Hm, now I’m a bit confused. I’m aware of the lazy live-lists and make use of them wherever I can, but I thought this also works when I do a withType(…).all. If my assumption is wrong, it means that if I want to do something for ALL current and future Upload tasks, I would have to do something like this:
def allUploadTasks = tasks.withType(Upload).matching { true }
allUploadTasks.all {
// it...
}
That seems odd. Is my assumption wrong or my conclusion (or both)??
As a small correction to your example: I would have a live-list of Task objects and not of MavenResolver objects.
Ah, you are right. There’s no way to get a live collection of installers, so you’d have to go through the tasks collection.
You’d only need:
tasks.withType(Upload)
The matching clause is redundant there.