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'
}
相关推荐
范什么特西1 分钟前
亲和路由(inspect)
java
她说可以呀3 分钟前
Docker 镜像的两种创建方式
java·分布式·docker
霸道流氓气质13 分钟前
AgentFlow 入门与实战:Task-Based 并发、EWMA 动态调度与四道门禁的 Java 生产级编排
java·开发语言
UrbanJazzerati23 分钟前
AutoHotkey v2 零基础快速入门 Part 3 像素亮度检测完全指南
后端
浮链序24 分钟前
Shopify 把 10 亿商品开放给任意 agent 了:我用零依赖 Python 撸了个带预算闸门的代购网关
后端·代码规范
愿得一猪蹄24 分钟前
# Java线程池实战,别再new Thread了
java
ee又momo33 分钟前
# 深入理解Java注解,从使用到自定义
java
Qlly35 分钟前
全栈工程师怎么接需求?从一句「加个功能」聊到能开发、能验收
前端·后端
马可家的菠萝39 分钟前
你的微信“文件传输助手”,是不是已经成了第二个网盘?
前端·后端·架构
花涧鹤1 小时前
学完循环我才发现,我写的不是死循环,是死机
后端