Milo Bem
Milo Bem

Reputation: 1033

how to declare dependency version in one place for many gradle projects

I work on a project with multiple grails services, plugins and libraries, all built with gradle with their dependencies declared in build.gradle files, one per project, this makes sense, I hope.

In maven I used to be able to declare versions of all dependencies in one parent project pom, or a pom template, and only include the dependencies in the projects that required them without the versions. This made upgrading dependencies easy in one place. Is there a simple way to do this in gradle?

Pseudocode example:

master_template/build.gradle

    dependencies {
        joda-time:joda-time:2.9.1
        cglib:cglib:3.2.9
        javax.servlet:javax.servlet-api:3.1.0
    }

service_a/build.gradle

    parent: master_template
    dependencies {
        joda-time:joda-time
        javax.servlet:javax.servlet-api
    }

service_b/build.gradle

    parent: master_template
    dependencies {
        cglib:cglib
        javax.servlet:javax.servlet-api
    }

Upvotes: 2

Views: 1500

Answers (2)

injecteer
injecteer

Reputation: 20699

You can easily achieve what you want by using Gradle's default apply from: 'other.gradle', so no additional plugins are needed.

In my micro-service project I'm using something like that:

common-gradle/env.gradle

apply plugin:'groovy'

ext.compile = [ 'joda-time:joda-time:2.9.1', 'cglib:cglib:3.2.9` ]
ext.testCompile = [ 'org.spockframework:spock-core:1.3-groovy-2.5' ]

common-gradle/dependencies.gradle

dependencies {
  compile ext.compile
  testCompile ext.testCompile
}

And the usage

service_a/build.gradle

apply from:'../common-gradle/env.gradle'

ext.compile << 'ch.qos.logback:logback-classic:1.2.3'

apply from:'../common-gradle/dependencies.gradle'

Thus each of my build.gradle files contain only 3-5 lines of critical information like project name and version.

You don't need to import the common-gradle as a project in your IDE, you can simply use symlinks to avoid using external references. Also during build on a Jenkins-like pipeline, the only thing you have to do is to check out the common-gradle repo into your working dir.

Upvotes: 1

Peter
Peter

Reputation: 5194

You can create a multi module project like you would do in maven with a parent pom.

In order to manage the dependency in the parent, I use the spring dependency management plugin.

You parent build.gradle would look like:

subprojects {
    apply plugin: "io.spring.dependency-management"

    sourceCompatibility = 1.8
    targetCompatibility = 1.8

    check.dependsOn dependencyCheckAggregate

    repositories {
        mavenLocal()
        jcenter()
        // other repos
    }

    dependencyManagement {

        def jacksonVersion = "2.+"

        dependencies {
            dependency "com.fasterxml.jackson.core:jackson-annotations:$jacksonVersion"
            dependency "com.fasterxml.jackson.core:jackson-core:$jacksonVersion"
            dependency "com.fasterxml.jackson.core:jackson-databind:$jacksonVersion"
            dependency "com.fasterxml.jackson.datatype:jackson-datatype-jdk8:$jacksonVersion"
            dependency "com.fasterxml.jackson.jaxrs:jackson-jaxrs-json-provider:$jacksonVersion"
        }
    }
}

Now, you can add dependencies to your submodules without specifying version.

Upvotes: 4

Related Questions