【问题标题】:Using diferent log4j2.xml per maven profile每个 Maven 配置文件使用不同的 log4j2.xml
【发布时间】:2020-12-10 02:05:18
【问题描述】:

使用 maven 和 springboot。我想为每个环境(dev、cert)使用不同的文件配置(log4j2.xml、application.properties)。我在我的 pom.xml 文件上创建了两个配置文件,我在 src/main/resources 中有两个文件夹,每个文件配置(dev 和 cert)。我想根据我使用的配置文件包含这些文件。我在目录中有其他文件,所以我不想使用这种方式来创建两个路由和重复文件,我只想包含 src/main/resources 中的所有内容以及目录 dev 或 cert 中的文件。

我尝试使用:

<plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.3.1</version>
                <configuration>
                    <packagingIncludes>src/main/resources/{resources.directory}</packagingIncludes>
                </configuration>
            </plugin>

但我得到了

[ERROR] 未能执行目标 org.springframework.boot:spring-boot-maven-plugin:1.4.3.RELEASE:repackage (默认)在项目 NProWebApp 上:目标的执行默认值 org.springframework.boot:spring-boot-maven-plugin:1.4.3.RELEASE:repackage 失败:找不到主类 -> [帮助 1]

编辑: 在 Maven 中添加配置文件:

<profiles>
    <profile>
        <id>dev</id>
        <properties>
            <packaging.type>jar</packaging.type>
        </properties>
    </profile>
    <profile>
        <id>cert</id>
        <properties>
            <packaging.type>war</packaging.type>
        </properties>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
                <scope>provided</scope>
            </dependency>
        </dependencies>
    </profile>
</profiles>

【问题讨论】:

    标签: spring-boot maven configuration log4j


    【解决方案1】:

    您无需对 Maven 进行任何操作。只需将所有文件打包在一起,并创建格式为 application-{profile}.properties 的 spring 配置文件。然后,在该文件中使用 logging.config=log4j2-{profile}.xml 定义您的日志记录配置位置,其中 {profile} 是当前文件的配置文件名称。

    请注意,在 yml 中,您还可以将多个配置文件配置放在一个 application.yml 文件中。

    【讨论】:

    • 嗨,你的解决方案听起来不错,但是当我创建战争时,它包含 application.properties(dev 和 cert)和 log4j.xml,我认为为了说maven 需要什么文件,对吗?我将编辑问题以添加有关个人资料的信息
    猜你喜欢
    • 1970-01-01
    • 2012-10-25
    • 2012-11-19
    • 1970-01-01
    • 2018-08-11
    • 1970-01-01
    • 1970-01-01
    • 2014-10-18
    • 1970-01-01
    相关资源
    最近更新 更多