Gradle构建时像Maven那样根据profile环境打包
Gradle中并没有直接类似Maven中的profiles支持,但是可以在processResources
任务中写一些脚本,通过传入的系统属性profile
值来支持.
例如运行gradle build时,传入profile系统属性
- 打包开发环境:
gradlew build -Dprofile=dev
- 打包测试环境:
gradlew build -Dprofile=test
- 打包生产环境:
gradlew build -Dprofile=prod
//@wjw_note: 根据传进来的profile系统属性来打包.
//def profileName = System.getProperty("profile") ?: "dev"
def profileName = System.getProperty("profile")
if(profileName==null) {
throw new BuildCancelledException("must pass The environment variable 'profile'rn"
"For example: gradlew clean build -i -x test --no-daemon -Dprofile=dev")
}
processResources {
include '**/public/**'
include '**/static/**'
include '**/templates/**'
include '**/tpl/**'
include { FileTreeElement details ->
details.isDirectory()==true || details.file.name.contains("-${profileName}.") /* 根据传入的profileName系统属性来过滤 */
}
}