개발/운영 환경에 따라 설정파일관련 정보들이 빌드 시점에 동적으로 변경되어야 한다.

Maven에는 Profile이라는 기능을 통하여 처리하는 반면, Gradle의 경우 System Propertie와 비슷한 방법을 이용하여 처리한다.


[디렉토리 구조]



[build.gradle]

build 시작에 properties-dev 에 있는 내용을 properties 에 옮기고, logback.xml 파일은 class root에 위치 시키는 내용이다.

build 실행 시 target이라는 옵션을 주면 된다.

-Ptarget=dev build  // 주의할 점은 java VM -D가 아니라는 점임

...

sourceSets {
	def targetServer = project.hasProperty('target') ? project.getProperty('target') : 'local'
	println "targetServer: $targetServer"
	if( "$targetServer" != "local" ){
		copy {
			from("src/main/resources/properties-$targetServer"){
				exclude 'logback.xml'
			}
			into "src/main/resources/properties"
		}
		copy {
			from("src/main/resources/properties-$targetServer"){
				include 'logback.xml'
			}
			into("src/main/resources/")
		}
		delete file("src/main/resources/properties-dev")
		delete file("src/main/resources/properties-stage")
		delete file('src/main/resources/properties-real')
	}
}

...


+ Recent posts