In Gradle, how do I declare common dependencies in a single place?

后端 未结 7 1725
余生分开走
余生分开走 2020-11-29 16:08

In Maven there is a very useful feature when you can define a dependency in the section of the parent POM, and reference that depen

7条回答
  •  被撕碎了的回忆
    2020-11-29 16:57

    io.spring.gradle:dependency-management-plugin plugin has problems with new Gradle 3.x series but stable for 2.x series. For reference look to bug report Drop support for Gradle 3 #115

    In case of Spring (main promoter of BOM usage) you may end with:

    buildscript {
        repositories {
            mavenLocal()
            jcenter()
        }
        dependencies {
            classpath 'io.spring.gradle:dependency-management-plugin:1.0.0.RELEASE'
        }
    }
    
    repositories {
        mavenLocal()
        jcenter()
    }
    
    apply plugin: 'java'
    apply plugin: 'io.spring.dependency-management'
    
    dependencyManagement {
        imports {
            mavenBom 'io.spring.platform:platform-bom:Athens-SR3'
        }
    }
    
    dependencies {
        compile 'org.springframework.boot:spring-boot-starter-web'
    
        testCompile 'org.springframework.boot:spring-boot-starter-test'
    }
    

    Note that io.spring.platform:platform-bom have org.springframework.boot:spring-boot-starter-parent as parent so it is compatable with Spring Boot

    You can verify actual dependency resolution via:

    $ gradle dependencies
    $ gradle dependencies --configuration compile
    $ gradle dependencies -p $SUBPROJ
    
    $ gradle buildEnvironment
    $ gradle buildEnvironment -p $SUBPROJ
    

    or with task:

    task showMeCache {
        configurations.compile.each { println it }
    }
    

    Read official Soring blog post Better dependency management for Gradle to understand the reason of introducing io.spring.gradle:dependency-management-plugin.

提交回复
热议问题