Gradle ã§ç°å¢ãã¨ã«æ§æãå¤æ´ãã ãã®ï¼
ãããªå¤§å¤ãªã¨ãã«ãªãã§ãã...
ä½ãããªããã㯠èªåã«ã§ãããã¨ãç¶ãã¦ãããï¼ï¼
ã¨è¨ããã¨ã§...
id:fumokmm ãããã ConfigSlurper ã§è¤æ°ã®è¨å®ãã¡ã¤ã«ã®å
容ã
ãã¼ã¸ããæ¹æ³ãæãã¦ããã£ãã®ã§ãä»å㯠ç°å¢ãã¨ã®æ§æã
- ããã©ã«ãè¨å® environment/default.gradle
- ç°å¢æ¯ã®è¨å® environment/${env}.gradle
- ã¦ã¼ã¶æ¯ã®è¨å® ${user.home}/.gradle/environment.gradle
build.gradle ã¯ãããªæã...
if (!hasProperty('env')) { env = System.env['GRADLE_ENV'] ?: 'development' } config = { def slurper = new ConfigSlurper() slurper.binding = project.properties def files = files("environment/default.gradle", "environment/${env}.gradle", "${System.properties['user.home']}/.gradle/environment.gradle") files.inject(new ConfigObject()) { config, file -> file.exists() ? config.merge(slurper.parse(file.toURL())) : config } }()
inject ã§è¨å®ãã¡ã¤ã«ãé çªã«èªã¿è¾¼ã¿ãConfigObject#merge(ConfigObject) ã§ãã¼ã¸ããããã«ãã¦ã¿ã¾ããã
ConfigObject#merge(ConfigObject) 㯠å¼æ°ã® ConfigObject ã®æ¹ãåªå
ããããããªã®ã§ åªå
度ã®ä½ããã¡ã¤ã«ããèªã¿è¾¼ã¿ã¾ãã
Groovy ã使ãã°ããã£ãããã ã...
ã¨ã£ã¦ã便å©ã§ããã
ã§å®éããã§ä½ãã§ããããã«ãªã£ãã®ãã¨è¨ãã¨...
ä¾ãã°ããããªæãã§ãã¼ã¿ãã¼ã¹ã®è¨å®ãã§ããããã«ãªãã¾ãã
environment/default.gradle
å ±éçãªè¨å®ãæ¸ãã¾ããããdatabase { driverClassName = "org.hsqldb.jdbcDriver" username = 'bluepapa32' password = '' }
environment/${env}.gradle
åç°å¢æ¯ã®è¨å®ãæ¸ãã¾ãããã# deveropment.gradle database { url = "jdbc:hsqldb:mem:devDB" }
# test.gradle database { url = "jdbc:hsqldb:mem:testDB" }
# production.gradle database { driverClassName = "oracle.jdbc.driver.OracleDriver" url = "jdbc:oracle:thin:dbserv:1521:prodDB" }
production ç°å¢ã® ${user.home}/.gradle/environment.gradle
ãã¼ã¸ã§ã³ç®¡çã§ããªããã㪠å人è¨å®ããã¹ã¯ã¼ãçã®æ©å¯æ å ±ãæ¸ãã¾ããããdatabase { password = 'secret' }
åç°å¢ã§å®è¡ãã¦ã¿ãã¨...
$ ./gradlew -q server.url: http://app:8080/setting database.driverClassName: org.hsqldb.jdbcDriver database.url: jdbc:hsqldb:mem:devDB database.username: bluepapa32 database.password: $ ./gradlew -q -Penv=test server.url: http://app/setting database.driverClassName: org.hsqldb.jdbcDriver database.url: jdbc:hsqldb:mem:testDB database.username: bluepapa32 database.password: $ ./gradlew -q -Penv=production server.url: https://app/setting database.driverClassName: oracle.jdbc.driver.OracleDriver database.url: jdbc:oracle:thin:dbserv:1521:prodDB database.username: bluepapa32 database.password: secretã®ãããªæãã§æ§æãå¤æ´ã§ãã¾ãã
ãããèå³ã®ããæ¹ã¯
https://github.com/bluepapa32/gradle-environment-sample/tree/20110314ã«ãµã³ãã«ãããã¾ãã®ã§è©¦ãã¦ã¿ã¦ãã ããã