新建项目
直接在线新建: https://start.spring.io/
然后对比本地的区别, 修改 build.gradle.kts
编码问题
打包问题
参考 spring boot 插件官网: https://docs.spring.io/spring-boot/docs/current/gradle-plugin/reference/htmlsingle/
build.gradle.kts
import org.springframework.boot.gradle.tasks.bundling.BootJar
plugins {
// Apply the application plugin to add support for building a CLI application in Java.
application
java
id("org.springframework.boot") version "2.7.13"
id("io.spring.dependency-management") version "1.0.15.RELEASE"
}
repositories {
maven("https://maven.aliyun.com/repository/public/")
maven("https://maven.aliyun.com/repository/spring/")
mavenLocal()
mavenCentral()
}
dependencies {
// test
testImplementation("org.springframework.boot:spring-boot-starter-test")
testImplementation("org.testng:testng:7.5")
testImplementation("org.hamcrest:hamcrest-all:1.3")
// spring boot
implementation("org.springframework.boot:spring-boot-starter")
implementation("org.springframework.boot:spring-boot-starter-web")
// tool
implementation("cn.hutool:hutool-all:5.0.1")
implementation("com.google.guava:guava:31.1-jre")
implementation("org.apache.commons:commons-lang3")
implementation("org.mapstruct:mapstruct-jdk8:1.4.2.Final")
implementation("org.mapstruct:mapstruct-processor:1.4.2.Final")
// lombok
compileOnly("org.projectlombok:lombok")
annotationProcessor("org.projectlombok:lombok")
}
// 项目坐标
group = "com.laolang.shop"
version = "1.0"
// Apply a specific Java toolchain to ease working on different environments.
java {
toolchain {
languageVersion.set(JavaLanguageVersion.of(8))
}
}
application {
// 启动类
mainClass.set("com.laolang.shop.ShopApplication")
}
tasks.named<Test>("test") {
// Use TestNG for unit tests.
useTestNG()
}
tasks.withType<Test> {
this.testLogging {
this.showStandardStreams = true
}
}
// UTF-8 编码
tasks.withType<JavaCompile>{
options.encoding = "UTF-8"
}
// spring boot 插件打包
tasks.named<BootJar>("bootJar"){
// jar 包名称
archiveBaseName.set("boot-shop")
// 版本号设置为空
archiveVersion.set("")
}
运行
可以使用 gradle run 运行, 也可以使用spring boot 的插件的方式
app 为项目目录
gradle app:bootRun
打包
直接执行命令即可
app 为项目目录
gradle app:bootJar
也可以使用 idea