Dev/Java

[Jvm] JAVA_OPTS 설정

pu3vig 2022. 6. 20. 13:42
728x90
  • target:  Jvm 실행 시, java_opts 설정

 


  • method:  

1. Tomcat인 경우

// 리눅스인 경우, catalina.sh 파일에 JAVA_OPTS profiles 설정
// ex. profile명이 dev인 경우
JAVA_OPTS="$JAVA_OPTS -Dspring.profiles.active=dev"

※ $JAVA_OPTS를 통해, 이미 설정되어있는 환경변수 정보를 포함하여 profiles를 설정

※ -D${key}=${value}를 통해서 runtime에서 특정 변수에 특정 값을 닮아서, 서버 실행 시 지정 가능

 


2. Jvm 실행 시

// build한 jar 파일을 jvm에서 실행하면서 옵션을 주는 경우
$ java -jar ROOT.jar -Dspring.profiles.active=dev

※ -Dspring.profiles.active 이외에도 , -Dinstance_id=inst1과 같이 System property로 추가 가능

 


  • apply:

1. Xml

<!-- logback.xml -->

<if condition='"${spring.profiles.active}".contains("local")'>
    <then>
    	<property name="RUNTIME_MODE" value="local"/>
    </then>
</if>

#{systemProperties['spring.profiles.active']}와 같이 사용 가능

 


2.Java

String basePath = System.getProperty("basePath");
String profile = System.getProperty("spring.profiles.active");

File f = new File(basePath, "/" + profile + "/config.properties");

※ -DbasePath=/usr/local/conf 처럼 system의 proeprty로 -D옵션을 통해 key=value 형태로 runtime 시에 설정값 호출

 


 

728x90