【问题标题】:How filter value of maven profile in log4j2.xml with SpringBoot如何使用 Spring Boot 在 log4j2.xml 中过滤 maven 配置文件的值
【发布时间】:2018-03-20 20:05:39
【问题描述】:

我需要通过 maven 在我的 log4j2.xml 中添加一个环境变量,但是在 spring boot 项目中。我有一定的困难,但我做到了。 =)

以前的代码是怎样的。

log4j2.xml

<PatternLayout>
     <Pattern>${ambiente} - %d [%-6p] %c - %M - %m%n</Pattern>
</PatternLayout>

pom.xml

<profiles>
    <profile>
        <id>dev</id>
        <activation>
            <property>
                <name>env</name>
                <value>dev</value>
            </property>
        </activation>
        <properties>
            <ambiente>desenvolvimento</ambiente>
        </properties>
    </profile>
    <profile>
        <id>hom</id>
        <activation>
            <property>
                <name>env</name>
                <value>hom</value>
            </property>
        </activation>
        <properties>
            <ambiente>homologacao</ambiente>
        </properties>
    </profile>
    <profile>
        <id>prod</id>
        <activation>
            <property>
                <name>env</name>
                <value>prod</value>
            </property>
        </activation>
        <properties>
            <ambiente>producao</ambiente>
        </properties>
    </profile>
</profiles>
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

还有运行命令:clean install -Denv=dev

没用=(

<PatternLayout>
    <Pattern>${ambiente} - %d [%-6p] %c - %M - %m%n</Pattern>
</PatternLayout>

【问题讨论】:

    标签: maven spring-boot


    【解决方案1】:

    在尝试了各种选择之后,我做到了。

    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
    

    然后,将 ${ambiente} 更改为 @ambiente@

    <PatternLayout>
        <Pattern>@ambiente@ - %d [%-6p] %c - %M - %m%n</Pattern>
    </PatternLayout>
    

    用同样的命令再次运行clean install -Denv=dev

    现在我的 log4j2.xml 看起来很棒 =)。

    <PatternLayout>
        <Pattern>desenvolvimento - %d [%-6p] %c - %M - %m%n</Pattern>
    </PatternLayout>
    

    还有我的日志文件

    desenvolvimento - 2018-03-20 17:08:37,678 [ERROR ]
    

    希望对您有所帮助!

    【讨论】:

      猜你喜欢
      • 2018-10-13
      • 2017-09-07
      • 2020-12-10
      • 2019-11-03
      • 2018-06-12
      • 2014-10-19
      • 1970-01-01
      • 2018-06-14
      • 2020-01-11
      相关资源
      最近更新 更多