spring을 사용해 프로젝트를 진행하면
local, dev, release 와 같이 여러 환경을 만들고
해당 환경마다 다른 데이터를 사용하게 된다.
메이븐의 profile을 사용하면
컴파일이나 패키지 시에 파라미터를 넘겨줌으로써 손쉽게 해당 환경에 맞는 데이터를 사용할 수 있다.
예를 들어,
각 환경에서 아래의 정보를 사용한다고 해보자.
[local.properties]
1 2 3 |
<p>db.username=local</p> <p>db.password=local-pwd</p> <p>db.url=local_db_url</p> |
[release.properties]
1 2 3 |
<p>db.username=release</p> <p>db.password=relase-pwd</p> <p>db.url=release_db_url</p> |
로컬에서는 local.properties 안에 있는 정보를 사용하고
실제 서비스에서는 release.properties 안에 있는 정보를 사용해야 한다.
[/src/main/resources/data.properties]
1 2 3 |
<p>db.username=${db.username}</p> <p>db.password=${db.password}</p> <p>db.url=${db.url}</p> |
[pom.xml]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
<p> <profiles></p> <p> <profile></p> <p> <id>local</id></p> <p> <properties></p> <p> <resource.filter>build.local.properties</resource.filter></p> <p> </properties></p> <p> <activation></p> <p> <activeByDefault> true </activeByDefault></p> <p> </activation></p> <p> </profile></p> <p> <profile></p> <p> <id>release</id></p> <p> <properties></p> <p> <resource.filter>build.release.properties</resource.filter></p> <p> </properties></p> <p> </profile></p> <p></profiles></p> <p> </p> <p><build></p> <p> <resources></p> <p> <resource></p> <p> <filtering> true </filtering></p> <p> <directory>src/main/resources</directory></p> <p> </resource></p> <p> </resources></p> <p> <filters></p> <p> <filter>${resource.filter}</filter></p> <p> </filters></p> <p></build></p> |
pom.xml에 위의 정보를 추가해준다.
1. 2개의 profile이 추가된다.
- id : 메이븐 컴파일 시 넘기는 -P [id] 를 통해 동일한 id에 해당하는 profile을 사용하게 된다.
- resource.filter : 사용할 정보를 가지고 있는 실제 파일명 ( -P local 시, build.local.properties의 정보를 컴파일 )
- activeByDefault : 컴파일시 -P 정보를 안넘겨줄시 오류가 날 수 있으므로 디폴트로 사용할 profile을 지정해주는 역할
2. build 정보
- resources - filtering : 디폴트값은 false. true로 해줘야 필터링이 적용된다. (필터링 = properties에 있는 정보로 치환)
- resources - directory : 실제 resources가 저장될 디렉토리
- filters - filter : 위 profile에서 지정한 resource.filter를 사용한다. (실제 필터링 될 properties 파일 지정)
* 만약 실제로 적용되었는지 확인하려면 mvn package -P local | release 로 컴파일 후, target/snapshot/WEB-INF/classes/data.properties 에서 확인할 수 있다.
참고 :
