Gradle 多模块中的依赖插件 Spring 引导项目

Gradle dependency plugin in a multi module Spring Boot project

在使用 Gradle 插件 spring-boot-dependenciesspring-boot 的 multi-module 项目中,正确的 Gradle 配置是什么样的?

我有以下项目设置:

parent
  |
  + build.gradle
  |
  + alpha
  |   |
  |   + build.gradle
  |
  + beta
  |   |
  |   + build.gradle

为了使 Gradle 文件保持干燥,我已将通用模块脚本提取到 parent 的 build.gradle 文件中。

尝试使用下面的项目配置执行 $ gradle build 结果:

> Plugin with id 'io.spring.dependency-management' not found.

parent gradle.build

allprojects {
    group = "com.example"
    version '0.0.1-SNAPSHOT'

    ext {
        dependencyManagementPluginVersion = '0.5.3.RELEASE'
        springBootVersion = '1.3.0.RC1'
    }

    apply plugin: 'java'
    apply plugin: 'eclipse'
    apply plugin: 'idea'
}

subprojects {
    sourceCompatibility = 1.8
    targetCompatibility = 1.8

    buildscript {
        repositories {
            jcenter()
            maven { url "https://repo.spring.io/snapshot" }
            maven { url "https://repo.spring.io/milestone" }
        }
        dependencies {
            classpath("io.spring.gradle:dependency-management-plugin:${dependencyManagementPluginVersion}")
            classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
        }
    }

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

    dependencyManagement {
        imports {
            mavenBom("org.springframework.boot:spring-boot-dependencies:${springBootVersion}")
//            mavenBom("org.springframework.boot:spring-boot-starter-parent:${springBootVersion}")
        }
    }
}

alpha build.gradle

dependencies {
    compile('org.springframework:spring-web')
}

测试版gradle.build

apply plugin: 'spring-boot'

dependencies {
    compile project(':alpha')
    compile('org.springframework.boot:spring-boot-starter')
    compile('org.springframework.boot:spring-boot-starter-web')
}

评论:

原来 parent/build.gradle 应该按以下方式重新排列:

buildscript {
    ext {
        dependencyManagementPluginVersion = '0.5.3.RELEASE'
        springBootVersion = '1.3.0.RC1'
    }
    repositories {
        jcenter()
        maven { url "https://repo.spring.io/snapshot" }
        maven { url "https://repo.spring.io/milestone" }
    }
    dependencies {
        classpath("io.spring.gradle:dependency-management-plugin:${dependencyManagementPluginVersion}")
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

allprojects {
    group = "com.example"
    version '0.0.1-SNAPSHOT'

    apply plugin: 'java'
    apply plugin: 'eclipse'
    apply plugin: 'idea'
}

subprojects {
    sourceCompatibility = 1.8
    targetCompatibility = 1.8

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

    dependencyManagement {
        imports {
            mavenBom("org.springframework.boot:spring-boot-dependencies:${springBootVersion}")
//          mavenBom("org.springframework.boot:spring-boot-starter-parent:${springBootVersion}")
        }
    }
}

问题在于子项目的 buildscript 块确实配置得很好,但是......在错误的地方。此 subprojects 块与子项目相关,但它将在声明的脚本 中被评估 ,并且没有为它试图应用的插件声明任何依赖项。