美文网首页
springboot+gradle 多模块项目(实战派)

springboot+gradle 多模块项目(实战派)

作者: 为爱放弃一切 | 来源:发表于2019-12-24 15:46 被阅读0次

    最近公司要上新项目,技术栈:springboot、springcloud,项目管理工具:gradle,架构模式:分布式。这里我们纯手(不含项目创建)工搭建,方便给不熟悉gradle的同学一个参考。springboot使用2.1.9版本,gradle使用5.2.1版本。

    项目建立

    https://start.spring.io网站上创建项目,我一般都在该网站创建项目,个人习惯。我在该网站上共创建了7项目,分别是eureka(微服务注册中心)、iov-api(移动端后台服务)、iov-core(公共模块)、iov-manager(pc端服务后台)、iov-gateway(网关)、iov-task(定时作业)、iov-data-process(数据处理服务)。项目建好之后,另外建立一个文件夹,比如命名为iov,在该文件下建立两个文件,一个是build.gradle文件,一个是settings.gradle文件。建好之后将刚才下载的项目复制到该文件下,在一一修改配置文件即可。这是一个完整的分布式项目,下面看下截图,更直观一些。

    zhengti.jpg

    配置文件修改

    1、主项目文件修改

    build文件内容如下:

    plugins {
    
        id 'org.springframework.boot' version'2.1.9.RELEASE'
    
        id 'java'
    
    }
    
    repositories {
        maven { url 'http://maven.aliyun.com/nexus/content/groups/public' }
        mavenCentral()
    
    }
    
    // subprojects : 所有子模块共享的配置
    subprojects{
    
        apply plugin:'java'
    
        apply plugin:'idea'
    
        apply plugin:'eclipse'
    
        apply plugin:'jacoco'
        //使用springboot插件
        apply plugin:'org.springframework.boot'
        //版本管理插件
        apply plugin:'io.spring.dependency-management'
    
        group ='pro.tscar.sgcc.ecp'
        // JVM 版本号要求
        sourceCompatibility =1.8
    
        targetCompatibility =1.8
    
        version = '3.0-SNAPSHOT'
    
        // java编译的时候缺省状态下会因为中文字符而失败
        [compileJava,compileTestJava,javadoc]*.options*.encoding ='UTF-8'
    
        bootJar {
    
            enabled =false  // 默认不需要打可执行jar包
    
        }
        // 配置所有子模块的依赖仓库地址
        repositories {
            maven { url 'http://maven.aliyun.com/nexus/content/groups/public' }
            maven { url'https://repo.spring.io/milestone' }
            mavenCentral()
    
        }
        //所有子模块共有依赖
        dependencies {
    
            testImplementation 'org.springframework.boot:spring-boot-starter-test'
        }
    
    }
    
    //所有子项目都依赖iov-core包排除iov-core项目本身
    configure(subprojects.findAll({ it.name != 'iov-core' })) {
        dependencies {
            compile project(":iov-core")
        }
    }
    

    settings.gradle文件内容如下:

    这里配置项目名称,并导入子项目
    rootProject.name = 'iov'
    include 'iov-core'
    include 'iov-manager'
    include 'iov-gateway'
    include 'iov-api'
    include 'iov-task'
    include 'iov-data-process'
    include 'eureka'
    

    2、子项目文件修改

    删除子项目中的settings.gradle文件,只保留build文件即可,其它不变。

    iov-core配置介绍

    iov-core作为公共模块,有些特殊,所以先介绍该子项目,下面先看下项目结构。


    core.jpg

    除了删除settings.gradle文件之外,这里我们删了springboot启动类,此时iov-core就是一个纯java项目了。
    下面我们看下iov-core的gradle配置:

    
    jar {
        enabled = true
    }
    
    bootJar {
        enabled = false  // 默认不需要打可执行jar包
    }
    
    
    dependencies {
    
        compile 'org.springframework.boot:spring-boot-starter-data-jdbc'
        compile 'org.springframework.boot:spring-boot-starter-data-jpa'
        compile 'org.springframework.boot:spring-boot-starter-data-redis'
        compile 'org.springframework.boot:spring-boot-starter'
        compile 'org.springframework.boot:spring-boot-starter-logging'
        compile 'org.springframework.boot:spring-boot-starter-aop'
        compile 'com.vladmihalcea:hibernate-types-5:1.2.0'
        compile 'mysql:mysql-connector-java:5.1.47'
    
        compile 'org.apache.commons:commons-lang3:3.8.1'
        compile 'commons-codec:commons-codec:1.12'
        compile 'org.apache.commons:commons-collections4:4.3'
        compile 'commons-io:commons-io:2.6'
        compile 'joda-time:joda-time:2.10.1'
        compile 'commons-collections:commons-collections:3.2.1'
        compile 'commons-beanutils:commons-beanutils:1.9.2'
        compile 'org.apache.httpcomponents:httpclient:4.5.6'
        compile 'org.apache.poi:poi:4.1.0'
        
        compile group: 'com.google.guava', name: 'guava', version: '23.0'
        compile group: 'com.google.code.gson', name: 'gson', version: '2.8.5'
        compile 'javax.validation:validation-api:2.0.1.Final'
        compile 'org.bouncycastle:bcprov-jdk15on:1.60'
        
        compile 'ch.qos.logback:logback-classic:1.2.3'
        compile 'org.slf4j:jul-to-slf4j:1.7.25'
        compile 'org.slf4j:log4j-over-slf4j:1.7.25'
        compile 'javax.mail:mail:1.4.7'
    
        compile 'com.alibaba:druid:1.1.12'
        compile 'com.alibaba:fastjson:1.2.60'
        implementation 'org.springframework.kafka:spring-kafka:2.2.0.RELEASE'
        implementation 'org.apache.kafka:kafka-clients:2.0.0'
        
        compile 'org.apache.curator:curator-framework:2.12.0'
        compile 'org.apache.curator:curator-recipes:2.12.0'
        
        compile 'redis.clients:jedis:3.1.0'
        
        compile 'io.springfox:springfox-swagger2:2.8.0'
        compile 'io.springfox:springfox-swagger-ui:2.8.0'
    
    }
    
    这里注意compile 和 implementation 的区别,如果引入的jar仅仅在该项目中使用,则使用implementation 关键字,否则使用compile 。
    

    iov-core配置完成,已可以作为jar包供其它子项目使用。

    其它子项目配置介绍

    这里我们只介绍一例即可,其它都一样。我们就一iov-task为例介绍,下面看下项目结构。


    task.jpg

    这里就比较简单了,我们直接看下iov-task的gradle配置文件:

    
    jar {
        enabled = true
    }
    
    bootJar {
        enabled = true
    }
    
    dependencies {
    
        implementation 'org.springframework.boot:spring-boot-starter-web'
    
    
    }
    

    是的,iov-task的配置就是这么简单。

    总结

    至此,我们基于springboot、gradle构建的多模块项目已经构建完成。我个人比较喜欢gradle,使用gradle很少遇到,至少我使用以来从未遇到过jar包冲突的情况,gradle会自动帮你选择版本,这是gradle的一大优势,详细的大家可以自己去找资料,这里不再解释。

    相关文章

      网友评论

          本文标题:springboot+gradle 多模块项目(实战派)

          本文链接:https://www.haomeiwen.com/subject/knffoctx.html