How to read a properties files and use the values in project Gradle script?

前端 未结 3 1919
悲哀的现实
悲哀的现实 2020-12-04 14:08

I am working on a Gradle script where I need to read the local.properties file and use the values in the properties file in build.gradle. I am doin

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-04 15:13

    We can use a separate file (config.groovy in my case) to abstract out all the configuration.

    In this example, we're using three environments viz.,

    1. dev
    2. test
    3. prod

    which has properties serverName, serverPort and resources. Here we're expecting that the third property resources may be same in multiple environments and so we've abstracted out that logic and overridden in the specific environment wherever necessary:

    config.groovy

    resources {
        serverName = 'localhost'
        serverPort = '8090'
    }
    
    environments {
        dev {
            serverName = 'http://localhost'   
            serverPort = '8080'
        }
    
        test {
            serverName = 'http://www.testserver.com'
            serverPort = '5211'
            resources {
                serverName = 'resources.testserver.com'
            }
        }
    
        prod {
            serverName = 'http://www.productionserver.com'
            serverPort = '80'
            resources {
                serverName = 'resources.productionserver.com'
                serverPort = '80'
            }
        }
    }
    

    Once the properties file is ready, we can use the following in build.gradle to load these settings:

    build.gradle

    loadProperties()
    
    def loadProperties() {
        def environment = hasProperty('env') ? env : 'dev'
        println "Current Environment: " + environment
    
        def configFile = file('config.groovy')
        def config = new ConfigSlurper(environment).parse(configFile.toURL())
        project.ext.config = config
    }
    
    task printProperties {
        println "serverName:  $config.serverName"
        println "serverPort:  $config.serverPort"
        println "resources.serverName:  $config.resources.serverName"
        println "resources.serverPort:  $config.resources.serverPort"
    }
    

    Let's run these with different set of inputs:

    1. gradle -q printProperties

      Current Environment: dev
      serverName:  http://localhost
      serverPort:  8080
      resources.serverName:  localhost
      resources.serverPort:  8090
      
    2. gradle -q -Penv=dev printProperties

      Current Environment: dev
      serverName:  http://localhost
      serverPort:  8080
      resources.serverName:  localhost
      resources.serverPort:  8090
      
    3. gradle -q -Penv=test printProperties

      Current Environment: test
      serverName:  http://www.testserver.com
      serverPort:  5211
      resources.serverName:  resources.testserver.com
      resources.serverPort:  8090
      
    4. gradle -q -Penv=prod printProperties

      Current Environment: prod
      serverName:  http://www.productionserver.com
      serverPort:  80
      resources.serverName:  resources.productionserver.com
      resources.serverPort:  80
      

提交回复
热议问题