javaspringgradleapplication.propertiesgradle.properties

Prevent gradle.properties expansion


Im new in Gradle enviroment and I'm developing a Spring Application with property expansion in application.properties file, like:server.port = $ {app_server_port}

Everything is working OK, when I build with gradle build command, Gradle takes the file~ / .gradle / gradle.properties and expand variables into the builded application.properties file.

My question is:

Thanks


Solution

  • You can write your build script like this:

    apply plugin: 'java'
    
    def props = new Properties()
    file("local.properties").withInputStream { props.load(it) }
    
    processResources {
      if (project.hasProperty('expand'))
        expand(props)
    }
    

    Now if you build with gradlew build -Pexpand property expansion will be done with values from local.properties.

    If you build with gradlew build no property expansion will take place.


    As a note, I would recommend to not put project specific properties into ~ / .gradle / gradle.properties, since this file is shared between all the projects that you build on your machine.