首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >多模块Spring项目中的Gradle依赖插件

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

Stack Overflow用户
提问于 2015-11-14 09:26:28
回答 1查看 10.8K关注 0票数 20

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

我有以下项目设置:

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

为了保持Gradle文件的干燥,我已经将常见的模块脚本提取到父文件的build.gradle文件中。

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

代码语言:javascript
复制
> Plugin with id 'io.spring.dependency-management' not found.

亲本gradle.build

代码语言:javascript
复制
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}")
        }
    }
}

αbuild.gradle

代码语言:javascript
复制
dependencies {
    compile('org.springframework:spring-web')
}

βgradle.build

代码语言:javascript
复制
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插件was changed在SpringBoot1.3.0.M1中的行为
  • 分级版本: 2.8
  • Spring版本1.3.0.RC1
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-11-14 22:20:39

事实证明,parent/build.gradle应该按以下方式重新排列:

代码语言:javascript
复制
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块与子项目有关,但它将在声明的脚本中进行计算,并且它试图应用的插件没有声明依赖项。

票数 22
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33706957

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档