How to override the version comparison mechanism?


(Uladzislau Shalamitski) #1

I have three repositories. The first repository contains version 0.2.0-RELEASE. The second contains 0.2.0-NIGHTLY. And the third contains 0.2.0-LOCAL.

I found in the source code that gradle uses a lexicographical comparison. Gradle downloads version 0.2.0-RELEASE because “RELEASE”.compareTo(“NIGHTLY”) > 0 and “RELEASE”.compareTo(“LOCAL”) > 0

Does anyone know how to override this behavior?


(Lance Java) #2

You could define a custom ResolutionStrategy

configurations.all {
   resolutionStrategy {
      eachDependency { DependencyResolveDetails details ->
         def req = details.requested
         if (req.group == 'foo' && req.version.endsWith('-NIGHTLY') {
            details.useVersion doSomeTransformation(req.version)
         }
      }
   }
}

(Uladzislau Shalamitski) #3

Unfortunately it does not work in my case. I don’t request the version that ends with “-NIGHTLY” or “-RELEASE”. I request the version 0.2.+


(Lance Java) #5

Ah, I thought you had a dependency conflict but you are referring to dynamic dependencies. I think this can be done via a ComponentSelectionRule

resolutionStrategy {
   componentSelection {
      Comparator<ComponentSelection> comparator = new MyFantasticVersionComparator()
      Map<String, Set<ComponentSelection>> grouped = [:]
      all { ComponentSelection selection ->
         String key = "${selection.candidate.group}:${selection.candidate.name}"
         def set = grouped[key]
         if (!set) {
            set = new TreeSet(comparator)
            grouped[key] = set
         }
         set<< selection
      }
      for (Set set : grouped.values()) {
         Iterator<ComponentSelection> iterator = set.iterator()
         for (int i = 0; i < set.size() - 1) {
            iterator.next().reject("Better version available")
         }
      }
   }
}

Cannot resolve dependencies that are resolved by Maven
Best Practice Resolving Version Conflicts within Thirdparty Libraries
Proper way to resolve transitive modules with and without Scala variants