spring boot 프로필 시작 을 어떻게 지정 합 니까?

spring boot 프로젝트 는 서로 다른 환경 에 해당 하 는 프로필 을 설정 할 수 있 습 니 다.
다음 그림 에서 보 듯 이:

pom.xml 설정 은 다음 과 같 습 니 다.

<dependencies>
        
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.46</version>
        <scope>runtime</scope>
    </dependency>
    <!--   druid   -->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid</artifactId>
        <version>1.1.12</version>
    </dependency>
</dependencies>     
 
<!--     profile-->
    <profiles>
        <profile>
            <id>dev</id>
            <properties>
                <!--  ${environment}   -->
                <environment>dev</environment>
            </properties>
        </profile>
        <profile>
            <id>test</id>
            <properties>
                <environment>test</environment>
            </properties>
        </profile>
        <profile>
            <id>prod</id>
            <properties>
                <environment>prod</environment>
            </properties>
        </profile>
    </profiles>
 
    <build>
        <finalName>spring-boot-lean-${environment}</finalName> 
        <resources> 
            <!--        -->
            <resource>
                <directory>src/main/resources</directory>
                <excludes>
                    <exclude>application-*.yml</exclude>
                </excludes>
            </resource>
 
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
                <!--          -->
                <includes>
                    <include>application-${environment}.yml</include>
                </includes>
            </resource>
        </resources> 
 
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
로 컬 개발 사용 개발 환경,아이디어 시작 개발 환경 설정 은 다음 과 같 습 니 다.
1.설정 편집 을 클릭 합 니 다

2.아래 그림 과 같이 설정

3.프로젝트 시작

콘 솔 은 application-dev.yml 에서 설정 한 변 수 를 인쇄 했 습 니 다.
개발 할 때 하나의 프로젝트 가 여러 개의 인 스 턴 스 를 시작 해 야 하 는 장면 도 있 습 니 다.아 이 디 어 는 하나의 spring boot 프로젝트 가 여러 개의 인 스 턴 스 를 시작 하 는 것 을 지원 합 니 다.
방법 은 매우 간단 합 니 다.위의 튜 토리 얼 에 따라 아이디어 에 시작 설정 을 새로 만 들 고 Active profiles 를 prod 로 지정 하면 됩 니 다.다음 그림:

다음 그림 을 통 해 로 컬 에서 여러 개의 spring boot 인 스 턴 스 를 시작 할 수 있 습 니 다.

다 중 환경 패키지
1.maven 포장 명령 실행:
포장 테스트:

mvn clean package -D maven.test.skip=true -P test
이렇게 전 화 된 가방 에는 yml 파일 만 포 함 됩 니 다:application.yml,application-test.yml
패키지 prod:

mvn clean package -D maven.test.skip=true -P test
이렇게 걸 려 있 는 가방 에는 yml 파일 만 포 함 됩 니 다:application.yml,application-prod.yml

2、jar 패키지 실행 찾기
java-jar 이름.jar--spring.profiles.active=prod
테스트 환경의 가방 이 나 오 면 실행:
java-jar 이름.jar--spring.profiles.active=test
보충 점
mvn clean package-D maven.test.skip=true-P test 를 실행 합 니 다.target 디 렉 터 리 에는 application.yml,application-test.yml 만 있 습 니 다.이때 idea 를 사용 하여 프로젝트 를 시작 할 때 dev 설정 을 사용 할 수 없습니다.target 에 application-dev.yml 이 없 기 때 문 입 니 다.
target 을 삭제 한 후 프로젝트 를 다시 시작 해 야 합 니 다.이때 target 에 모든 프로필 이 있 고 dev 설정 을 사용 할 수 있 습 니 다.
이상 은 개인 적 인 경험 이 므 로 여러분 에 게 참고 가 되 기 를 바 랍 니 다.여러분 들 도 저 희 를 많이 응원 해 주시 기 바 랍 니 다.

좋은 웹페이지 즐겨찾기