본문 바로가기
spring & boot/Spring & Spring Boot

[spring boot] spring-boot profiles설정 & jar 실행시 프로파일 선택

by lucas_owner 2022. 12. 29.

 

spring-boot 프로파일 설정 및 jar 실행시 프로파일 선택하는 방법

 

목차 

1. 프로파일 설정이유

2. 운영환경

3. yml 전체 코드

4. 프로파일(profiles) 설정 방법

5. Jar 실행시 프로파일 설정 방법

6. IntelliJ 에서 프로파일 지정 후 자동 실행 방법


프로파일을 설정하는 이유?

Spring boot로 개발을 진행 하다보면, 상황에 따라 설정을 수정할 것들이 많아진다 ! 

예를들면 운영환경, 개발환경(Local) 이와 같이 두개의 환경이 다른 경우가 있다!

제일 많이 다루는 설정이라 하면,, 보통 DB 선택과 설정이다!

 

yml(properties)를 잘 몰랐을 때는 일일히 주석으로 막아가며 테스트와 운영개발을 진행했었다.. 

하지만 프로파일 설정을 알게 된다면 개발을 좀더 편하게 할 수 있다! 

 

필자는 2개의 DB 를 번갈아 가며 테스트와 개발을 진행해야 하는 환경이다.


운영환경

- Spring-boot 2.7.2

- intelliJ

- MySQL 5.7

- H2


yml 파일

- 현재 프로젝트 yml의 일부분을 발췌 했다. 보기에는 뭔가 길고 복잡해 보이지만 실상 뜯어보면 그렇지 않다.

 

- default(전역) 변하지 않는부분을 설정 해주고, 변환이 필요한 부분은 프로파일에 따로 나누어 설정 해주었다.  

#default
# batch Test - ddl-auto create !!!
  spring:
    profiles:
      active: h2
    # JPA
    jpa:
      show-sql: true
      properties:
        hibernate:
          format_sql: true
      hibernate:
        ddl-auto: create
  #  SQL auto Create allow - use - true, always,  notUse - false, never
      defer-datasource-initialization: false
    sql:
      init:
        mode: never


---
#  H2
  spring:
    config:
      activate:
        on-profile: h2
      #  Dev Options
    devtools:
      livereload:
        enabled: true
    thymeleaf:
      cache: true
      #  H2
      #  DB
    datasource:
        url: jdbc:h2:tcp://localhost/~/make
        driver-class-name: org.h2.Driver
        username: sa
    batch:
      jdbc:
        initialize-schema: embedded
      job:
        enabled: true

---
  # mysql
  spring:
    config:
      activate:
        on-profile: mysql
      #  Dev Options
    devtools:
      livereload:
        enabled: true
    thymeleaf:
      cache: true
        #  MySQL
        #  DB
    datasource:
        url: jdbc:mysql://localhost:3306/make?useSSL=false
        driver-class-name: com.mysql.cj.jdbc.Driver
        username: root
        password: 1234
    batch:
      jdbc:
       initialize-schema: always
      job:
        enabled: false

프로파일(Profiles) 설정하는 방법

- 스프링 부트 2.4 이후 버전부터는 프로파일 설정법이 변경되었다.

- ---(하이푼3개) 를 사용하여 각 부분을 나눌수(구분할수) 있다! 

 

2.4 이전 버전

# 2.4 이전 버전

# Default
spring:
  profiles:
     active: mysql

---
# h2
spring:
  profiles: h2 
  # 기타 다른 설정
  
---
# mysql
spring:
  profiles: mysql 
  # 기타 다른 설정

 

2.4 이후 버전 

# 2.4 이후 버전

# Defalut
spring: 
  profiles:
    active: h2
    
---
# h2 
spring: 
  config: 
  	activate:
    	on-profiles: h2
# 기타 다른 설정       

---
# mysql
spring: 
  config: 
  	activate:
    	on-profiles: mysql
# 기타 다른 설정

---
# test 
spring: 
  config: 
  	activate:
    	on-profiles: test 
# 기타 다른 설정

--- 
spring:
	profiles:
    	group:
        	"dev": "test, mysql"

- group을 통해 여러가지의 프로파일을 하나의 프로파일처럼 사용할 수 도 있다. 

- 세부 설정을 진행 하다보면 설정의 위치를 잘못잡아서 yml 설정이 안될 수 도 있으니 주의 해야한다.


Jar 실행시 프로파일 선택 방법

jar 파일로 만드는 방법은 생략하도록 하겠다!

 

실행 옵션

--spring.profiles.active=mysql

실행 예제

java -jar testProject-0.0.1-SNAPSHOT.jar —spring.profiles.active=mysql

 

실행 예제2 (2023.01.02 추가)

- 해당 방법도 가능

java -jar -Dspring.profiles.active=[프로파일명] testProject-0.0.1-SNAPSHOT.jar


IntelliJ 에서 Active Profile 설정으로 편하게 사용 하고싶다면!!

2023.01.02 추가! 

 

https://lucas-owner.tistory.com/22

 

[IntelliJ] active profile 설정 하기 (Ultimate & Community)

IntelliJ (Ultimate, Community)에서 active profile 설정 하는 방법 IntelliJ IDE를 사용해서 Spring Boot 환경에서 개발하다 보면 profile 별로 실행을 해야 할 때가 존재한다. profile 별로 실행 할 때 마다, application.ym

lucas-owner.tistory.com

 

반응형

댓글