springboot創建多環境profile打包


springboot開發打包時,一般會有多個環境,dev,qa,prod等,配置文件大多雷同,只是方便開發切換,但是生成部署時產生的war包就無需這么多重復配置了,這時這些dev,qa的配置就不應該打入war包,這時就得用到profile屬性

1、pom文件中添加profile節點,並在build下的resources節點添加打包過濾的配置文件規則

    <profiles>
        <profile>
            <id>dev</id>
            <properties>
                <profileActive>dev</profileActive>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <id>local</id>
            <properties>
                <profileActive>local</profileActive>
            </properties>
        </profile>
        <profile>
            <id>qa</id>
            <properties>
                <profileActive>qa</profileActive>
            </properties>
        </profile>
        <profile>
            <id>sdrmyy</id>
            <properties>
                <profileActive>sdrmyy</profileActive>
            </properties>
        </profile>
        <profile>
            <id>show</id>
            <properties>
                <profileActive>show</profileActive>
            </properties>
        </profile>
    </profiles>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
            </plugin>
            <!-- maven打包插件 end -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <configuration>
                    <warName>oip_hosp</warName>
                </configuration>
            </plugin>
        </plugins>
        <resources>
            <resource>
                <directory>src/main/webapp</directory>
            </resource>
            <resource> <directory>src/main/resources</directory> <includes> <include>application-${profileActive}.yml</include> <include>application.yml</include> <include>**/*.xml</include> </includes> <filtering>true</filtering> </resource>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
        </resources>
    </build>

此節點需與配置文件后綴一一對應,如-dev

打包過濾配置文件規則也是用一個占位符進行占位,打包時也會通過maven傳入參數進行替換

2、在主application.yml中配置一個動態屬性進行占位,默認的分隔符是@屬性名@,這個屬性會通過maven打包時傳入參數進行替換

3、maven打包時可以通過兩種方式傳入參數:

a)、通過 -D命令傳入屬性值profileActive,如:

clean install -Dmaven.test.skip=true -DprofileActive=dev

b)、通過-P命令指定profile環境,如:

clean package -P prod

或者直接通過插件可視化操作:

如果是idea的話,直接在右側maven工具欄中勾選更方便:

4、本機運行時,一般通過指定profile屬性,如

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM