先说一下初衷把:为啥突然要用Gradle,没办法公司领导说要用,后期自研项目都使用Gradle构建......
1、下载安装
这个就不说了,网上大家搜索一下,配置一下环境变量即可
2、Groovy的一些语法Gradle任务、文件操作等等,大家感兴趣可以去看看
// TODO 后期补上来
3、使用Gradle创建SpringBoot工程
首先,Gradle中的依赖方式分为三种:
- 本地依赖:依赖本地的某一个jar包,具体的可通过文件集合、文件树的方式指定
- 模块依赖:依赖某个project
- 直接依赖:依赖的类型 依赖的组名 依赖的名称 依赖的版本号
dependencies {
// 本地依赖
implementation files('lib/mysql.jar','lib/junit.jar') // 依赖本地项目下的lib包中的jar文件
implementation fileTree('dir':'lib',includes: ['xxx.jar'],excludes: ['xxx.jar']) // 指定包含某些jar和排除依赖
// 项目依赖
implementation project(':项目的名称')
// 直接依赖
implementation 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
implementation group:'org.junit.jupiter',name:'junit-jupiter-engine',version:'5.8.1' // 完整写法
}
其次,Gradle依赖配置有5种方式
- 1、implementation:会将指定的依赖添加到编译路径,并且会将该依赖打包到输出,如jar中,但是这个依赖在编译时不能暴露给其他模块。一句话:依赖不会传递
- 2、api:使用api配置的依赖会将对应的依赖添加到编译路径,并将依赖打包输出,但是这个依赖是可以传递的,比如模块A依赖模块B,B依赖库C,模块B在编译时能够访问到库C
注意:api 与 implementation 区别 ===> api 来定义依赖信息是可以进行依赖传递的,而implementation则不会
- 3、compileOnly:compileOnly修饰的依赖会添加到编译路径中,但是不会打包到jar中,因此只能在编译时访问,且compileOnly修饰的依赖不会传递。
- 4、runtimeOnly:与compileOnly相反,它修饰的依赖不会添加到编译路径中,但是被打包到jar中,运行时使用。
- 5、annotationProcessor:这个我也没整明白
1、创建一个工程
2、配置一下IDEA的Gradle构建工具,他默认使用的是wrapper下的gradle。我们改为自己本地安装的
改为本地安装的Gradle
3、在build.gradle文件中加入依赖
plugins {
id 'java'
id 'org.springframework.boot' version '2.7.3' // 定义SpringBoot版本,不单独使用
id 'io.spring.dependency-management' version '1.1.0'
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web' //省略版本,原生bom支持,插件management提供
......
}
4、测试一下
4、使用Gradle创建SSM多模块工程
1、创建父工程
注意:记得把gradle修改为自己本地安装的gradle
2、一次创建几个子工程
gradle-ssm-api:前台门户端
gradle-ssm-admin:后台管理端
gradle-ssm-service:业务处理层
gradle-ssm-mapper:持久层访问数据库
gradle-ssm-domain:实体类VO,BO,DTO,POJO......
..... 创建过程我就省略了
最终效果:
项目依赖关系:
3、我们先检查gradle-ssm-parent工程的settings.gradle
rootProject.name = 'gradle-ssm-parent'
include 'gradle-ssm-api'
include 'gradle-ssm-admin'
include 'gradle-ssm-service'
include 'gradle-ssm-mapper'
include 'gradle-ssm-domain'
4、再修改gradle-ssm-parent工程的build.gradle
plugins {
id 'java'
}
group 'com.qbb'
version '1.0-SNAPSHOT'
// 指定仓库位置
repositories {
mavenLocal()
mavenCentral()
}
test {
useJUnitPlatform()
}
// 指定子工程的共性信息
subprojects {
//添加插件
apply plugin: 'java'
apply plugin: 'java-library'//支持api
//编码环境jdk版本
sourceCompatibility = 17
//编译时jdk版本
targetCompatibility = 17
group 'com.qbb'
version '1.0-SNAPSHOT'
/*指定一下字符集,不然打包时有可能会报错*/
//业务编码字符集,注意这是指定源码解码的字符集[编译器]
compileJava.options.encoding "UTF-8"
//测试编码字符集,注意这是指定源码解码的字符集[编译器]
compileTestJava.options.encoding "UTF-8"
//编译JAVA文件时采用UTF-8:注意这是指定源码编码的字符集【源文件】
tasks.withType(JavaCompile) {
options.encoding = "UTF-8"
}
//编译JAVA文件时采用UTF-8:注意这是指定文档编码的字符集【源文件】
tasks.withType(Javadoc) {
options.encoding = "UTF-8"
}
//配置外部属性
ext {
spring_version = "5.2.5.RELEASE"
}
//依赖的配置
dependencies {
//lombok
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
implementation 'log4j:log4j:1.2.17'
//implementation 'org.slf4j:slf4j-api:1.7.25'
}
test {
useJUnitPlatform()
}
}
project("gradle-ssm-domain") {
dependencies {
// implementation 'org.projectlombok:lombok:1.18.24'
// 这里使用api的目的就是为了让lombok依赖可以进行传递,上面我们也说过api和implementation的区别
api 'org.projectlombok:lombok:1.18.24'
}
}
project("gradle-ssm-mapper") {
dependencies {
api project(':gradle-ssm-domain')
api 'org.mybatis:mybatis-spring:2.0.6'
api 'com.alibaba:druid:1.2.8'
api 'org.mybatis:mybatis:3.5.8'
api 'mysql:mysql-connector-java:8.0.30'
}
}
project("gradle-ssm-service") {
dependencies {
api project(':gradle-ssm-mapper')
api 'org.springframework:spring-web:5.2.5.RELEASE'
api 'org.springframework:spring-test:5.2.5.RELEASE'
api 'org.springframework:spring-context:5.2.5.RELEASE'
api 'org.springframework:spring-jdbc:5.2.5.RELEASE'
api 'org.aspectj:aspectjweaver:1.9.8'
}
}
project("gradle-ssm-admin") {
apply plugin: 'war' // 打成war宝
dependencies {
implementation project(':gradle-ssm-service')
implementation 'org.springframework:spring-webmvc:5.2.5.RELEASE'
implementation 'com.fasterxml.jackson.core:jackson-databind:2.12.3'
implementation "com.fasterxml.jackson.core:jackson-annotations:2.12.3"
implementation "com.fasterxml.jackson.core:jackson-core:2.12.3"
compileOnly 'javax.servlet:javax.servlet-api:4.0.1'
implementation 'jstl:jstl:1.2'
}
}
project("gradle-ssm-api") {
apply plugin: 'war'
dependencies {
//implementation project(':gradle-ssm-domain')
implementation project(':gradle-ssm-service')
implementation 'org.springframework:spring-webmvc:5.2.5.RELEASE'
implementation "com.fasterxml.jackson.core:jackson-databind:2.12.3"
implementation "com.fasterxml.jackson.core:jackson-annotations:2.12.3"
implementation "com.fasterxml.jackson.core:jackson-core:2.12.3"
implementation 'jstl:jstl:1.2'
}
}
5、编写测试类进行测试 (代码比较简单,我直接放在后面的代码仓库了)
clear -> build -> 配置本地的tomcat
注意:我这里用的是JDK17所以相关的版本也是试错过得,大家JDK如果不是17,需要注意Spring的版本。不然构建会报错的
Unsupported class file major version 61
到这本以为全部ok了,但是有一个大坑......我替大家踩了!我在发请求的时候后台查询数据库没问题,但是前台一致报错500...
解决办法:参考Gradle打包lombok不生效
6、看着好像是完成了,但是还不够还!这个日期时间我们变成了时间戳,我不喜欢
数据库中的:
我修改了一下build.gradle依赖信息
plugins {
id 'java'
}
group 'com.qbb'
version '1.0-SNAPSHOT'
// 指定仓库位置
repositories {
mavenLocal()
mavenCentral()
}
test {
useJUnitPlatform()
}
// 指定子工程的共性信息
subprojects {
//添加插件
apply plugin: 'java'
apply plugin: 'java-library'//支持api
//编码环境jdk版本
sourceCompatibility = 17
//编译时jdk版本
targetCompatibility = 17
group 'com.qbb'
version '1.0-SNAPSHOT'
/*指定一下字符集,不然打包时有可能会报错*/
//业务编码字符集,注意这是指定源码解码的字符集[编译器]
compileJava.options.encoding "UTF-8"
//测试编码字符集,注意这是指定源码解码的字符集[编译器]
compileTestJava.options.encoding "UTF-8"
//编译JAVA文件时采用UTF-8:注意这是指定源码编码的字符集【源文件】
tasks.withType(JavaCompile) {
options.encoding = "UTF-8"
}
//编译JAVA文件时采用UTF-8:注意这是指定文档编码的字符集【源文件】
tasks.withType(Javadoc) {
options.encoding = "UTF-8"
}
//配置外部属性
ext {
spring_version = "5.3.22"
}
//依赖的配置
dependencies {
//lombok
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
implementation 'log4j:log4j:1.2.17'
//implementation 'org.slf4j:slf4j-api:1.7.25'
}
test {
useJUnitPlatform()
}
}
project("gradle-ssm-domain") {
dependencies {
// implementation 'org.projectlombok:lombok:1.18.24'
// 这里使用api的目的就是为了让lombok依赖可以进行传递,上面我们也说过api和implementation的区别
// api 'org.projectlombok:lombok:1.18.24'
// 解决Gradle打包后lombok不生效
api 'org.projectlombok:lombok:1.18.24'
annotationProcessor 'org.projectlombok:lombok:1.18.24'
// 在domain层加入,后续依赖domain的模块可继续使用
api 'com.fasterxml.jackson.core:jackson-databind:2.12.3'
api "com.fasterxml.jackson.core:jackson-annotations:2.12.3"
api "com.fasterxml.jackson.core:jackson-core:2.12.3"
}
}
project("gradle-ssm-mapper") {
dependencies {
api project(':gradle-ssm-domain')
api 'org.mybatis:mybatis-spring:2.0.6'
api 'com.alibaba:druid:1.2.8'
api 'org.mybatis:mybatis:3.5.8'
api 'mysql:mysql-connector-java:8.0.30'
}
}
project("gradle-ssm-service") {
dependencies {
api project(':gradle-ssm-mapper')
api 'org.springframework:spring-web:5.3.22'
api 'org.springframework:spring-test:5.3.22'
api 'org.springframework:spring-context:5.3.22'
api 'org.springframework:spring-jdbc:5.3.22'
api 'org.aspectj:aspectjweaver:1.9.8'
}
}
project("gradle-ssm-admin") {
apply plugin: 'war' // 打成war宝
dependencies {
implementation project(':gradle-ssm-service')
implementation 'org.springframework:spring-webmvc:5.3.22'
// implementation 'com.fasterxml.jackson.core:jackson-databind:2.12.3'
// implementation "com.fasterxml.jackson.core:jackson-annotations:2.12.3"
// implementation "com.fasterxml.jackson.core:jackson-core:2.12.3"
compileOnly 'javax.servlet:javax.servlet-api:4.0.1'
implementation 'jstl:jstl:1.2'
}
}
project("gradle-ssm-api") {
apply plugin: 'war'
dependencies {
//implementation project(':gradle-ssm-domain')
implementation project(':gradle-ssm-service')
implementation 'org.springframework:spring-webmvc:5.3.22'
// implementation "com.fasterxml.jackson.core:jackson-databind:2.12.3"
// implementation "com.fasterxml.jackson.core:jackson-annotations:2.12.3"
// implementation "com.fasterxml.jackson.core:jackson-core:2.12.3"
implementation 'jstl:jstl:1.2'
}
}
在实体类上序列化一下
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime createTime;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime updateTime;
本以为大功告成,结果又报了这个错
03-Dec-2022 17:59:42.295 涓ラ噸 [http-nio-8080-exec-4] org.apache.catalina.core.StandardWrapperValve.invoke 鍦ㄨ矾寰勪负/ssm鐨勪笂涓嬫枃涓紝Servlet[DispatcherServlet]鐨凷ervlet.service锛堬級寮曞彂浜嗗叿鏈夋牴鏈師鍥犵殑寮傚父Request processing failed; nested exception is org.springframework.http.converter.HttpMessageConversionException: Type definition error: [simple type, class java.time.LocalDateTime]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Java 8 date/time type `java.time.LocalDateTime` not supported by default: add Module "com.fasterxml.jackson.datatype:jackson-datatype-jsr310" to enable handling (through reference chain: java.util.ArrayList[0]->com.qbb.User["createTime"])
com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Java 8 date/time type `java.time.LocalDateTime` not supported by default: add Module "com.fasterxml.jackson.datatype:jackson-datatype-jsr310" to enable handling (through reference chain: java.util.ArrayList[0]->com.qbb.User["createTime"])
at com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:77)
at com.fasterxml.jackson.databind.SerializerProvider.reportBadDefinition(SerializerProvider.java:1276)
at com.fasterxml.jackson.databind.ser.impl.UnsupportedTypeSerializer.serialize(UnsupportedTypeSerializer.java:35)
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:728)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:770)
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:178)
at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serializeContents(CollectionSerializer.java:145)
at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:107)
at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:25)
at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider._serialize(DefaultSerializerProvider.java:480)
at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:400)
at com.fasterxml.jackson.databind.ObjectWriter$Prefetch.serialize(ObjectWriter.java:1510)
at com.fasterxml.jackson.databind.ObjectWriter.writeValue(ObjectWriter.java:1006)
at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.writeInternal(AbstractJackson2HttpMessageConverter.java:456)
at org.springframework.http.converter.AbstractGenericHttpMessageConverter.write(AbstractGenericHttpMessageConverter.java:104)
at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:290)
at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.handleReturnValue(RequestResponseBodyMethodProcessor.java:183)
at org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.java:78)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:135)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:808)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1070)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:963)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:670)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:779)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:227)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:94)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:177)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:135)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:687)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:360)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:399)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:891)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1784)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191)
at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.base/java.lang.Thread.run(Thread.java:833)
解决办法:加入一个依赖就好了
project("gradle-ssm-domain") {
dependencies {
......
// 解决 Java 8 date/time type `java.time.LocalDateTime` not supported
implementation 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.14.0'
......
}
}
至此Gradle构建SSM多模块项目完成