使用的idea版本2026.1.3,使用的gradle版本8.14.5
初始化项目
使用idea创建单模块项目之后,初始化的配置文件如下
settings.gradle
XML
rootProject.name = 'testgradle'
build.gradle
XML
plugins {
id 'java'
}
group = 'com.erbaoge'
version = '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
testImplementation platform('org.junit:junit-bom:5.10.0')
testImplementation 'org.junit.jupiter:junit-jupiter'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
}
test {
useJUnitPlatform()
}
创建模块
创建示例模块log 和 common


创建结束后两个子模块 项目结构中只有build.gradle文件。

修改跟项目中插件配置
之前plugin中只有一个插件java,将java进行修改为java-library,功能更丰富,且包含java插件,可以打依赖包,就是打包成jar包供其他项目使用。并且引入依赖可以使用api方式,实现传递依赖。
添加插件: org.springframework.boot springboot项目需要,实现打包springboot的可执行jar。
添加插件:io.spring.dependency-management实现依赖版本管理。项目中依赖Jar包的版本,在根项目的 build.gradle中配置,子项目中引入依赖不需要再写版本号。
依赖jar版本管理三部曲
参考:springboot项目使用Gradle工具实现依赖版本控制-CSDN博客
所有子模块中共有的依赖配置
参考:springboot项目使用Gradle工具实现依赖版本控制-CSDN博客
完整配置
项目结构定义好后的完整settings.gradle文件
Groovy
rootProject.name = 'SpringBootGradle'
include 'common'
include 'schedule'
include 'login'
include 'notice'
include 'BillPage'
include 'log'
include 'permission'
include 'config'
include 'file'
include 'BillStarter'
根项目中定义好插件,项目依赖版本控制,项目编译打包编码,完整的build.gradle文件。其中子模块都会依赖的包也写到这个文件中
Groovy
plugins {
id 'java'
id 'org.springframework.boot' version '2.7.13' apply false
id 'io.spring.dependency-management' version '1.1.7' apply false
}
allprojects {
group = 'com.erbaoge'
version = '1.0-SNAPSHOT'
repositories {
mavenLocal()
maven { url 'https://maven.aliyun.com/nexus/content/groups/public/' }
mavenCentral()
}
}
subprojects {
apply plugin: 'java'
apply plugin: "io.spring.dependency-management"
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
}
dependencyManagement {
imports {
mavenBom org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES
}
// 统一管理所有依赖版本
dependencies {
dependency "com.baomidou:mybatis-plus-boot-starter:3.5.12"
dependency "com.baomidou:mybatis-plus-generator:3.5.12"
dependency "com.alibaba:druid-spring-boot-starter:1.2.8"
dependency "com.github.pagehelper:pagehelper-spring-boot-starter:1.4.1"
dependency "com.github.ulisesbocchio:jasypt-spring-boot-starter:2.1.0"
dependency "com.alibaba:fastjson:1.2.83"
dependency "dom4j:dom4j:1.6.1"
dependency "com.google.zxing:core:3.3.0"
dependency "com.alibaba:easyexcel:3.3.4"
dependency "com.jcraft:jsch:0.1.54"
dependency "io.springfox:springfox-swagger2:3.0.0"
dependency "com.github.xiaoymin:knife4j-spring-ui:3.0.3"
dependency "com.auth0:java-jwt:3.10.3"
dependency "net.sourceforge.tess4j:tess4j:5.8.0"
}
}
dependencies {
// web 排除内置tomcat,改用undertow
implementation('org.springframework.boot:spring-boot-starter-web') {
exclude group: 'org.springframework.boot', module: 'spring-boot-starter-tomcat'
}
implementation 'org.springframework.boot:spring-boot-starter-undertow'
// 参数校验
implementation 'org.springframework.boot:spring-boot-starter-validation'
// mybatis-plus
implementation "com.baomidou:mybatis-plus-boot-starter"
// 分页插件
implementation "com.github.pagehelper:pagehelper-spring-boot-starter:"
// lombok
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
// swagger knife4j
implementation "io.springfox:springfox-swagger2"
implementation "com.github.xiaoymin:knife4j-spring-ui"
// 工具类
implementation 'org.apache.commons:commons-lang3'
}
}
common模块完整的build.gradle文件
Groovy
dependencies {
// web 排除内置tomcat,改用undertow
implementation('org.springframework.boot:spring-boot-starter-web') {
exclude group: 'org.springframework.boot', module: 'spring-boot-starter-tomcat'
}
implementation 'org.springframework.boot:spring-boot-starter-undertow'
// mybatis-plus
implementation "com.baomidou:mybatis-plus-boot-starter"
// 代码生成器
implementation "com.baomidou:mybatis-plus-generator"
implementation 'org.freemarker:freemarker'
// 阿里druid连接池
implementation "com.alibaba:druid-spring-boot-starter"
// mysql驱动 runtime范围
runtimeOnly 'com.mysql:mysql-connector-j'
// 参数校验
implementation 'org.springframework.boot:spring-boot-starter-validation'
// 分页插件
implementation "com.github.pagehelper:pagehelper-spring-boot-starter:"
// 邮件
implementation 'org.springframework.boot:spring-boot-starter-mail'
// swagger knife4j
implementation "io.springfox:springfox-swagger2"
implementation "com.github.xiaoymin:knife4j-spring-ui"
// easyexcel excel导入导出
implementation "com.alibaba:easyexcel"
// 二维码条形码
implementation "com.google.zxing:core"
// sftp
implementation "com.jcraft:jsch"
// httpclient
implementation 'org.apache.httpcomponents:httpclient'
// xml dom4j
implementation "dom4j:dom4j"
// jwt token
implementation "com.auth0:java-jwt"
// fastjson
implementation "com.alibaba:fastjson"
// 工具类
implementation 'org.apache.commons:commons-lang3'
// lombok
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
// 配置加密 jasypt
implementation "com.github.ulisesbocchio:jasypt-spring-boot-starter"
// 测试依赖
testImplementation 'junit:junit'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
login模块完整的build.gradle文件
Groovy
dependencies {
implementation project(':common')
// jwt token
implementation "com.auth0:java-jwt"
}
其他模块类似......启动模块应用springboot插件,并配置依赖包打包到启动包外部依赖模式,完整的build.gradle文件
Groovy
apply plugin:'org.springframework.boot'
dependencies {
implementation project(':common')
implementation project (':schedule')
implementation project (':login')
implementation project (':notice')
implementation project (':BillPage')
implementation project (':log')
implementation project (':permission')
implementation project (':config')
implementation project (':file')
// 阿里druid连接池
implementation "com.alibaba:druid-spring-boot-starter"
// mysql驱动 runtime范围
runtimeOnly 'com.mysql:mysql-connector-j'
// easyexcel excel导入导出
implementation "com.alibaba:easyexcel"
// 二维码条形码
implementation "com.google.zxing:core"
// sftp
implementation "com.jcraft:jsch"
// httpclient
implementation 'org.apache.httpcomponents:httpclient'
// xml dom4j
implementation "dom4j:dom4j"
// fastjson
implementation "com.alibaba:fastjson"
// lombok
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
// 配置加密 jasypt
implementation "com.github.ulisesbocchio:jasypt-spring-boot-starter"
implementation 'net.sourceforge.tess4j:tess4j'
// 测试依赖
testImplementation 'junit:junit'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
// 配置 bootJar,将依赖分离出来
bootJar {
// 禁用默认的 fat jar 打包方式
enabled = false
archiveFileName.set('bill.jar')
}
// 启用普通的 jar 打包
jar {
archiveFileName.set('bill.jar')
enabled = true
exclude '*.yml', '*.yaml', 'logback-spring.xml'
manifest {
attributes (
'Main-Class': 'com.erbaoge.bill.ErbaogeApplication',
'Class-Path': configurations.runtimeClasspath.files.collect { "lib/$it.name" }.join(' '))
}
}
// 创建复制依赖的任务
tasks.register('copyDependencies', Copy) {
from configurations.runtimeClasspath
into 'build/libs/lib'
}
tasks.register('copyConfig', Copy) {
from 'src/main/resources'
include '*.yml', '*.yaml', 'logback-spring.xml'
into 'build/libs/config'
}
// 让 build 任务依赖 copyDependencies和 copyConfig
build.dependsOn copyDependencies, copyConfig