gradle安装、配置,使用命令构建spring boot项目
- 安装
# 下载到本地直接解压
# 配置环境变量
GRADLE_HOME
GRADLE_USER_HOME
PATH
# 打开cmd:gradle -v
-
idea配置
-
打包
-
常用命令
# cmd进入项目根路径
gradlew build
gradlew clean
gradle build
gradle clean
gradle clean build
- 使用命令构建gradle项目
# 参考:https://blog.csdn.net/nece001/article/details/106428343
# 新建一个文件夹作为项目根目录,输入gradle init
# 跨子项目 + 新API
C:\Users\ychen224\OneDrive - DXC Production\Desktop\demo01>gradle init # 初始化一个项目
Select type of project to generate:
1: basic
2: application
3: library
4: Gradle plugin
Enter selection (default: basic) [1..4] 2 # 项目类型
Select implementation language:
1: C++
2: Groovy
3: Java
4: Kotlin
5: Scala
6: Swift
Enter selection (default: Java) [1..6] 3 # 项目语言
# 是否跨多个子项目拆分功能
Split functionality across multiple subprojects?:
1: no - only one application project
2: yes - application and library projects
Enter selection (default: no - only one application project) [1..2] 2
Select build script DSL:
1: Groovy
2: Kotlin
Enter selection (default: Groovy) [1..2] 1 # 构建脚本
# 是否使用新的API构建
Generate build using new APIs and behavior (some features may change in the next minor release)? (default: no) [yes, no] yes
Project name (default: demo01): demo01 # 项目名
Source package (default: demo01): io.github.chnq # 包名
> Task :init
Get more help with your project: https://docs.gradle.org/7.3.3/samples/sample_building_java_applications_multi_project.html
BUILD SUCCESSFUL in 1m 32s
2 actionable tasks: 2 executed
# 跨子项目 + 不使用新的API
Split functionality across multiple subprojects?:
1: no - only one application project
2: yes - application and library projects
Enter selection (default: no - only one application project) [1..2] 2
Generate build using new APIs and behavior (some features may change in the next minor release)? (default: no) [yes, no] no
# 不跨子项目 + 使用新的API
Split functionality across multiple subprojects?:
1: no - only one application project
2: yes - application and library projects
Enter selection (default: no - only one application project) [1..2] 1
Generate build using new APIs and behavior (some features may change in the next minor release)? (default: no) [yes, no] yes
# 不跨子项目 + 不使用新的API
Split functionality across multiple subprojects?:
1: no - only one application project
2: yes - application and library projects
Enter selection (default: no - only one application project) [1..2] 1
Generate build using new APIs and behavior (some features may change in the next minor release)? (default: no) [yes, no] no
- 常用配置
gradle配置
jdk
字符集
注解生效激活
过滤编译文件:添加类型,enter,apply
.gradle
.gitattributes
gradlew
gradlew.bat
- 热部署
# build.gradle
spring-boot-devtools
# yml
spring:
devtools:
restart:
enabled: true
-
ctrl + shift + alt + /
-
spring boot模板
# lombok报错改为使用如下方式引入
implementation 'org.projectlombok:lombok:1.18.8'
annotationProcessor 'org.projectlombok:lombok:1.18.8'
# 不跨子项目 + 使用新的API
# 导入所需依赖
dependencies {
//
implementation 'com.google.guava:guava:30.1.1-jre'
// spring-boot-starter-web
implementation group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: '2.6.1'
// spring-boot-devtools
implementation group: 'org.springframework.boot', name: 'spring-boot-devtools', version: '2.6.1'
// lombok
compileOnly group: 'org.projectlombok', name: 'lombok', version: '1.18.20'
// spring-boot-starter-test
testImplementation group: 'org.springframework.boot', name: 'spring-boot-starter-test', version: '2.6.1'
// spring-boot-configuration-processor
implementation group: 'org.springframework.boot', name: 'spring-boot-configuration-processor', version: '2.6.1'
}
# 配置yml
server:
port: 8080
error:
whitelabel:
enabled: true
spring:
application:
name: demo01
messages:
basename: messages
encoding: UTF-8
devtools:
restart:
enabled: true
logging:
level:
com:
example: DEBUG
org:
springframework:
security: DEBUG
pattern:
console: '%clr(%d{E HH:mm:ss.SSS}){blue} %clr(%-5p) %clr(${PID}){faint} %clr(---){faint}
%clr([%8.15t]){cyan} %clr(%-40.40logger{0}){blue} %clr(:){red} %clr(%m){faint}%n'
# 编写接口
@RestController
public class TestController {
@RequestMapping("/test")
public String test(){
return "hello";
}
}
# 编写一个index.html
# 启动项目测试
http://localhost:8080/test
http://localhost:8080/index.html
- error
# 打包后执行java -jar app.jar
# 报错:没有主清单属性
# 参考:https://www.cnblogs.com/shaosks/p/9984350.html
# 再次运行报错
Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/boot/SpringApplication
at io.github.ychen.App.main(App.java:13)
Caused by: java.lang.ClassNotFoundException: org.springframework.boot.SpringApplication
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:582)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
... 1 more
# 错误原因:依赖没有被打包到springboot项目的jar包中
- 打成war包部署
# 参考:https://blog.csdn.net/kingboyworld/article/details/75244769
# build.gradle中添加
apply plugin: 'war'
# 修改启动类
@SpringBootApplication
public class App extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(App.class);
}
}
# 控制台输入命令打包
gradle build
# 将war包上传到服务器
# 拉取tomcat镜像
docker pull chniny/tomcat:9
docker run -d -p 8080:8080 chniny/tomcat:9
# 将war复制到tomcat容器
docker cp app.war container_ID:/usr/local/tomcat/webapps
- 编译项目时乱码导致报错
# Help > Edit Custom VM Options
# 添加一行,重启idea
-Dfile.encoding=UTF-8
- 编译时报错
# 安装lombok插件
# 启用annotation processing
# 更改compiler,将eclipse更改为javac(settings > build > compiler > Java compiler > use compiler)
# 检查类中有没有添加注解@Slf4j
# build.gradle中修改lombok依赖
implementation 'org.projectlombok:lombok:1.18.8'
annotationProcessor 'org.projectlombok:lombok:1.18.8'
- 配置junit5
testing {
suites {
// Configure the built-in test suite
// test {
// // Use JUnit4 test framework
// useJUnit('4.13.2')
// }
test {
// Use JUnit4 test framework
useJUnit('5.13.2')
}
}
}
// 使用junit5
testImplementation("org.junit.jupiter:junit-jupiter-api:5.1.0")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.1.0")