SpringBoot應用與原理之項目打包方案

一 本章概述

介紹SpringBoot應用基於Maven實現不同環境的應用打包

二 SpringBoot應用介紹

默認情況下SpringBoot應用都會有一個默認的配置文件application.properties或者是application.yml,
而開發人員在測試部署項目時,通常要切換到不同的環境,而不同的環境會存在不同的配置,例如數據庫,redis等等。
這裏就可以藉助SpringBoot提供的Profile來實現。

2.1 環境規劃

環境名稱 環境說明
dev 開發環境
fat 測試環境
uat 集成環境
pro 生產環境

2.2 SpringBoot應用配置

在明確環境規劃之後準備5個配置文件,分別是application.properties,application-dev.properties,application-fat.properties,application-uat.properties,application-pro.properties。文件的配置內容如下
application.properties
profiles.active是pom.xml中配置profile的變量,用於替換激活的環境

spring.profiles.active=@profiles.active@

application-dev.properties
設定tomcat服務器的端口,開發環境爲8099

server.port=8099

application-fat.properties
設定tomcat服務器的端口,測試環境爲8089

server.port=8089

application-uat.properties
設定tomcat服務器的端口,集成環境爲8079

server.port=8079

application-pro.properties
設定tomcat服務器的端口,生產環境爲80

server.port=80

application.properties文件通常用於配置通用的屬性,也就是不同環境下的配置項是相同的.例如server.tomcat.uri-encoding=utf-8
而爲了區分,這裏不同環境的配置採用不同的端口。

2.3 maven打包設置

pom文件中主要針對不同的環境設置對應的profile,其中默認激活的是dev環境的配置

<profiles>

        <!-- 定義四個不同環境,分別是開發環境dev,測試環境fat,集成環境uat,生產環境pro-->
        <profile>
            <id>dev</id>
            <properties>
                <profiles.active>dev</profiles.active>
            </properties>

            <!-- 默認激活開發環境-->
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>


        <profile>
            <id>fat</id>
            <properties>
                <profiles.active>fat</profiles.active>
            </properties>

        </profile>


        <profile>
            <id>uat</id>
            <properties>
                <profiles.active>uat</profiles.active>
            </properties>


        </profile>

        <profile>
            <id>pro</id>
            <properties>
                <profiles.active>pro</profiles.active>
            </properties>
        </profile>


    </profiles>

然後配置需要包含和過濾的application.properties

<build>
        <resources>
            <!-- 指定打包時需要過濾的文件-->
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
                <excludes>
                    <exclude>application-dev.properties</exclude>
                    <exclude>application-fat.properties</exclude>
                    <exclude>application-uat.properties</exclude>
                    <exclude>application-pro.properties</exclude>
                </excludes>


            </resource>

            <!-- 指定打包時需要包含的配置文件-->
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
                <includes>
                    <!--${profiles.active}會根據指定的profile動態替換-->
                    <include>application-${profiles.active}.properties</include>
                    <include>application.properties</include>
                </includes>
            </resource>
        </resources>

    </build>

然後使用maven的打包命令實現不同環境打包即可

MacBookPro:springboot tony$ mvn clean package -Dmaven.test.skip=true -P fat

使用 mvn clean package -Dmaven.test.skip=true -P fat表示打包項目,環境爲fat。
-P後面的選項可以是之前配置的profile,例如dev,fat,uat,pro。
當打包完成之後解壓打包的文件會發現BOOT-INF/class文件下會存在application.properties和@profiles.active@值對應配置,例如application-fat.properties

重要說明:
移動終端更好的閱讀方案,關注個人微信公衆號: ittimeline,CSDN文章內容會實時同步到該公衆號中。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章