Gradle ã§ç°å¢ãã¨ã«æ§æãå¤æ´ãã ãã®ï¼
前回ã前々回 ã¨æ¢ã«ï¼åãæ¸ãã¦ãããªãããªãã§ããã
è¨å®ãã¡ã¤ã«ãèªã¿è¾¼ã㧠æ¨æºåºåããã ãã®ãµã³ãã«ãªãã¦ä½ã®å½¹ã«ãç«ããªã...
ã¨è¨ããã¨ã§ãä»å㯠èªã¿è¾¼ãã è¨å®ã ã¢ããªã±ã¼ã·ã§ã³ã®ããããã£ãã¡ã¤ã«ã«
å±éãã¦ã¿ããã¨ã«...
çµå± build.gradle ã¯ãããªæãã«ãªãã¾ãã...
defaultTasks 'clean', 'build' apply plugin: 'java' if (!hasProperty('env')) { env = System.env['GRADLE_ENV'] ?: 'development' } { -> def slurper = new ConfigSlurper() slurper.binding = project.properties def files = files("environment/default.gradle", "environment/${env}.gradle", "${System.properties['user.home']}/.gradle/environment.gradle") def config = files.inject(new ConfigObject()) { config, file -> file.exists() ? config.merge(slurper.parse(file.toURL())) : config } // Project#properties ã«ã»ãã config.each { k, v -> setProperty k, v } }() // Project#properties ã ãªã½ã¼ã¹ã«å±é processResources { expand (properties) } // ãããã㣠åºå build << { println file("${sourceSets.main.classesDir}/Application.properties").text }
ååã¨ã®éãã¯
- èªã¿è¾¼ãã è¨å®ãã¡ã¤ã«ã®å 容ã Project#properties ã«è¨å®ããããã«ãã
- processResources ã¿ã¹ã¯ã§ Application.properties ãã³ãã¼ããéã« Project#properties ãå±éããããã«ãã
- build ã¿ã¹ã¯ 㧠Application.properties ã®å 容ãæ¨æºåºåããããã«ãã
èªã¿è¾¼ãã è¨å®ãã¡ã¤ã«ã®å 容ã Project#properties ã«è¨å®ããã¨
println config.server.urlã§ã¯ãªã
println server.urlã®ããã«ã¢ã¯ã»ã¹ã§ããããã«ãªã£ã¦ ãã¡ãã£ã¨ã ãããæãã«ãªãã¾ãã
Application.properties ã« Project#properties ãå±éããã«ã¯ build.gradle ã«
processResources { expand(project.properties) }ã追å ã㦠(ãã¡ãã "<<" ã¯ããã¾ãã) Application.properties ã«
server.url = ${server.url} database.driverClassName = ${database.driverClassName} database.url = ${database.url} database.username = ${database.username} database.password = ${database.password}ã®ããã«è¨è¿°ããã ãã§ãã
ããããã£ãã¡ã¤ã«ã®æ¸ãæ¹ã¯ Ant ã§ããããã£å±éããå ´åã¨ã»ã¨ãã©åãã§ãã
å®éã«å®è¡ãã¦ã¿ãã¨
$ ./gradlew -p server.url: http://app:8080/setting database.driverClassName: org.hsqldb.jdbcDriver database.url: jdbc:hsqldb:mem:devDB database.username: bluepapa32 database.password:ã®ããã« ã¡ãã㨠Application.properties ã«ããããã£å±éããã¦æ¨æºåºåããã¾ãã
ãããèå³ã®ããæ¹ã¯
https://github.com/bluepapa32/gradle-environment-sample/tree/20110315ã«ãµã³ãã«ãããã¾ãã®ã§è©¦ãã¦ã¿ã¦ãã ããã