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

时间:2015-11-14 09:26:29

标签: gradle spring-boot dependency-management spring-boot-gradle-plugin

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

我有以下项目设置:

parent
  |
  + build.gradle
  |
  + alpha
  |   |
  |   + build.gradle
  |
  + beta
  |   |
  |   + build.gradle
  • parent模块包含常见的项目配置。
  • alpha模块是一个模块,我想在其中使用spring-boot-dependencies bom中指定的版本号导入依赖项,但结果是标准jar。
  • beta模块是依赖于alpha的模块,结果是可执行的Spring Boot jar文件(包括所有依赖项)。因此,此项目既需要spring-boot-dependencies也需要spring-boot插件。

为了保持Gradle文件DRY,我已经将常用模块脚本提取到父{q} 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')
}

beta 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')
}

评论:

  • Spring Boot 1.3.0.M1中spring-boot插件was changed的行为
  • Gradle版本:2.8
  • Spring Boot版本1.3.0.RC1

1 个答案:

答案 0 :(得分:20)

事实证明,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块与子项目有关,但它将在声明的脚本中进行评估,并且没有声明它试图应用的插件的依赖项。