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

后端 未结 1 1618
深忆病人
深忆病人 2021-01-13 07:33

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.:

<
相关标签:
1条回答
  • 2021-01-13 08:31

    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.

    0 讨论(0)
提交回复
热议问题