spring을 사용해 프로젝트를 진행하면
local, dev, release 와 같이 여러 환경을 만들고
해당 환경마다 다른 데이터를 사용하게 된다.
메이븐의 profile을 사용하면
컴파일이나 패키지 시에 파라미터를 넘겨줌으로써 손쉽게 해당 환경에 맞는 데이터를 사용할 수 있다.
예를 들어,
각 환경에서 아래의 정보를 사용한다고 해보자.
[local.properties]
db.username=local
db.password=local-pwd
db.url=local_db_url
[release.properties]
db.username=release
db.password=relase-pwd
db.url=release_db_url
로컬에서는 local.properties 안에 있는 정보를 사용하고
실제 서비스에서는 release.properties 안에 있는 정보를 사용해야 한다.
[/src/main/resources/data.properties]
db.username=${db.username}
db.password=${db.password}
db.url=${db.url}
[pom.xml]
<profiles>
<profile>
<id>local</id>
<properties>
<resource.filter>build.local.properties</resource.filter>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>release</id>
<properties>
<resource.filter>build.release.properties</resource.filter>
</properties>
</profile>
</profiles>
<build>
<resources>
<resource>
<filtering>true</filtering>
<directory>src/main/resources</directory>
</resource>
</resources>
<filters>
<filter>${resource.filter}</filter>
</filters>
</build>
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 에서 확인할 수 있다.
참고 :