If you’re using Gradle for a while, this probably isn’t new for you, but I’m entering Neuland with this Gradle based project. Although I’m not as critical as Spring committer and fellow author Greg Turnquist is, but I had my doubts and moments of pain, too.

One of my favorite features of Spring Boot is the building dependency management which takes away a lot of the pain finding compatible versions of dependencies for you. It’s available through Parent-POMs in Maven and a dedicated Gradle Plugin.

To use Spring Boot 2 dependency management with at least Gradle 4, all you have to do is to define the desired Boot version in the global build script block, declare the build dependency and apply the plugin:

Listing 1. build.gradle
buildscript {
    ext {
        springBootVersion = "2.0.0.RELEASE"
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath "org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}"
    }
}

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

As you see, I’m adding Springs snapshot repositories as I’m on Boot 2 here. After that one can use dependencies without explicit versions as shown in Listing 2 with spring-boot-starter-hateoas

Listing 2. build.gradle, using managed dependencies
dependencies {
    compile "org.springframework.boot:spring-boot-starter-hateoas"
}

If you need to overwrite a specific version, you have two options, both apply for both Maven and Gradle. The obvious and wrong solution is specifying the versions right at the dependency. This way, dependency management gets circumvented, your build becomes fragile.

The right way is to use the dedicated properties for that.

You find all the properties in the spring-boot-dependencies-project, namely in the POM. They are partially hidden away in the reference documentation, but I find the above list more useful.

In a Maven project, you would use Maven properties, too. With Gradle you either use them in the ext map or - as I recommend - in a dedicated gradle.properties file. Listing 3 shows my current setup to test the new Spring-HATEOAS-Affordance-Api:

Listing 3. gradle.properties
spring-hateoas.version = 1.0.0.BUILD-SNAPSHOT
spring-plugin.version = 2.0.0.BUILD-SNAPSHOT

Stay tuned for further post on what this is and how you can use this.