Programing

IntelliJ에서 실행할 때 Spring Boot 프로필을 어떻게 활성화합니까?

lottogame 2020. 8. 21. 08:23
반응형

IntelliJ에서 실행할 때 Spring Boot 프로필을 어떻게 활성화합니까?


5 가지 환경이 있습니다.

 - local (my development machine)
 - dev
 - qc
 - uat
 - live
 - staging

각 환경에 대해 서로 다른 응용 프로그램 속성을 사용하기를 원하므로 데이터 소스에 대해 서로 다른 URL을 가진 다음 속성 파일이 있습니다.

 - application.properties  (containing common properties)
 - application-local.properties
 - application-dev.properties
 - application-qc.properties
 - application-uat.properties
 - application-live.properties

IntelliJ를 사용하고 로컬 컴퓨터의 Gradle 플러그인에서 bootRun을 사용하여 내 앱을 실행하고 있습니다. Tomcat을 실행하는 다른 모든 환경에서 동일한 응용 프로그램 war 파일을 배포 할 것입니다.

나는 추가를 시도했다 :

--spring.profiles.active = local

스크립트 매개 변수에서 실행 구성에 추가합니다.

나는 추가를 시도했다

-Dspring.profiles.active = local

VM 옵션 아래의 실행 구성에.

둘 다 작동하지 않습니다. 시작할 때 INFO 메시지가 계속 표시됩니다. 활성 프로필이 설정되지 않았습니다. 기본 프로필로 돌아갑니다. 기본값

Windows 명령 줄에서 내 앱을 실행하면

gradle bootRun

하지만 먼저 환경 변수를 설정했습니다.

set SPRING_PROFILES_ACTIVE=local

그러면 모든 것이 작동합니다.

제 질문은 IntelliJ에서 bootRun을 실행할 때 로컬 스프링 부트 프로필을 어떻게 활성화합니까?


-Dspring.profiles.active=testVM 옵션에 추가 한 다음 해당 구성을 다시 실행했습니다. 완벽하게 작동했습니다.

이것은 다음과 같이 설정할 수 있습니다.

  • 고르는 Run | Edit Configurations...
  • Configuration탭으로 이동
  • Environment표시 섹션 확장VM options

실제로 스프링 부트 실행 구성을 사용하는 경우 (현재 Ultimate Edition에서만 지원됨) "활성 프로필"설정에서 프로필을 쉽게 사전 구성 할 수 있습니다.

여기에 이미지 설명 입력


내 build.gradle에 다음을 추가했습니다.

bootRun {
  environment SPRING_PROFILES_ACTIVE: environment.SPRING_PROFILES_ACTIVE ?: "local"
}

test {
  environment SPRING_PROFILES_ACTIVE: environment.SPRING_PROFILES_ACTIVE ?: "test"
}

이제 IntelliJ에서 bootRun을 실행할 때 기본값은 "로컬"프로필입니다.

다른 환경에서는 Tomcat에서 'SPRING_PROFILES_ACTIVE'환경 변수를 간단히 설정합니다.

https://github.com/spring-projects/spring-boot/pull/592 에서 찾은 주석에서 이것을 얻었습니다.


가능한 원인은 명령 줄 매개 변수를 응용 프로그램 기본 메서드에 전달하지 않았기 때문일 수 있습니다. 몇 주 전에 같은 실수를 저질렀습니다.

public static final void main(String... args) {
    SpringApplication.run(Application.class, args);
}

build.gradle에이 명령을 추가해보십시오.

여기에 이미지 설명 입력

따라서 실행하려면 해당 모양을 구성하십시오.

여기에 이미지 설명 입력


Intellij Community Edition을 사용합니다. "실행 / 디버그 구성"> 실행기 탭> 환경 변수> "..."버튼 클릭으로 이동합니다. 추가 : SPRING_PROFILES_ACTIVE = 로컬

spring.profiles.active


Spring Boot는 진화함에 따라 VM 옵션을 읽는 방식을 변경 한 것 같습니다. Intellij에서 응용 프로그램을 시작하고 일부 프로필을 활성화하려는 경우 시도 할 수있는 몇 가지 방법은 다음과 같습니다.

1. VM 옵션 변경

"실행"에서 "구성 편집"을 열고 "VM 옵션"에서 다음을 추가합니다. -Dspring.profiles.active=local

It actually works with one project of mine with Spring Boot v2.0.3.RELEASE and Spring v5.0.7.RELEASE, but not with another project with Spring Boot v2.1.1.RELEASE and Spring v5.1.3.RELEASE.

Also, when running with Maven or JAR, people mentioned this:

mvn spring-boot:run -Drun.profiles=dev

or

java -jar -Dspring.profiles.active=dev XXX.jar

(See here: how to use Spring Boot profiles)

2. Passing JVM args

It is mentioned somewhere, that Spring changes the way of launching the process of applications if you specify some JVM options; it forks another process and will not pass the arg it received so this does not work. The only way to pass args to it, is:

mvn spring-boot:run -Dspring-boot.run.jvmArguments="..."

Again, this is for Maven. https://docs.spring.io/spring-boot/docs/current/maven-plugin/examples/run-debug.html

3. Setting (application) env var

두 번째 프로젝트에서 저에게 효과적인 것은 위의 일부 답변에서 언급했듯이 환경 변수를 설정하는 것입니다. "구성 편집"- "환경 변수"및 :

spring.profiles.active=local

이 시도. 다음과 같이 build.gradle 파일을 편집하십시오.

ext { profile = project.hasProperty('profile') ? project['profile'] : 'local' }

들어 봄 부팅 2.1.0 이상 당신은 사용할 수 있습니다

mvn spring-boot:run -Dspring-boot.run.profiles=foo,bar


프로필 이름을 BE로 바꿉니다.

위의 방법으로 프로필을 활성화 할 수 있습니다.


-Dspring.profiles.active=local프로그램 인수에서 설정합니다 .

참고 URL : https://stackoverflow.com/questions/39738901/how-do-i-activate-a-spring-boot-profile-when-running-from-intellij

반응형