如果使用Maven,通常将log4j.properties放在Java或资源下?

If using maven, usually you put log4j.properties under java or resources?

在使用常规Maven目录时,应该将log4j.properties文件放在哪里?


src/main/resources是它的"标准位置"。

更新:上面的内容回答了这个问题,但这并不是最佳的解决方案。查看其他答案和对此的评论...您可能不会将自己的日志记录属性与jar一起交付,而是将其留给客户端(例如,app-server,舞台环境等)来配置所需的日志记录。因此,将其放入src/test/resources是我的首选解决方案。

注意:说到将具体的日志配置留给客户端/用户,您应该考虑在应用程序中用slf4j替换log4j


只需将其放入src/main/resources即可将其捆绑在工件中。例如。如果工件是JAR,则将在其中包含log4j.properties文件,从而失去了使日志可配置的最初目的。

我通常将其放在src/main/resources中,并将其设置为像这样输出到目标:

1
2
3
4
5
6
7
8
9
10
11
<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <targetPath>${project.build.directory}</targetPath>
            <includes>
                <include>log4j.properties</include>
            </includes>
        </resource>
    </resources>
</build>

此外,为了使log4j实际看到它,您必须将输出目录添加到类路径。
如果您的工件是可执行的JAR,则可能使用了maven-assembly-plugin来创建它。在该插件内部,您可以通过添加Class-Path清单条目,将JAR的当前文件夹添加到类路径,如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<plugin>
    maven-assembly-plugin</artifactId>
    <configuration>
       
            <manifest>
                <mainClass>com.your-package.Main</mainClass>
            </manifest>
            <manifestEntries>
                <Class-Path>.</Class-Path>
            </manifestEntries>
        </archive>
        <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
    </configuration>
    <executions>
        <execution>
            <id>make-assembly</id> <!-- this is used for inheritance merges -->
            <phase>package</phase> <!-- bind to the packaging phase -->
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
</plugin>

现在,log4j.properties文件将位于您的JAR文件旁边,可以独立配置。

要直接从Eclipse运行应用程序,请将resources目录添加到运行配置中的类路径中:Run->Run Configurations...->Java Application->New选择Classpath选项卡,选择Advanced并浏览到src/resources目录。 >


一些"数据挖掘"说明src/main/resources是典型的位置。

Google代码搜索的结果:

  • src/main/resources/log4j.properties:4877
  • src/main/java/log4j.properties:215


用于初始化项目的资源最好放在src / main / resources文件夹中。为了在构建过程中加载这些资源,可以简单地在maven项目的pom.xml中添加条目作为构建资源

1
2
3
4
5
6
7
8
<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
    </resources>
</build>

其他.properties文件也可以保留在此文件夹中,用于初始化。
如果要在资源文件夹的属性文件中包含一些变量,并从配置文件过滤器属性文件中填充变量,则将过滤设置为true,这些文件保存在设置为配置文件的src / main / filters中,但完全是另一种用例。现在,您可以忽略它们。

这是一个很棒的资源maven资源插件,它很有用,也可以浏览其他部分。


将资源文件放置在另一个位置不是最佳解决方案时,可以使用:

1
2
3
4
5
6
7
8
9
10
<build>
  <resources>
    <resource>
      <directory>src/main/java</directory>
      <excludes>
        <exclude>**/*.java</exclude>
      </excludes>
    </resource>
  </resources>
<build>

例如,当资源文件(例如jaxb.properties)与Java类一起深入包中时。


如果在src / main / resources下找不到您的log4j.properties或log4j.xml文件,请使用此PropertyConfigurator.configure(" log4j.xml");

1
2
3
   PropertyConfigurator.configure("log4j.xml");
   Logger logger = LoggerFactory.getLogger(MyClass.class);
   logger.error(message);

在pom.xml的build标记内,从resources标记中添加以下代码。
因此,这意味着资源标签必须位于pom.xml

中的构建标签内

1
2
3
4
5
6
7
8
<build>
    <resources>
        <resource>
            <directory>src/main/java/resources</directory>
                <filtering>true</filtering>
         </resource>
     </resources>
<build/>