简体   繁体   English

maven 在哪里搜索 log4j.properties 文件?

[英]Where does maven search for log4j.properties file?

I am facing issues using log4j with Maven.我在 Maven 中使用 log4j 时遇到问题。 I've one properties file ie log4j.properties and I've put that file at the same path where project's pom.xml is stored.我有一个属性文件,即log4j.properties ,我将该文件放在存储项目pom.xml的同一路径中。

pom.xml pom.xml

    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
        <scope>test</scope>
</dependency>

I've used log4j in my code which is under test folder.我在test文件夹下的代码中使用了 log4j。

Code代码

package com.example.automationtest;

import org.apache.log4j.*;
import org.junit.Test;

public class AppTest
{
    @Test
    public void testLogger(){
   //PropertyConfigurator.configure("C:\\Users\\Desktop\\AutomationTest\\log4j.properties");
    Logger log = Logger.getLogger("exampleLogger");
    log.debug("Hello, World!");
   }
}

I would like to know, how does maven identify where the log4j.properties file is located?我想知道,maven 如何识别 log4j.properties 文件所在的位置?

In the above scenario if I run the command mvn test it gives a warning, please check the screenshot below for warning messages.在上面的场景中,如果我运行命令mvn test它会给出警告,请查看下面的屏幕截图以获取警告消息。

在此处输入图片说明

so as a workaround I am providing the complete path of log4j.properties in my code.所以作为一种解决方法,我在我的代码中提供了 log4j.properties 的完整路径。 I've used below line:我使用了以下行:

PropertyConfigurator.configure("C:\\Users\\Desktop\\AutomationTest\\log4j.properties")

Is it necessary to use the above line of code, or is there any specific directory where maven looks for log4j.properties file?是否有必要使用上面的代码行,或者maven查找log4j.properties文件是否有任何特定目录?

The file needs to go into src/main/resources/ or src/test/resources/ (if you need it only for unit tests).该文件需要进入src/main/resources/src/test/resources/ (如果您只需要它用于单元测试)。

Longer explanation: Maven splits the Java classpath into source code and resources (non-Java things like property files, images, etc).更长的解释:Maven 将 Java 类路径拆分为源代码和资源(非 Java 事物,如属性文件、图像等)。 The main reason for this is that Maven can do filtering of resources for you and to make this extra safe, they put the resources into a different folder than the sources.这样做的主要原因是 Maven 可以为您过滤资源并使其更加安全,他们将资源放入与源不同的文件夹中。

There's a lot of answers relating to the popular solution src/main/resources or the src/test/resources but generally not having the log4j.properties in your application can be useful.有很多与流行的解决方案src/main/resourcessrc/test/resources相关的答案,但通常在您的应用程序中没有 log4j.properties 可能很有用。 Rather than providing a hardcoded log4j.properties in your code, leave it to the client (app-server, stage environment, etc) to configure the desired logging.与其在代码中提供硬编码的log4j.properties ,不如将其留给客户端(应用程序服务器、舞台环境等)来配置所需的日志记录。 So, I would suggest to be cautious on the specifics of your deployment to make the decision of having the log4j.properties included or excluded of your maven assembly.因此,我建议您对部署的细节保持谨慎,以决定将log4j.properties包含在您的 maven 程序集中还是排除在外。

Reference: If using maven, usually you put log4j.properties under java or resources?参考: 如果使用maven,一般你把log4j.properties放在java或者resources下?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM