How Do I Migrate My Gradle Dependencies To Be Transitive?

May 18, 2013

Background

We’re using gradle as a build tool and for managing dependencies on our Java-based project. The project is multi-module, with one parent build.gradle file and each module (subproject) having its own build.gradle file.

Problem

The parent build.gradle defines an ivy repository with a maven layout for each of the subprojects. Transitive dependencies don’t resolve with this configuration, so most of the transitives are required individually. We’d like to change it to simply be a maven repository and have dependencies transitively resolved - cutting out about 50% of our build file - but that causes many conflicts.

Solution

Migrate piecemeal. In each subproject, we can define the same repository url as a maven repository, and transitive resolution will work for that project only. Once this is done for all the children, the parent can be switched to a maven repository and we can delete the old ivy repository.

Example parent build.gradle:

subprojects {
    repositories {
        ivy {
            layout 'maven'
            url 'path://to/my/maven/repo'
        }
    }
}

Example child build.gradle:

repositories {
    maven {
        url 'path://to/my/maven/repo'
    }
}

Gotchas

Watch out for projects that depend on other projects. If A depends on B, and you make B transitively resolved first, A will not compile. It’s most effective (and least painful) to start making the outermost projects transitive first.


Profile picture

Written by @sghill, who works on build, automated change, and continuous integration systems.