springboot创建多环境profile打包

1.  https://www.cnblogs.com/javabg/p/10058753.html

 

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属性,如

 

SpringBoot中,你可以使用不同的profile配置来打包生产环境。首先,你需要在项目中创建一个专门用于生产环境的配置文件。可以按照以下步骤进行操作: 1. 在项目中新建一个名为"application-prod.yml"的配置文件,用于配置生产环境的属性。你可以在这个文件中设置与生产环境相关的属性,例如数据库连接、日志级别等。 2. 修改"application.yml"文件,指定默认的配置文件为"application-prod.yml"。可以通过添加以下内容来实现: ```yaml spring: profiles: active: prod ``` 3. 在命令行中使用"Maven"进行打包,同时指定profile为"prod"。可以使用以下命令: ```bash mvn clean package -Dspring.profiles.active=prod ``` 这将根据"application-prod.yml"中的配置进行打包。 4. 打包成功后,你将得到一个针对生产环境的jar。你可以在目标文件夹中找到这个jar,名称可能类似于"springboot-demo1-1.0.0-SNAPSHOT.jar"。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [SpringBoot打包测试、生产环境](https://blog.csdn.net/qq_23215957/article/details/103574923)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [Springboot的开发环境(dev)、测试环境(beta)和生产环境product)打包部署](https://blog.csdn.net/u011652364/article/details/109282399)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值