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'
}
相关推荐
卷无止境3 小时前
模块与包:Python 代码组织的两层逻辑
后端·python
我是唐青枫4 小时前
Java Jetty 实战详解:从嵌入式 HTTP 服务到 Spring Boot 容器替换
java
带刺的坐椅4 小时前
当所有人都在用 TS/Python 写 Agent,我们为什么坚持 Java
java·ai·solon·codex·opencode·soloncode
明月_清风4 小时前
🛡️ Web3 安全入门:新手防骗完全指南
后端·web3
小王师傅664 小时前
英语学习记
java·学习
明月_清风4 小时前
🔐 Solidity 完全指南:关键语法解析与智能合约中的核心作用
后端·web3
geovindu4 小时前
CSharp: Iterative Algorithms
开发语言·后端·算法·c#·.net·迭代算法
早点睡啊Y4 小时前
深入学LangChain官方文档:Observability 与 Studio——先看清 Agent 到底做了什么
java·数据库·langchain
雨落在了我的手上4 小时前
Java数据结构(六):链表的介绍
java·开发语言·数据结构
雪隐5 小时前
AI股票小助手10-我用代码管住自己的手:一个普通人的市场观察笔记
前端·人工智能·后端