maven 打包时动态替换properties,xml资源文件中的配置值

背景:

最近在开发过程中,发现 logback.xml 中的变量 maven 编译的时候不能被替换。

环境:

maven version: 3.3.9

springboot version: 2.1.9.RELEASE

 

 logback.xml 内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <include resource="org/springframework/boot/logging/logback/defaults.xml"/>
    <appender name="dailyRollingFile" class="ch.qos.logback.core.rolling.RollingFileAppender">

        <File>@log.path@/app.log</File>
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <!-- daily rollover -->
            <fileNamePattern>@log.path@/app.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
            <maxHistory>30</maxHistory>
            <maxFileSize>50MB</maxFileSize>
        </rollingPolicy>
        <encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
            <layout class="org.apache.skywalking.apm.toolkit.log.logback.v1.x.TraceIdPatternLogbackLayout">
                <pattern>%d{HH:mm:ss.SSS} [%tid] @app.name@ [%thread] %-5level %logger{35} - %msg %n</pattern>
            </layout>
        </encoder>
    </appender>

    <appender name="consoleRolling" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%X{traceId}/%X{spanId}] @app.name@ [%thread] %-5level %logger{35} - %msg %n</pattern>
            <charset>utf8</charset>
        </encoder>
    </appender>

    <root level="info">
        <appender-ref ref="dailyRollingFile"/>
        <appender-ref ref="consoleRolling"/>
    </root>
</configuration>

执行 maven 编译之后,发现 编译后 变量并没有被替换。

解决方案:

pom build节点下面添加resource配置:

         
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
        

通过下面配置的 profile ,重新编译,变量被替换了。

<profiles>
        <profile>
            <id>dev</id>
            <properties>
                <log.path>D:/log<log.path/>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
 
        <profile>
            <id>prod</id>
            <properties>
                <log.path>/opt/log<log.path/>
            </properties>
        </profile>
    </profiles>

resource 草草吃下:

  • filtering 属性用来表示资源文件中的占位符是否需要被替换,true为需要替换。
  • include 用来设置匹配规则

下面是一个示例:

所有的.properties文件中的EL表达式占位符都会在打包时动态替换,所有的.yml文件则不会替换占位符。

        <resources>
            <resource>
                <directory>src/main/resources/</directory>
                <filtering>true</filtering>
                <includes>
                    <include>**/*.properties</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/resources/</directory>
                <filtering>false</filtering>
                <includes>
                    <include>**/*.yml</include>
                </includes>
            </resource>
        </resources>


maven 占位符

请看如下链接:

https://maven.apache.org/plugins/maven-resources-plugin/resources-mojo.html

 

<delimiters>
  <delimiter>${*}</delimiter>
  <delimiter>@</delimiter>
</delimiters>

参考:

https://blog.csdn.net/xiao_jun_0820/article/details/49864285

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
spring-boot-maven-plugin 是一个 Maven 插件,它可以将 Spring Boot 应用程序打包成可执行的 JAR 文件,同也可以将应用程序的依赖项打包成内置的 JAR 文件。这个插件还有一个很棒的功能,就是它可以将应用程序的配置文件打包到 JAR 包之外,这样在应用程序升级,可以直接替换现有的 JAR 文件而不需要担心会覆盖配置文件。 在默认的情况下,Spring Boot 将 application.properties 或者 application.yml 文件打包到 JAR 包的根目录下,这种默认的方式并不是最好的,因为在应用程序升级配置文件会被一起打包进去,这样会产生一些困扰,比如你需要手动备份这些配置文件。 为了解决这个问题,spring-boot-maven-plugin 插件提供了一个很好的解决方案,就是将配置文件打包到 JAR 包之外,具体的做法是在 pom.xml 文件添加如下配置: ```xml <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <classifier>exec</classifier> </configuration> <executions> <execution> <id>repackage</id> <goals> <goal>repackage</goal> </goals> <configuration> <classifier>exec</classifier> <compress>false</compress> <shutdown>graceful</shutdown> <finalName>${project.artifactId}-${project.version}-exec</finalName> </configuration> </execution> </executions> </plugin> </plugins> </build> ``` 这个配置添加了一个 classifier 属性,它会创建一个带有 exec 后缀的 JAR 包,该 JAR 包除了包含应用程序的类文件和依赖项,还会包含配置文件和其他静态资源文件。在 pom.xml 文件加入以上配置,并在应用程序的根目录下创建一个名为 config 的目录,将配置文件放置在该目录,然后重新编译和打包应用程序,你将会得到一个新的 JAR 文件,该文件会在根目录下生成一个名为 config 的文件夹,其包含你的配置文件。这样就实现了将配置文件打包到 JAR 包之外的目的。 总结一下,spring-boot-maven-plugin 插件提供了一个将应用程序的配置文件打包到 JAR 包之外的方式,只需要在 pom.xml 文件配置一个 classifier 属性即可实现。这样可以避免在应用程序升级面临配置文件被覆盖的问题,也便于组织和管理配置文件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值