springboot maven项目多环境profile配置,打包后配置文件properties或yml中的${}或@@变量不替换问题

maven一些配置如下:

项目中配置了多环境

    <profiles>
<!--        数据库类型-->
        <profile>
            <!-- mysql版 默认 -->
            <id>mysql</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <dbType>mysql</dbType>
                <skipAssemblyMysql>false</skipAssemblyMysql>
                <skipAssemblyOracle>true</skipAssemblyOracle>
            </properties>
        </profile>
        <profile>
            <!-- oracle版 -->
            <id>oracle</id>
            <properties>
                <dbType>oracle</dbType>
                <skipAssemblyMysql>true</skipAssemblyMysql>
                <skipAssemblyOracle>false</skipAssemblyOracle>
            </properties>
        </profile>
<!--        激活的环境-->
        <!-- 开发环境 默认 -->
        <profile>
            <id>dev</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <env>dev</env>
            </properties>
        </profile>
        <!-- 测试环境 -->
        <profile>
            <id>test</id>
            <properties>
                <env>test</env>
            </properties>
        </profile>
        <!-- 生产环境 -->
        <profile>
            <id>prod</id>
            <properties>
                <env>prod</env>
            </properties>
        </profile>
    </profiles>

 开启了filtering和可以使用占位符${}

<resource>
    <directory>src/main/resources</directory>
    <filtering>true</filtering>
</resource>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-resources-plugin</artifactId>
    <configuration>
        <encoding>UTF-8</encoding>
        <!-- 加入这行配置可以在配置文件中使用${env}代替@env@ -->
        <useDefaultDelimiters>true</useDefaultDelimiters> 
    </configuration>
</plugin>

接下来是重点,一般来说,不做特殊配置,配置文件会打包进jar包中,可以先clean install看看最后的打包目录结构。之前项目的配置文件和代码都是打进一个jar包的,这个项目最后是打包成了一个zip且配置文件与运行jar包分离

所有文件打包到jar包中的结构

 本项目最终打包的zip结构

问题来了,打包成zip后application.properties中的spring.profiles.active依旧为图中所示,按道理应该为spring.profiles.active=dev,mysql

 但其实在classes文件中变量已经被成功替换了

那么我换一下思路,最终打包到zip中的配置文件我从target/classes这边拿而不从src/main/resources中拿,于是找到了项目中配置打包到zip的部分修改fileset,修改代码如下,最终解决问题

<!-- 从classes中拿已经替换过的配置文件 -->
<fileSet>
    <directory>${project.basedir}/target/classes</directory>
    <outputDirectory>/</outputDirectory>
    <includes>
        <include>application.properties</include>
        <include>application-${env}.properties</include>
    </includes>
</fileSet>

 

 各项目的代码不一定通用,这边贴的代码只是参考,需要自行搞清楚项目的打包配置再调整即可,希望可以帮到大家

  • 5
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值