How to find Upload tasks that do not have a mavenInstaller configured

(Etienne Studer) #1

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.

(Szczepan Faber) #2

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 :slight_smile:

Hope that helps!

(Etienne Studer) #3

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 {
            }.each { MavenResolver resolver ->
                    // resolver....

The same can be applied to get just the deployers (‘MavenInstaller’)).

(Luke Daley) #4

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 {'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.

(Etienne Studer) #5

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)??

(Etienne Studer) #6

As a small correction to your example: I would have a live-list of Task objects and not of MavenResolver objects.

(Luke Daley) #7

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.

(Luke Daley) #8

You’d only need:


The matching clause is redundant there.