Spring boot resources directory profile별 환경분리

gh·2022년 4월 1일
0

resources 디렉토리별 환경분리를 사용하는 이유

일반 적으로 application-${profile}.properties 형태로 사용하여 실행옵션에 active profile로 사용할 properties를 지정해줍니다. 이와 같은 방법에는 아래와 같은 단점이 있습니다.

  • 빌드된 jar 파일안에 모든 환경의 properties 가 노출됩니다.
  • profile 별로 새로운 resources를 추가해야할 경우 해당 설정이 들어가야합니다.
    -> 필자의경우 log4j2의 설정을 profile 별로 분리하였습니다.

개발 환경

  • gradle 7.4.1
  • spring boot 2.6.6

설정

build.gradle

// build 시 profile 옵션이 빠졌을시 자동으로 local로 세팅 -> 해당 ide에서 따로 세팅할필요가없다.
ext.profile = (!project.hasProperty('profile') || !profile) ? 'local' : profile

//resources-* 를 전체 제외 시키고 필요한 resources-${profile} 만 빌드에 포함시킨다.
sourceSets{
    main{
        resources {
            srcDirs "src/main/resources/resources-${profile}"
            exclude "resources-*"
        }
    }
}

Directory 구조

application.properties

#개발환경에 상관없는 공통 프로퍼티
spring.profiles.include=core
#active profile
spring.profiles.active=local

빌드결과


빌드된 jar 파일의 압축을 풀었을때 위와 같이 해당 properties 파일만 들어간것을 확인할 수 있습니다.

Git

https://github.com/gh90/example-profile

profile
개발자~

0개의 댓글