Gradle: copy various properties files depending on the environment and create a jar

I am evaluating gradle for my spring boot project. Everything seems to work, but that's where I got stuck. I have 2 properties files. One for the product:

application_prod.properties

and the other for qa ie:

application_qa.properties

My requirement is that while I create (create a jar file) a project from gradle, I have to rename the properties file to

application.properties

and then create the jar file. As far as I know, gradle has a default build task. Therefore, here I have to redefine it so that it takes into account only the necessary properties file and renames it, and then creates it depending on the environment.

How can i achieve this?

+5
source share
1 answer

What you need to do is override the processResources configuration:

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

When replacing the following code fragment, you will get the following result:

 $ ./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 .

+5
source

Source: https://habr.com/ru/post/1247630/


All Articles