我尝试使用Gradle 7.5.1创建了一个Spring Boot 2.7.5的项目
首先
我用Gradle创建了一个Spring Boot项目,以下是步骤。我将使用当前最新版本的库。我将尝试使用Gradle插件和BOM两种方法来指定Spring Boot的版本。我还尝试在版本目录的toml文件中进行配置。
目标读者
- Gradle, Spring Boot で開発経験のある方
环境
-
- Gradle 7.5.1
-
- Java 18.0.2
- OS Windows11
项目创建
用Gradle init创建一个作为基础的项目。
$ gradle init --type java-application --test-framework junit-jupiter --dsl groovy --project-name SpringBootExample --package example
Generate build using new APIs and behavior (some features may change in the next minor release)? (default: no) [yes, no]
> Task :init
Get more help with your project: https://docs.gradle.org/7.5.1/samples/sample_building_java_applications.html
BUILD SUCCESSFUL in 3s
2 actionable tasks: 2 executed
您可以在下面查看命令的选项。
$ gradle help --task :init
将会创建如下文件。
│ .gitattributes
│ .gitignore
│ gradlew
│ gradlew.bat
│ settings.gradle
│
├─.gradle
├─app
│ │ build.gradle
│ │
│ └─src
│ ├─main
│ │ ├─java
│ │ │ └─example
│ │ │ App.java
│ │ │
│ │ └─resources
│ └─test
│ ├─java
│ │ └─example
│ │ AppTest.java
│ │
│ └─resources
└─gradle
└─wrapper
gradle-wrapper.jar
gradle-wrapper.properties
添加Spring库
我尝试了两种版本指定的方法,将依赖库添加到 build.gradle 文件中。
如果使用Spring Boot Gradle插件
使用id来指定org.springframework.boot。通过插件指定的版本将被应用到Spring的各个库中,这样在多项目配置中,构建文件的管理将变得更加容易。
这次虽然没有使用,但是从Spring Initializer创建的项目似乎是按照这种方式来的。
plugins {
id 'application'
id 'org.springframework.boot' version '2.7.5'
id 'io.spring.dependency-management' version '1.1.0'
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.junit.jupiter:junit-jupiter:5.8.2'
}
repositories {
mavenCentral()
}
application {
mainClass = 'example.App'
}
tasks.named('test') {
useJUnitPlatform()
}
使用BOM的情况
这是一种在实施平台中使用BOM指定的模式。我在某个地方看到过关于大型项目构建时间差异的文章,很好奇实际情况如何。
plugins {
id 'application'
}
dependencies {
implementation platform('org.springframework.boot:spring-boot-dependencies:2.7.5')
implementation 'org.springframework.boot:spring-boot-starter'
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.junit.jupiter:junit-jupiter:5.8.2'
}
以下略..
没有动的模式
似乎还有其他方法可以使用,但我不清楚使用的方法。
dependencies {
implementation platform(org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES)
}
可能发生这样的错误。是版本的问题还是根本使用方法错误呢?
* What went wrong:
A problem occurred evaluating project ':app'.
> Could not get unknown property 'org' for object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.
版本目录中的版本定义
如果使用版本目录的机制,可以在settings.gradle或toml文件中进行版本管理。本次我们将尝试使用toml文件的模式。
toml文件需要手动在gradle文件夹的顶层中创建,并且需要命名为libs.versions.toml。在gradle init命令中不会自动创建该文件。
└─gradle
libs.versions.toml
[versions]
spring = "2.7.5"
spring-dependency = "1.1.0"
[libraries]
# プラグインを使う場合
spring-starter = {module = "org.springframework.boot:spring-boot-starter"}
spring-web = {module = "org.springframework.boot:spring-boot-starter-web"}
# BOM を使う場合
spring-bom = {module = "org.springframework.boot:spring-boot-dependencies", version.ref = "spring"}
[plugins]
spring-boot = { id = "org.springframework.boot", version.ref = "spring" }
spring-dependency = { id = "io.spring.dependency-management", version.ref = "spring-dependency" }
如果使用Spring Boot Gradle插件,可以按照以下方式进行修改。
plugins {
id 'application'
alias libs.plugins.spring.boot
alias libs.plugins.spring.dependency
}
dependencies {
implementation libs.spring.starter
implementation libs.spring.web
testImplementation 'org.junit.jupiter:junit-jupiter:5.8.2'
}
以下略..
使用BOM时,需要将连字符分隔的键修改为点分隔才能避免错误。例如,”spring-starter”应该以”spring.starter”的形式进行引用。要进行修正,请按照以下步骤操作。
plugins {
id 'application'
}
dependencies {
implementation platform(libs.spring.bom)
implementation libs.spring.starter
implementation libs.spring.web
testImplementation 'org.junit.jupiter:junit-jupiter:5.8.2'
}
以下略..
应用程序插件和JUnit库没有进行修改,但也可以通过toml文件进行定义。
你好世界的实现
实现HelloWorld界面。省略测试类的修改。
package example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@SpringBootApplication
@Controller
public class App {
public static void main(final String[] args) {
SpringApplication.run(App.class, args);
}
@GetMapping("/")
@ResponseBody
public String hello() {
return "<h1>hello!</h1>";
}
}
执行
我会尝试通过命令来启动。
$ gradle run
只要能够通过浏览器访问,就算完成了。
