development

스프링 부트의 명령 줄에서 활성 프로파일 및 구성 위치 설정

big-blog 2020. 7. 10. 07:41
반응형

스프링 부트의 명령 줄에서 활성 프로파일 및 구성 위치 설정


스프링 부트 응용 프로그램이 있습니다.

내 응용 프로그램-> 개발, 준비 및 프로덕션에 세 가지 프로필이 있습니다. 3 개의 파일이 있습니다

  1. application-development.yml
  2. application-staging.yml
  3. application-production.yml

application.yml 은 안에 src/main/resources있습니다. application.yml에서 활성 프로파일을 다음과 같이 설정했습니다.

spring:
  profiles.active: development

다른 3 개의 프로파일 특정 구성 파일은 C:\config폴더에 있습니다.

일식에 gradle 플러그인을 사용하고 있습니다. " bootRun "을 시도 할 때 , gradle 구성에서 명령 줄 인수를 일식으로 설정합니다.

 -Dspring.profiles.active=staging -Dspring.config.location=C:\Config

그러나 명령 줄 속성이 반영되지 않고 활성 프로필이 항상 개발로 설정됩니다 (applications.yml 파일에서 언급 한 것). 또한 C : \ Config 폴더는 프로파일 특정 구성 파일을 검색하지 않습니다.

여기에 뭔가 빠진 것 같습니다. 지난 2 일 동안 알아 내려고 노력했습니다. 그러나 운이 없다. 정말 도움을 주셔서 감사합니다.


명령 행에서 스프링 특성을 추가 / 재정의 할 수있는 두 가지 방법이 있습니다.

옵션 1 : Java 시스템 특성 (VM 인수)

-D 매개 변수는 application.jar 앞에 있어야합니다. 그렇지 않으면 인식되지 않습니다.

java -jar -Dspring.profiles.active=prod application.jar


옵션 2 : 프로그램 인수

java -jar application.jar --spring.profiles.active=prod --spring.config.location=c:\config

나는 이것을 추가해야했다 :

bootRun {
    String activeProfile =  System.properties['spring.profiles.active']
    String confLoc = System.properties['spring.config.location']
    systemProperty "spring.profiles.active", activeProfile
    systemProperty "spring.config.location", "file:$confLoc"
}

이제 bootRun은 프로필 및 구성 위치를 선택합니다.

포인터에 대해 @jst에게 감사드립니다.


-Dspring.profiles.active=staging -Dspring.config.location=C:\Config

정확하지 않습니다.

해야한다:

--spring.profiles.active=staging --spring.config.location=C:\Config

다음 명령 줄을 사용할 수 있습니다.

java -jar -Dspring.profiles.active=[yourProfileName] target/[yourJar].jar

Maven 플러그인통해 프로파일을 설정할 때run.jvmArguments

mvn spring-boot:run -Drun.jvmArguments="-Dspring.profiles.active=production"

디버그 옵션으로 :

mvn spring-boot:run -Drun.jvmArguments="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005 -Dspring.profiles.active=jpa"

나는이 여행을 많은 사람들을 보았습니다.


OS 변수 SPRING_PROFILES_ACTIVE를 설정하여 다른 방법이 있습니다 .

예를 들어 :

SPRING_PROFILES_ACTIVE=dev gradle clean bootRun

참조 : 활성 스프링 프로파일 설정 방법


최선의 방법은 이것을 VM "-D"인수로 정의하는 것입니다. 스프링 부트 1.x와 2.x의 차이점에 유의하십시오.

사용할 프로파일은 명령 행에서 지정할 수 있습니다.

스프링 부트 2.x

-Dspring-boot.run.profiles=local

스프링 부트 1.x

-Dspring.profiles.active=local

maven 사용 예 :

스프링 부트 2.x

mvn spring-boot:run -Dspring-boot.run.profiles=local

스프링 부트 1.x

mvn spring-boot:run -Dspring.profiles.active=local

여러 프로필의 경우 쉼표로 구분해야합니다.

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

문제가 spring.config.location과 관련이 있다고 생각하며 경로가 "/"로 끝나지 않는 것입니다.

문서 인용

If spring.config.location contains directories (as opposed to files) they should end in / (and will be appended with the names generated from spring.config.name before being loaded).

http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-external-config-application-property-files


Michael Yin's answer is correct but a better explanation seems to be required!

A lot of you mentioned that -D is the correct way to specify JVM parameters and you are absolutely right. But Michael is also right as mentioned in Spring Boot Profiles documentation.

What is not clear in the documentation, is what kind of parameter it is: --spring.profiles.active is a not a standard JVM parameter so if you want to use it in your IDE fill the correct fields (i.e. program arguments)

참고URL : https://stackoverflow.com/questions/31038250/setting-active-profile-and-config-location-from-command-line-in-spring-boot

반응형