Gradle根据部署环境引用不同的配置文件进行打包

来源:互联网 发布:seo自学网haiyaoseo 编辑:程序博客网 时间:2024/05/18 03:15

由于开发,测试,生产环境不同,需要不同的配置文件,如果项目多了打包很不方便。综合网上的一些资料,实现Gradle从Git上获取配置文件进行打包

buildscript {  dependencies {    classpath("org.ajoberstar:gradle-git:1.5.0")  }}

Gradle打war包,修改build.gradle文件如下:

apply plugin: 'war'import org.ajoberstar.grgit.*def env = System.getProperty("profile") ?: "dev"//用profile参数确定引用哪个配置文件 def proPath="$buildDir/prop"task packageWar(type: War){  doFirst {  new File("$proPath").deleteDir();  Grgit.clone(dir: file("$proPath"), uri: 'git仓库',credentials: new Credentials(username: '用户名', password: '密码'))    copy {            from "$proPath/$env/application.properties"            into "$buildDir/resources/main"        }  } }

用gradlew packageWar命令打包


Gradle打jar包,修改build.gradle文件如下:

import org.ajoberstar.grgit.*def proPath="$buildDir/prop"task copyPro(type: Jar) {doFirst {  Grgit.clone(dir: file("$proPath"), uri: 'git仓库',credentials: new Credentials(username: '用户名', password: '密码'))    copy {            from "$proPath/application.properties"            into "$buildDir/resources/main"        }  }  from sourceSets.main.output}task packageJar(type: BootRepackage, dependsOn: copyPro) {    new File("$buildDir").deleteDir();    withJarTask = copyPro} 

用gradlew packageJar命令打包

0 0
原创粉丝点击