我已经广泛搜索了这个问题的答案。我对 Maven 有广泛的了解,但对 Gradle 还是很陌生。我正在使用 Intellij 2019.2 Ultimate。我的 build.gradle 设置如下:
plugins {
id 'java'
id 'org.springframework.boot' version '2.2.6.RELEASE'
}
apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'idea'
apply plugin: 'io.spring.dependency-management'
dependencies {
implementation 'org.springframework.boot:spring-boot-dependencies:2.2.6.RELEASE'
}
tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
options.compilerArgs << '-Xlint:unchecked'
options.deprecation = true
}
compileJava {
options.incremental = true
options.fork = true
options.failOnError = false
}
compileTestJava {
options.incremental = true
options.fork = true
options.failOnError = false
}
springBoot {
mainClassName = "com.app.StartMain"
}
bootWar {
manifest {
attributes 'Start-Class': 'com.app.StartMain'
}
}
java {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
我会认为 Gradle 会下载 spring-boot-dependencies-2.2.6.pom 文件中的所有依赖项,并放置在 Intellij 的外部依赖项树中。但这并没有发生。该文件中有大量依赖项,我只是认为这就是它的行为方式。我的构建文件中是否缺少某些内容?
提前致谢!
最佳答案
spring-boot-dependencies
module 是一个 BOM( Material list ),其中包含与 Spring Boot 兼容的模块和库版本的精选列表。但它只建议使用哪个版本,实际上并不会让您的项目依赖它们。如果你打开它的 POM 文件(here 是 Spring Boot 2.2.6 的那个),你会看到它们都在一个 <dependencyManagement>
中。堵塞。如果它们被声明为实际依赖项,它们将位于 <dependencies>
中。在项目根目录下直接阻止。 Maven 和 Gradle 的这种行为是相同的。
我不认为有一个“全部”库可以让您在项目中获得所有这些依赖项。但这也是不好的做法,因为您不太可能需要它们。相反,您应该使用 Spring Boot Starters这对你来说很有意义。
即使 Spring Boot 中的当前文档告诉您不同,我建议您不要使用 io.spring.dependency-management
插件,因为它以 Gradle 中不标准的方式配置依赖项,这可能会导致混淆。它是在 Gradle 不支持原生导入 BOM 文件的时候创建的,但现在可以了。这是一种在没有它的情况下声明依赖项的方法:
dependencies {
implementation platform("org.springframework.boot:spring-boot-dependencies:2.2.6-RELEASE") // Import the BOM
implementation "org.springframework.boot"spring-boot-starter" // Use a Spring Boot starter (and not that there is no version defined)
}
关于spring-boot - 如何使用 gradle 下载所有 Spring Boot 依赖项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60982111/