多模块 Maven 项目中的 log4j 配置文件
我正在做一个多模块Maven项目,其结构是这样的:
war-module
jar-module
war模块依赖于jar模块,并且打包后会将jar工件添加到webapp的lib目录中。
并且war-module和jar-module都使用Apache log4j进行日志记录,并共享相同的log4j配置文件(log4j.xml),目前该文件位于jar-module项目中。而这个log4j.xml会被打包成jar-module.jar文件,但是我想把它放到war包中的WEB-INF/classes目录中而不是jar文件中,这样用户就很容易找到这个配置文件并在必要时修改它(如果该文件位于 WEB-INF/lib/jar-module.jar 中,他们很难找到它,因为该目录下还有许多其他 jars)。
我的问题是:Maven解决这个问题的方法是什么?
更新:
我的真实项目有点复杂,并且有一个ear-module也依赖于jar-module(又名。jar-module可以在多个不同的项目中独立使用,并且我不能只是将文件放入 war-module/src/main/resources 目录来解决这个问题)。我不想在多个项目中重复一些配置文件,例如 log4j.xml(以及其他配置文件,例如 myapp.properties)。
I am working on a multi-module Maven project, whose structure is like this:
war-module
jar-module
The war-module depends on the jar-module, and will add the jar artifact into the webapp's lib directory after packaging.
And both the war-module and jar-module use Apache log4j for logging, and share the same log4j configuration file (log4j.xml), which locates in jar-module project at present. And this log4j.xml will be packaged into jar-module.jar file, however, I would like to make it into WEB-INF/classes directory in the war package rather than in the jar file so that users will be easy to find this configuration file and modify it if necessary (it is very hard for them to find it if this file is in the WEB-INF/lib/jar-module.jar because there are many other jars under that directory).
My question is: what is the Maven way to solve this problem?
Update:
My real project is a bit more complex, and there is a ear-module which depends on the jar-module too (aka. the jar-module can be used independently in several different projects, and I cannot just put the file into war-module/src/main/resources directory to fix this problem). And I don't want to duplicate some configuration files such as log4j.xml (and other configuration files such as myapp.properties) across the several projects.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我通过在网络上进行更多搜索找到了答案。
一般来说,多模块Maven项目中共享资源的方式有以下三种:
这是 Maven 背后的公司 Sonatype 的一篇博客文章,内容涉及在 Maven 中跨项目共享资源,这正是我需要的答案:
http://www.sonatype.com/people/2008 /04/如何在maven中跨项目共享资源/
I found the answer via some more searching on the web.
Generally, there are three ways to share resources in a multi module Maven project:
Here's a blog post from Sonatype, the company behind Maven, on sharing resources across projects in Maven, and it is the exact answer I need:
http://www.sonatype.com/people/2008/04/how-to-share-resources-across-projects-in-maven/
在 jar 模块中,从 jar 中排除该文件:
使用 buildhelper 插件将 log4j.xml 作为单独的工件附加到构建中
现在在您的 war 工件中,将 xml 复制到输出目录:
当然,这会更容易首先将文件放在 [web-artifact]/src/main/resources 中:-)
In the jar module, exclude the file from the jar:
Use the buildhelper plugin to attach the log4j.xml to the build as a separate artifact
Now in your war artifact, copy the xml to the output directory:
But of course it would be easier to just put the file in [web-artifact]/src/main/resources in the first place :-)
根据我的经验,这可以通过一种简单的方式实现,只需:
详细步骤:
我更喜欢使用提供的范围并在运行时从类路径配置文件夹中提供文件
From my experience this can be implemented in an easy way , just :
Detailed steps:
I prefer to use the scope provided and provide the file from a classpath config folder at runtime.