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

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.***/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'
    //指定编译版本
    source***patibility = 1.8
    target***patibility = 1.8

    tasks.withType(Java***pile){
        options.encoding='UTF-8'
    }

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

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

    // 所有子项目的基础包
    dependencies {
        ***pileOnly '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 "***.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 "***.alibaba.cloud:spring-cloud-starter-alibaba-nacos-discovery:${springCloudAlibabaVersion}"
            dependency "org.springframework.cloud:spring-cloud-starter-openfeign:${springCloudAlibabaVersion}"
            dependency "***.alibaba.cloud:spring-cloud-starter-alibaba-nacos-config:${springCloudAlibabaVersion}"
            dependency "***.alibaba:druid-spring-boot-starter:${druidVersion}"
            dependency "mysql:mysql-connector-java:${mysqlVersion}"
            dependency "***.github.pagehelper:pagehelper:${pagehelperVersion}"
            dependency "***.github.pagehelper:pagehelper-spring-boot-autoconfigure:${pagehelperSpringbootVersion}"
            dependency "***.github.pagehelper:pagehelper-spring-boot-starter:${pagehelperSpringbootVersion}"
            dependency "***.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 '***.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 '***.alibaba.cloud:spring-cloud-starter-alibaba-nacos-discovery'
    implementation '***.alibaba.cloud:spring-cloud-starter-alibaba-nacos-config'
    implementation 'org.springframework.cloud:spring-cloud-starter-openfeign'
    implementation '***.alibaba:druid-spring-boot-starter'
    implementation 'mysql:mysql-connector-java'
    implementation '***.github.pagehelper:pagehelper'
    implementation '***.github.pagehelper:pagehelper-spring-boot-autoconfigure'
    implementation '***.github.pagehelper:pagehelper-spring-boot-starter'
    implementation '***.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包里

转载请说明出处内容投诉
CSS教程_站长资源网 » gradle+springboot实现多项目分环境打包

发表评论

欢迎 访客 发表评论

一个令你着迷的主题!

查看演示 官网购买