Wookim

spring application 실행 환경 분리 (프로파일) 본문

programming language/Java

spring application 실행 환경 분리 (프로파일)

개발자인 경우 2021. 7. 9. 18:06

내용

어플리케이션의 실행 환경을 **프로파일(환경)**별로 나누어 별도 저장하여 관리 필요

작업

  1. spring boot 프로젝트 생성
  2. src/main/resources/application.properties 파일 확인

application.properties 파일 (공통)

spring.profiles.active=@profileActive@

# 환경 별 공통 설정 내용 작성

 

3. src/main/resources/application-local.properties 파일 생성 (로컬)

# 로컬 환경 전용 설정 내용 작성

 

4. src/main/resources/application-dev.properties 파일 생성 (개발)

# 개발 환경 전용 설정 내용 작성

 

5. src/main/resousrces/application-prod.properties 파일 생성 (운영)

# 운영 환경 전용 설정 내용 작성

 

6. 빌드 관련 문서(mavne의 pom, gradle의 gradle.build) 내용에 profiles 내용 추가

...

</dependencies>


<!-- 프로파일 설정 -->
<profiles>
	<profile>
		<id>local</id>
		<properties>
			<env>local</env>
			<build.profile.id>local</build.profile.id>
			<profileActive>local</profileActive>
		</properties>
	</profile>
	<profile>
		<id>dev</id>
		<properties>
			<env>dev</env>
			<build.profile.id>dev</build.profile.id>
			<profileActive>dev</profileActive>
		</properties>
	</profile>
	<profile>
		<id>prod</id>
		<properties>
			<env>prod</env>
			<build.profile.id>prod</build.profile.id>
			<profileActive>prod</profileActive>
		</properties>
	</profile>

</profiles>

<!-- 프로파일 별 컴파일 시 프로필 설정 파일 포함 여부 설정 -->
<resources>
	<resource>
		<directory>src/main/resources</directory>
		<includes>
			<include>application.properties</include>
			<include>application-${env}.properties</include>
		</includes>
	</resource>
</resources>

7. IDE 에서 로컬 > 컴파일 > 실행 하기 위해 maven, gradle의 알맞은 프로파일 옵션을 설정한다.

8. 혹은 컴파일을 직접 한다면 명령어에 프로파일을 직접 설정할것.

 

ex)
mvn clean package {컴파일 옵션...} -P dev

ex)
mvn clean package ${선언한 프로필 환경변수}
Comments