Replicating Maven "dependencyManagement" tag from inside Gradle build

Viewed 3631

I am trying to follow this Spring Boot/Vaadin guide however I'm using Gradle, not Maven.

At the very top of that guide they say to use the following Maven XML:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>com.vaadin</groupId>
            <artifactId>vaadin-bom</artifactId>
            <version>10.0.11</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

However I don't see a dependencyManagement task available via Gradle. So I ask: how do I replicate the same behavior as the <dependencyManagement/> XML element above over in "Gradle land"?

Update: current attempt

dependencyManagement {
     imports {
          mavenBom 'com.vaadin:vaadin-bom:10.0.11'
     }
}

Only problem with this is that when I add it to my build.gradle and then run ./gradlew clean I get the following Gradle error:

"Could not find method dependencyManagement() for arguments..."

1 Answers

This should give you a working build:

plugins {
    // the Gradle plugin which provides the “dependencyManagement” block
    id 'io.spring.dependency-management' version '1.0.6.RELEASE'
    // add Java build functionality to be able to follow the Vaadin guide
    id 'java'
}

dependencyManagement {
    imports {
        // the Maven BOM which contains a coherent set of module versions
        // for Vaadin dependencies
        mavenBom 'com.vaadin:vaadin-bom:10.0.11'
    }
}

repositories {
    // find dependency modules on Maven Central
    mavenCentral()
}

dependencies {
    // the dependency module you need according to the Vaadin with
    // Spring Boot guide; the version of the module is taken from the
    // imported BOM; transitive dependencies are automatically taken
    // care of by Gradle (just as with Maven)
    compile 'com.vaadin:vaadin-spring-boot-starter'
}

Run ./gradlew dependencies --configuration compileClasspath to see that all dependencies are available on your Java compile classpath now.


Edited to reply to the question in the comments: indeed, the import of the BOM leads to a slightly different set of dependencies than would be used without it. You can see the dependencies difference like so:

  1. ./gradlew dependencies --configuration compileClasspath > with-BOM.txt
  2. Remove the dependencyManagement block and add a version to the single dependency: compile 'com.vaadin:vaadin-spring-boot-starter:10.0.11'
  3. ./gradlew dependencies --configuration compileClasspath > without-BOM.txt
  4. diff -u with-BOM.txt without-BOM.txt

You can see slight differences like org.webjars.bowergithub.webcomponents:webcomponentsjs:1.2.6 being used with the BOM and version 1.2.2 without it. The reason for that can be found in the BOM where version 1.2.6 is defined and where the authors also mention the reason for that: “Transitive webjar dependencies, defined here for repeatable builds”

Related