Question

How to add Spring 4 BOM dependencies in Gradle configuration file?

In Maven it could be done as mentioned here.

Maven equivalent:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-framework-bom</artifactId>
            <version>${spring.framework.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<repositories>
    <repository>
        <id>spring-milestones</id>
        <url>http://repo.spring.io/milestone</url>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </repository>
</repositories>
Was it helpful?

Solution

From https://github.com/spring-gradle-plugins/dependency-management-plugin

plugins {
   id "io.spring.dependency-management" version "0.4.1.RELEASE"
}

dependencyManagement {
    imports {
        mavenBom 'org.springframework:spring-framework-bom:4.1.6.RELEASE'
    }
}

OTHER TIPS

As far as I know Gradle has no analogue of Maven's dependency management. You have to declare suitable versions of the dependencies manually.

Check out this spring gradle plugin

A Gradle plugin that provides Maven-like dependency management and exclusions

https://github.com/spring-gradle-plugins/dependency-management-plugin

Basically, there are 2.5 choices here.

Using Gradle's native BOM support

As of Gradle 4.6 it supports BOMs natively. Add these lines in your build files:

settings.gradle:

enableFeaturePreview('IMPROVED_POM_SUPPORT')

Starting from Gradle 5.0 it's not needed.

build.gradle:

dependencies {
    implementation("org.springframework.boot:spring-boot-dependencies:2.0.3.RELEASE")

    // Now you can import Spring dependencies without particular version:
    implementation("org.springframework.boot:spring-boot-starter")
}

Using Spring Boot Gradle plugin

When you apply the io.spring.dependency-management plugin, Spring Boot’s plugin will automatically import the spring-boot-dependencies bom from the version of Spring Boot that you are using. This provides a similar dependency management experience to the one that’s enjoyed by Maven users. For example, it allows you to omit version numbers when declaring dependencies that are managed in the bom. To make use of this functionality, simply declare dependencies in the usual way but omit the version number.

build.gradle:

plugins {
    id "org.springframework.boot" version "2.0.3.RELEASE"
}

dependencies {
    // Now just import what you need:
    implementation "org.springframework.boot:spring-boot-starter"
}

Using io.spring.dependency-management

Nice feature is that you can use Spring's dependency plugin even if you're not interested in Spring Boot.

build.gradle:

plugins {
    id("io.spring.dependency-management") version "1.0.6.RELEASE"
}

dependencyManagement {
    imports {
        mavenBom("org.springframework.boot:spring-boot-dependencies:2.0.6.RELEASE")
        mavenBom("com.amazonaws:aws-java-sdk-bom:1.11.409") // Import any bom!
    }
}

dependencies {
    implementation("org.springframework.boot:spring-boot-starter")
    implementation("com.amazonaws:aws-java-sdk-dynamodb")
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top