use*_*949 126 java configuration log4j maven
在使用传统的Maven目录时,我应该在哪里放置log4j.properties文件?
Jan*_*ski 136
src/main/resources 是这个的"标准位置".
更新:上面回答了问题,但它不是最好的解决方案.查看其他答案和对此的评论......您可能不会使用jar发送自己的日志记录属性,而是将其留给客户端(例如app-server,stage environment等)来配置所需的日志记录.因此,将其放入src/test/resources是我的首选解决方案.
注:说到离开具体的日志配置到客户/用户,您应该考虑更换log4j与slf4j您的应用程序.
Zol*_*tán 59
只需将其src/main/resources放入工件即可.例如,如果您的工件是JAR,那么您将在其中log4j.properties包含文件,从而失去了使日志记录可配置的初始点.
我通常把它放入src/main/resources,并将其设置为输出到目标,如下所示:
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<targetPath>${project.build.directory}</targetPath>
<includes>
<include>log4j.properties</include>
</includes>
</resource>
</resources>
</build>
Run Code Online (Sandbox Code Playgroud)
另外,为了让log4j实际看到它,你必须将输出目录添加到类路径中.如果您的工件是可执行的JAR,您可能使用maven-assembly-plugin来创建它.在该插件中,您可以通过添加Class-Path清单条目将JAR的当前文件夹添加到类路径中,如下所示:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<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>
Run Code Online (Sandbox Code Playgroud)
现在,log4j.properties文件将紧挨着您的JAR文件,可以独立配置.
要直接从Eclipse运行应用程序,resources请在运行配置中将目录添加到类路径中:Run->Run Configurations...->Java Application->New选择Classpath选项卡,选择Advanced并浏览到您的src/resources目录.
spl*_*ash 26
一些"数据挖掘"说明这src/main/resources是典型的地方.
Google代码搜索结果:
src/main/resources/log4j.properties:4877src/main/java/log4j.properties:215 小智 9
用于初始化项目的资源最好放在src/main/resources文件夹中.要在构建期间启用这些资源的加载,可以简单地在maven项目的pom.xml中添加条目作为构建资源
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
Run Code Online (Sandbox Code Playgroud)
其他.properties文件也可以保存在用于初始化的此文件夹中.如果你想在资源文件夹的属性文件中的某些变量和填充它们从配置文件过滤性文件,这些文件保存在被设定为的src/main /过滤器过滤设置为true 配置文件,但它是一个完全不同的使用情况.现在,您可以忽略它们.
这是一个很好的资源maven资源插件,它很有用,只是浏览其他部分.
将资源文件放在另一个位置不是您可以使用的最佳解决方案:
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
<build>
Run Code Online (Sandbox Code Playgroud)
例如,当资源文件(例如jaxb.properties)与Java类一起深入到包内时.
| 归档时间: |
|
| 查看次数: |
126102 次 |
| 最近记录: |