创建Gradle多模块SpringBoot项目

使用的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
相关推荐
猿究院_夏燊10 小时前
Spring Bean 生命周期
java·后端·spring
Wang's Blog11 小时前
Java 项目实战: 交友项目:登录链路回顾与今日任务地图
java·开发语言·状态模式·交友
信誓旦旦的程序猿11 小时前
【Python量化实战宝典 #08】可转债与债券接口手册:转股价·溢价率·纯债价值(Python 可跑)
java·python·股票数据api·股票数据api接口·股票api数据接口·股票量化数据api·股票量化数据接口
砚底藏山河11 小时前
python量化:行情数据回溯
java·开发语言·python·金融·maven
Jlzn888812 小时前
华东电芯分选通道数怎么配才不堵线
java·人工智能·架构·自动化·制造
香吧香12 小时前
GRPC连接机制---长连接分析
java·网络与传输协议
CCblandy12 小时前
雪花 ID 四个边界实测:时钟回拨重号 500 个,前端 JSON.parse 直接丢精度
后端·python·算法·性能优化·架构
我是人✓12 小时前
SpringBoot项目的五种创建方式
java·spring boot·后端
Wang's Blog12 小时前
Java 项目实战: 交友项目 - 阿里云 OSS 入门案例:从创建 Bucket 到上传拿到可访问 URL
java·阿里云·交友
裕晟资质规划13 小时前
三类涉密资质延续时限差异分析:军工60日与集成印制3个月的边界
java·服务器·前端·网络·安全