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'\r\n"
+"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系统属性来过滤 */
}
}