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

发布了48 篇原创文章 · 获赞 4 · 访问量 8万+

猜你喜欢

转载自blog.csdn.net/H_Rhui/article/details/102825409