0


gradle+springboot实现多项目分环境打包

父项目的build.gradle

// 统一配置版本号
buildscript {
    ext {
        springBootVersion='2.3.11.RELEASE'
        springCloudVersion='Hoxton.SR8'
        mysqlVersion = '8.0.26'
        lombokVersion='1.18.16'
        springCloudAlibabaVersion='2.2.5.RELEASE'
        hutoolVersion='5.7.16'
        pagehelperVersion='5.1.10'
        pagehelperSpringbootVersion='1.2.12'
        druidVersion='1.1.10'
        springKafkaVersion='2.5.16.RELEASE'

        // 单元测试依赖版本
        jupiterVersion='5.6.0'
        powermockVersion='2.0.9'
    }
    // 脚本自身的插件仓库
    repositories {
        mavenLocal()
        maven{ url 'https://maven.aliyun.com/nexus/content/groups/public/'}
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

// 全局配置
allprojects{
    apply plugin: 'java'
    group 'hicon'
    version '1.0-SNAPSHOT'
    //指定编译版本
    sourceCompatibility = 1.8
    targetCompatibility = 1.8

    tasks.withType(JavaCompile){
        options.encoding='UTF-8'
    }

    repositories {
        mavenLocal()
        maven{ url 'https://maven.aliyun.com/nexus/content/groups/public/'}
        mavenCentral()
    }

}
// 子项目
subprojects{
    // 版本由spring-boot-gradle-plugin统一管理
    apply plugin: 'io.spring.dependency-management'
    apply plugin: 'org.springframework.boot'

    // 所有子项目的基础包
    dependencies {
        compileOnly 'org.projectlombok:lombok'
        annotationProcessor 'org.projectlombok:lombok'

        // 单元测试
        implementation 'org.springframework.boot:spring-boot-starter-test'
        implementation 'org.junit.jupiter:junit-jupiter'
        implementation 'org.powermock:powermock-module-junit4'
        implementation 'org.powermock:powermock-api-mockito2'
    }
    //gradle构建过程中的默认任务processResources,重写
    processResources {
        filesMatching('bootstrap.yml') {
            expand(project.properties)
        }
    }
    dependencyManagement {
        dependencies {
            dependency "org.springframework.cloud:spring-boot-dependencies:${springBootVersion}"
            dependency "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
            dependency "com.alibaba.cloud:spring-cloud-alibaba-dependencies:${springCloudAlibabaVersion}"
            dependency "org.springframework.boot:spring-boot-starter-web:${springBootVersion}"
            dependency "org.springframework.boot:spring-boot-starter-parent:${springBootVersion}"
            dependency "org.springframework.boot:spring-boot-starter-data-jpa:${springBootVersion}"
            dependency "com.alibaba.cloud:spring-cloud-starter-alibaba-nacos-discovery:${springCloudAlibabaVersion}"
            dependency "org.springframework.cloud:spring-cloud-starter-openfeign:${springCloudAlibabaVersion}"
            dependency "com.alibaba.cloud:spring-cloud-starter-alibaba-nacos-config:${springCloudAlibabaVersion}"
            dependency "com.alibaba:druid-spring-boot-starter:${druidVersion}"
            dependency "mysql:mysql-connector-java:${mysqlVersion}"
            dependency "com.github.pagehelper:pagehelper:${pagehelperVersion}"
            dependency "com.github.pagehelper:pagehelper-spring-boot-autoconfigure:${pagehelperSpringbootVersion}"
            dependency "com.github.pagehelper:pagehelper-spring-boot-starter:${pagehelperSpringbootVersion}"
            dependency "cn.hutool:hutool-all:${hutoolVersion}"
            dependency "org.springframework.kafka:spring-kafka:${springKafkaVersion}"

            // 单元测试
            dependency "org.springframework.boot:spring-boot-starter-test:${springBootVersion}"
            dependency "org.junit.jupiter:junit-jupiter:${jupiterVersion}"
            dependency "org.powermock:powermock-module-junit4:${powermockVersion}"
            dependency "org.powermock:powermock-api-mockito2:${powermockVersion}"
        }

    }
}
wrapper {
    gradleVersion = '7.0.2'
    archivePath = 'wrapper/dists'
    distributionPath = 'wrapper/dists'
    distributionUrl = 'https://services.gradle.org/distributions/gradle-7.0.2-bin.zip'
}

上面的文件中,控制环境变量的代码块是
在这里插入图片描述

子项目的build.gradle


dependencies {
    implementation fileTree(dir:'libs',includes: ['*.jar'])
    implementation 'org.springframework.cloud:spring-cloud-dependencies'
    implementation 'com.alibaba.cloud:spring-cloud-alibaba-dependencies'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-parent'
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'com.alibaba.cloud:spring-cloud-starter-alibaba-nacos-discovery'
    implementation 'com.alibaba.cloud:spring-cloud-starter-alibaba-nacos-config'
    implementation 'org.springframework.cloud:spring-cloud-starter-openfeign'
    implementation 'com.alibaba:druid-spring-boot-starter'
    implementation 'mysql:mysql-connector-java'
    implementation 'com.github.pagehelper:pagehelper'
    implementation 'com.github.pagehelper:pagehelper-spring-boot-autoconfigure'
    implementation 'com.github.pagehelper:pagehelper-spring-boot-starter'
    implementation 'cn.hutool:hutool-all'
    implementation 'org.springframework.kafka:spring-kafka'

}

环境变量在bootstrap.ym中

#配置环境
spring:
  profiles:
    active: ${profile}

打包命令

gradle build -x test -Pprofile=dev

执行结果截图

-x test 是排除单元测试
-Pprofile=dev是传递环境参数
打包完成后,bootstrap.yml中的变量会替换为dev

下一篇
打包时,外部依赖的jar,不打进jar包里


本文转载自: https://blog.csdn.net/yumo_fly/article/details/130272035
版权归原作者 yumo_fly 所有, 如有侵权,请联系我们删除。

“gradle+springboot实现多项目分环境打包”的评论:

还没有评论