gradle+springboot多模块构建demo

说明

多模块构建项目,版本说明

  • gradle版本:6.3
  • springboot版本:2.1.14
  • jdk 1.8
  • 开发工具:intellij

项目结构

demo中分为viewservicerepositorycommon一共4个子模块,外加一个父模块 gradleMultiModuleDemo(intellij中称为project)。
以下“–> ”表示依赖:
view --> service --> repository --> common
贴上一个图应该更明显:
在这里插入图片描述

gradle多模块构建代码

父模块的 settings.gradle

rootProject.name = 'gradleMultiModuleDemo'
include 'view'
include 'service'
include 'repository'
include 'common'

父模块的 build.gradle

plugins {
    id 'org.springframework.boot' version '2.1.14.RELEASE'
    id "io.spring.dependency-management" version "1.0.9.RELEASE"
}

// 为所有子项目设置属性(作用域包含view、service、repository、common模块)
subprojects {
    group = 'com.demo'
    version = '0.1'
    apply plugin: "java"
    apply plugin: "org.springframework.boot"
    apply plugin: "io.spring.dependency-management"
    sourceCompatibility = 1.8
    // view为最外层的模块
    if (project.name != "view") {
        jar.enabled = true
    }
    // 设置打包时的启动类
    springBoot {
        mainClassName = 'com.demo.view.Application'
    }
    repositories {
        maven { url 'https://maven.aliyun.com/repository/spring' }
        jcenter()
        mavenCentral()
        google()
    }
    // 共用依赖配置
    dependencies {
        compileOnly 'org.projectlombok:lombok:1.18.10'
        annotationProcessor 'org.projectlombok:lombok:1.18.10'
        testCompileOnly 'org.projectlombok:lombok:1.18.10'
        testAnnotationProcessor 'org.projectlombok:lombok:1.18.10'
    }
}

下面是各个子模块的 build.gradle

// view模块的build.gradle
dependencies {
    compile project(':service')
    testCompile group: 'junit', name: 'junit', version: '4.12'
}
// service模块的build.gradle
dependencies {
    compile project(':repository')
    testCompile group: 'junit', name: 'junit', version: '4.12'
}
// repository模块的build.gradle
dependencies {
    compile project(':common')
    compile 'org.springframework.boot:spring-boot-starter-data-jpa'
    compile 'org.springframework.boot:spring-boot-starter-web'
    runtimeOnly 'mysql:mysql-connector-java'
    testCompile group: 'junit', name: 'junit', version: '4.12'
    annotationProcessor "org.springframework.boot:spring-boot-configuration-processor"
}
// common模块的build.gradle
dependencies {
    compile 'org.springframework.boot:spring-boot-autoconfigure'
    compile 'org.springframework.boot:spring-boot-starter-logging'
    compile 'com.google.guava:guava:26.0-jre'
    compile 'com.squareup.okhttp3:okhttp:4.5.0'
    testCompile group: 'junit', name: 'junit', version: '4.12'
    annotationProcessor "org.springframework.boot:spring-boot-configuration-processor"
}

springboot配置文件处理

多模块下,除了最外层view子模块以外,其他子模块的配置文件都按照如下格式命名:
application-[module]-[env].yml,module命名随便,这里以模块名称为例
例如:common模块配置文件:

  • application-common.yml
  • application-common-dev.yml
  • application-common-pro.yml

view模块的配置文件保持与springboot的一致:

  • application.yml
  • application-dev.yml
  • application-pro.yml
# application.yml ,使用include引入其他模块的配置文件
spring:
  profiles:
    include: service,repository,common
server:
  port: 8081
# application-dev.yml
spring:
  profiles:
    include: service-dev,repository-dev,common-dev
server:
  port: 8082
# application-pro.yml
spring:
  profiles:
    include: service-pro,repository-pro,common-pro
server:
  port: 8083

为了验证springboot的配置文件正确生效,在common模块中加了一个自定义字段的配置,pub字段对应3个环境的值分别为:pubpubDevpubPro

package com.demo.common.config;

import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;


@Setter
@Getter
@Configuration
@ConfigurationProperties(prefix = "demo")
public class DemoConfig {

    private String pub;
    
}

view模块加了个入口,验证配置是否生效

package com.demo.view.controller;

import com.demo.common.config.DemoConfig;
import com.demo.repository.dto.DemoResponse;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/test")
public class TestController {

    private final DemoConfig demoConfig;

    public TestController(DemoConfig demoConfig) {
        this.demoConfig = demoConfig;
    }

    @GetMapping("/la")
    public DemoResponse test() {
        return new DemoResponse(demoConfig.getPub());
    }

}

用dev启动:
在这里插入图片描述
调用接口看看响应的值是不是pubDev:
在这里插入图片描述
可以看出common模块的dev环境的配置文件生效了。

gradle打包

打包仅需要在父模块目录下执行gradle打包命令,即可在view模块的build/libs目录下生产jar包,方便jenkins集成

gradle build -x test

-x test :打包时跳过测试代码即不会执行test包下的类
gradle现在会自动识别springboot项目,并执行bootJar task,也就是下面的打包命令和上面的打包命令结果是一致的

gradle build bootJar -x test
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值