java 多模块Maven项目中的log4j配置文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4162992/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 05:07:11  来源:igfitidea点击:

log4j configuration file in a multi-module Maven project

javamavenmaven-2packaging

提问by nybon

I am working on a multi-module Maven project, whose structure is like this:

我正在做一个多模块的Maven项目,它的结构是这样的:

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.

war-module依赖于jar-module,打包后会将jar artifact添加到webapp的lib目录下。

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).

并且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中,他们很难找到它,因为该目录下还有许多其他jar)。

My question is: what is the Maven way to solve this problem?

我的问题是:解决这个问题的 Maven 方法是什么?

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.

我的实际项目有点复杂,并且有一个ear-module也依赖于jar-module(也就是jar-module可以在几个不同的项目中独立使用,我不能只是将文件放入war- module/src/main/resources 目录来解决这个问题)。而且我不想在多个项目中重复一些配置文件,例如 log4j.xml(以及其他配置文件,例如 myapp.properties)。

采纳答案by nybon

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:

通常,在多模块Maven项目中共享资源有以下三种方式:

  • Cut and paste them.
  • Use Assembly and Dependency plugins
  • Use the maven-remote-resources-plugin
  • 剪切并粘贴它们。
  • 使用程序集和依赖插件
  • 使用 maven-remote-resources-plugin

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:

这是 Maven 背后的公司 Sonatype 的一篇关于在 Maven 中跨项目共享资源的博客文章,它是我需要的确切答案:

http://www.sonatype.com/people/2008/04/how-to-share-resources-across-projects-in-maven/

http://www.sonatype.com/people/2008/04/how-to-share-resources-across-projects-in-maven/

回答by Sean Patrick Floyd

In the jar module, exclude the file from the jar:

在 jar 模块中,从 jar 中排除文件:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>2.3.1</version>
    <configuration>
      <excludes>
        <exclude>log4j.xml</exclude>
      </excludes>
    </configuration>
</plugin>

Use the buildhelper plugin to attach the log4j.xml to the build as a separate artifact

使用 buildhelper 插件将 log4j.xml 作为单独的工件附加到构建中

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <version>1.5</version>
    <executions>
      <execution>
        <id>attach-artifacts</id>
        <phase>package</phase>
        <goals>
          <goal>attach-artifact</goal>
        </goals>
        <configuration>
          <artifacts>
            <artifact>
              <file>${project.build.outputDirectory}/log4j.xml</file>
              <type>xml</type>
              <classifier>log4j</classifier>
            </artifact>
          </artifacts>
        </configuration>
      </execution>
    </executions>
</plugin>

Now in your war artifact, copy the xml to the output directory:

现在在您的War工件中,将 xml 复制到输出目录:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
      <execution>
        <id>copy</id>
        <phase>prepare-package</phase>
        <goals>
          <goal>copy</goal>
        </goals>
        <configuration>
          <artifactItems>
            <artifactItem>
              <groupId>${project.groupId}</groupId>
              <artifactId>your.jar.project.artifactId</artifactId>
              <version>${project.version}</version>
              <type>xml</type>
              <classifier>log4j</classifier>
              <outputDirectory>${project.build.outputDirectory}
              </outputDirectory>
              <destFileName>log4j.xml</destFileName>
            </artifactItem>
          </artifactItems>
        </configuration>
      </execution>
    </executions>
</plugin>

But of course it would be easier to just put the file in [web-artifact]/src/main/resources in the first place :-)

但当然,首先将文件放在 [web-artifact]/src/main/resources 中会更容易:-)

回答by Hatem

From my experience this can be implemented in an easy way , just :

根据我的经验,这可以通过简单的方式实现,只需:

  1. Make the log4j configuration resource at the parent module.
  2. Call the dependency of log4j in all pom modules.
  1. 在父模块上制作 log4j 配置资源。
  2. 在所有 pom 模块中调用 log4j 的依赖。

Detailed steps:

详细步骤:

  1. Create a project with common-config
  2. In the src/main/resources folder put the log4j or logback config file
  3. Install the artifact in the local Maven repository to be used by other projects
  4. Add the dependency as a normal Maven dependency in the subsequent projects
  1. 使用 common-config 创建一个项目
  2. 在 src/main/resources 文件夹中放置 log4j 或 logback 配置文件
  3. 将artifact安装到本地Maven仓库中,供其他项目使用
  4. 在后续项目中将依赖添加为普通的Maven依赖
<dependency> 
  <groupId>com.your.group.id</groupId> 
  <artifactId>common-config</artifactId> 
  <scope>provided</scope> 
</dependency>

I prefer to use the scope provided and provide the file from a classpath config folder at runtime.

我更喜欢使用提供的范围并在运行时提供类路径配置文件夹中的文件。