首页 > 解决方案 > Gradle 编译项目使用 Spring Boot 插件失败

问题描述

我试图理解为什么将 Gradle 4.x Spring Boot 插件添加到 Gradle 依赖项会导致我的构建失败。基于此链接的设置:

Project
  |--build.gradle //plugin here is fine
  |--settings.gradle
Dependency
  |--build.gradle //plugin here causes failure
  |--com.activemq.common //dependency I want to import

在 Dependency/build.gradle 中,如果我只有:

//Dependency/build.gradle
apply plugin: 'java'

gradle build --> 这按预期工作

现在,如果我添加 Spring Boot 插件,它会失败:

//Dependency/build.gradle
apply plugin: 'java'
apply plugin: 'org.springframework.boot'

repositories {
    mavenCentral()
}

我收到一个错误,它找不到依赖项下的包

gradle build --> Application.java:5: error: package com.activemq.common 不存在

我可以删除插件,但依赖项也是 Spring Boot,所以我想拥有它。

我尝试做 gradle build --info,但没有看到任何有用的东西。还尝试了 Gradle 5,但出现了我仍在调查的不同错误。谁能解释为什么添加插件会导致此失败?

标签: javaspring-bootgradle

解决方案


看起来我需要修复两件事

  1. 我不能像上面那样使用典型的 gradle.build,但必须使用一个特殊的“Spring Boot 的独立依赖管理”版本,它使用“mavenBom”——这个例子对我有用https://github.com/spring-guides/ gs-multi-module/blob/master/complete/library/build.gradle

  2. 我也不能在 Project/Dependency 文件夹中使用 settings.gradle,而是需要将 settings.gradle 放在顶层,如下所示: https ://github.com/spring-guides/gs-multi-module/ blob/master/complete/settings.gradle

这没有用:

Project
  |--settings.gradle
Dependency
  |--settings.gradle

这有效:

Project
  |
Dependency
  |
settings.gradle

推荐阅读