Skip to content

Instantly share code, notes, and snippets.

@slavashvets
Created March 13, 2016 16:53

Revisions

  1. slavashvets created this gist Mar 13, 2016.
    38 changes: 38 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,38 @@
    /**
    Maven Resolution Strategy
    =========================

    Description:

    Adds ability to set Maven-like conflict resolution strategy for any configuration

    | Maven works on the principle of nearest wins strategy while resolving the dependency
    | conflicts, that means whichever version it finds nearer in the tree, it will take
    | that version and ignore the other versions.
    | [http://techidiocy.com/maven-dependency-version-conflict-problem-and-resolution/]

    Usage:

    1. Type `maven()` inside `resolutionStrategy` closure param

    Example (apply to all configuration):

    configurations.all.resolutionStrategy { maven() }

    Example (apply to compile configuration):

    configuration.compile.resolutionStrategy { maven() }
    */
    project.configurations.all {
    resolutionStrategy.extensions.add "maven", {
    def versionMap = new HashMap()
    resolutionStrategy.eachDependency { DependencyResolveDetails details ->
    def moduleId = "${details.requested.group}:${details.requested.name}" as String
    if (!versionMap.containsKey(moduleId)) {
    versionMap[moduleId] = details.requested.version
    } else {
    details.useVersion versionMap[moduleId]
    }
    }
    }
    }