Gradle : Copy different properties file depending on the environment and create jar

荒凉一梦 提交于 2020-01-29 13:23:07

问题


I am evaluating gradle for my spring boot project. Everything seems to work but here is where I am stuck. I have 2 properties file. One for prod i.e.:

application_prod.properties

and another for qa i.e.:

application_qa.properties

My requirement is such that while I build (create jar file) the project from gradle, I've to rename the properties file to

application.properties

and then build the jar file. As far as I know, gradle has a default build task. So here I've to override it such that it considers only the required properties file and rename it and then build depending on the environment.

How can I achieve this?


回答1:


What you need to do is to override processResources configuration:

processResources {
    def profile = (project.hasProperty('profile') ? project.profile : 'qa').toLowerCase()
    include "**/application_${profile}.properties"
    rename {
        'application.properties'
    }
}

With the following piece of code changed you will get the output below:

$ ./gradlew run -Pprofile=PROD
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:run
LOL
Profile: PROD

BUILD SUCCESSFUL

Total time: 3.63 secs

$ ./gradlew run -Pprofile=QA  
:compileJava UP-TO-DATE
:processResources
:classes
:run
LOL
Profile: QA

BUILD SUCCESSFUL

Total time: 3.686 secs

$ ./gradlew run             
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:run
LOL
Profile: QA

BUILD SUCCESSFUL

Total time: 3.701 secs

Demo is here.



来源:https://stackoverflow.com/questions/36771697/gradle-copy-different-properties-file-depending-on-the-environment-and-create

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!