逝者如斯

gradle build文件多环境配置

2017-04-24  本文已影响26人  福克斯记

build.gradle

task printProps << {
    println "db.url:  $config.database.url"
    println "db.user: $config.database.user"
    println "db.password:   $config.database.password"
    println "db.dbname:   $config.database.dbname"
}

def loadConfiguration() {
    def environment = hasProperty('env') ? env : 'dev'
    project.ext.environment = environment

    def configFile = file('config.groovy')
    def config = new ConfigSlurper(environment).parse(configFile.toURI().toURL())
    project.ext.config = config
}

同目录下,config.groovy

environments {
    dev {
        database {
            url = "jdbc:mysql://127.0.0.1/"
            user = "root"
            password = ""
            dbname = "diamond"
        }
    }

    test {
        database {
            url = ""
            user = ""
            password = ""
            dbname = ""
        }
    }

    prod {
        database {
            url = ""
            user = ""
            password = ""
            dbname = "bizdb"
        }
    }
}
上一篇下一篇

猜你喜欢

热点阅读