【问题标题】:Gradle dependency plugin in a multi module Spring Boot project多模块 Spring Boot 项目中的 Gradle 依赖插件
【发布时间】:2016-02-15 20:58:09
【问题描述】:

在使用 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-dependenciesspring-boot 插件。

为了保持 Gradle 文件 DRY,我已将常用模块脚本提取到父级的 build.gradle 文件中。

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

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

父 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 插件 was changed 在 Spring Boot 1.3.0.M1 中的行为
  • Gradle 版本:2.8
  • Spring Boot 版本 1.3.0.RC1

【问题讨论】:

  • 如果我的回答有帮助,请点赞。
  • @Opal 抱歉耽搁了,我以为我已经接受了你的回答。我已经就此事写了blog post,请查看致谢。 :-)
  • 谢谢,正是我遇到的问题 --- 为了帮助其他人在谷歌上搜索这个问题,我会补充:如果你天真地尝试:将插件“spring-boot”应用于所有子项目,(仅按顺序免费获得spring依赖管理)你会得到构建错误(至少我在windows下得到了),比如:任务':xxxxx:bootRepackage'的执行失败。无法将“xxxxx.jar”重命名为“xxxxx.jar.original”
  • @matsev,刚刚注意到有关文章的信息 - 看起来不错。感谢您被提及!

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


【解决方案1】:

原来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 块与子项目相关,但它会在声明的脚本中评估,并且没有为它试图应用的插件声明依赖项。

【讨论】:

  • 谢谢,但它并不完全有效。现在 Gradle 无法找到子模块的存储库:> Cannot resolve external dependency org.springframework.boot:spring-boot-dependencies:1.3.0.RC1 because no repositories are defined. Required by: com.example:alpha:0.0.1-SNAPSHOTrepositories 复制到 allprojects 可以解决问题,但是有没有办法定义 repositories once 以便可以从buildscriptallprojects?
  • 否 :/ 不幸的是,这些是单独的配置。它需要复制。
  • 仅供参考,buildscript 适用于 build.gradle,而不适用于其中的任何特定项目,因此您发现的并不奇怪。但是您的答案与您的问题中显示的不同;根项目不再有dependencyManagement
猜你喜欢
  • 2014-11-26
  • 2016-06-11
  • 2018-10-06
  • 2018-09-21
  • 2020-01-12
  • 1970-01-01
  • 2017-06-27
  • 2018-01-31
  • 2017-08-16
相关资源
最近更新 更多