springboot项目使用Gradle工具实现依赖版本控制

需要引入插件:

Groovy 复制代码
io.spring.dependency-management

具体使用参考文档:Dependency Management Plugin

build.gralde配置具体变更,plugins中需要添加插件:io.spring.dependency-management,因为是springboot项目还需要声明插件:org.springframework.boot。

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
}

上述三个插件,其中Java插件跟项目以及所有子模块都需要应用,org.springframework.boot。插件只需要启动模块应用,dependency management插件所有子模块需要应用。因为另外两个插件,在根项目中不应用插件,所以apply写false。在 build.gradle文件中需要添加如下配置:

Groovy 复制代码
subprojects {
    apply plugin: 'java'
    apply plugin: "io.spring.dependency-management"
}

在build.gradle文件中,subprojects标签下需要添加dependency management,并引入org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES。以及添加下spring中没有管理到的jar包。

Groovy 复制代码
    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"
        }
    }

如果项目中所有子模块都需要引入一些炸包,可以统一在根项目的build.gradle文件中设置。在subprojects标签下添加

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'
        // 参数校验
        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'
    }

子模块中只需要引入自己需要jar。在子模块的build.gradle文件中(这就是完整内容)

Groovy 复制代码
dependencies {
    // 模块依赖
    implementation project(':common')

    // jwt token
    implementation "com.auth0:java-jwt"
    
}

再添加下相关编码设置后,根模块下的完整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'
    }
}

启动模块创建启动类后,是能直接启动的,但打包并部署springboot启动包。需要在启动模块应用插件,在启动模块的build.gradle文件中添加

Groovy 复制代码
apply plugin:'org.springframework.boot'

引入相关依赖后,启动模块完整的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'
}
相关推荐
2601_962071572 小时前
【Java报错已解决】org.springframework.beans.factory.BeanCreationException
java·开发语言
zww89491112 小时前
酒馆预约系统开发实战:从需求分析到上线全流程指南
java·eclipse
东风破_4 小时前
从跨域到 WebSocket:前端跨域方案、SSE 与双向实时通信详解
前端·后端
zww89491115 小时前
匿名树洞系统开发实战:从需求分析到部署指南
java·eclipse
行百里er5 小时前
Gateway 上别硬塞 MVC:DeferredImportSelector 看菜下锅
spring boot·后端·开源
名字还没想好☜5 小时前
Java 遍历时删元素抛 ConcurrentModificationException:fail-fast 原理与三种正确删法
后端
IT_陈寒6 小时前
React Hooks闭包陷阱差点让我加班到凌晨
前端·人工智能·后端
黑马程序员毕设6 小时前
基于Java的医院药品管理系统的优化设计与实现
java·开发语言·spring boot·小程序·架构·课程设计·毕设
木井巳6 小时前
【BFS/DFS 解决 FloodFill 算法】太平洋大西洋水流问题
java·算法·leetcode·深度优先·广度优先·宽度优先·推荐算法
码事漫谈6 小时前
本体论,彻底懂
后端