gradle構建時按profile環境打包

配置文件如下

plugins {
    id 'java'
    id 'org.springframework.boot' version '2.2.3.RELEASE'
    id 'io.spring.dependency-management' version '1.0.8.RELEASE'
}


jar {
    baseName = 'test'
    version = '0.0.1'
    manifest {
        attributes "Manifest-Version": 1.0,
                'Main-Class': 'com.dongxin.iifdaservice.iifdaserviceApplication'
    }
}

group = 'com.dongxin'
version = '1.0.0'

sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8


// 構建時profile的指定並替換profile,
// 構建命令:
// gradle clean build -i -x test --no-daemon -Dprofile=uat -b build.gradle processResources
// 或者
// gradle clean build -i -x test --no-daemon -Dprofile=uat -b build.gradle"
def profileName = System.getProperty("profile") ?: "dev"
processResources {
    filter org.apache.tools.ant.filters.ReplaceTokens, tokens: [profileName: profileName]
}

allprojects {
    repositories {
        if(profileName == 'dev'){
            // 指定私庫
            maven { url "http://ip:8081/repository/maven-public/" }
        }
        maven{ url 'http://maven.aliyun.com/nexus/content/groups/public/'}
        google()
    }
}

主要是這裏,構建的時候替換掉

// 構建時profile的指定並替換profile,
// 構建命令:
// gradle clean build -i -x test --no-daemon -Dprofile=uat -b build.gradle processResources
// 或者
// gradle clean build -i -x test --no-daemon -Dprofile=uat -b build.gradle"
def profileName = System.getProperty("profile") ?: "dev"
processResources {
    filter org.apache.tools.ant.filters.ReplaceTokens, tokens: [profileName: profileName]
}

同時yml的profile要使用變量

spring:
  profiles:
    active: '@profileName@'

打包命令

gradle clean build -i -x test --no-daemon -Dprofile=uat -b build.gradle processResources
或者
gradle clean build -i -x test --no-daemon -Dprofile=uat -b build.gradle

build -i 把編譯的內容輸出出來
-x test 跳過單元測試

但是這裏會有個小問題,就是yml的profiles的active: '@profileName@'

在idea運行默認會使用@profileName@這個文本去指定環境,但是實際上沒有這個環境。所以直接在idea指定環境運行

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章