15R6
15R6

Reputation: 307

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

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?

Upvotes: 6

Views: 5580

Answers (1)

Opal
Opal

Reputation: 84756

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.

Upvotes: 8

Related Questions