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

发布于 2024-10-02 06:07:13 字数 657 浏览 7 评论 0原文

我正在做一个多模块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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

雅心素梦 2024-10-09 06:07:14

我通过在网络上进行更多搜索找到了答案。

一般来说,多模块Maven项目中共享资源的方式有以下三种:

  • 剪切并粘贴它们。
  • 使用程序集和依赖项插件
  • 使用 maven-remote-resources-plugin

这是 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:

  • Cut and paste them.
  • Use Assembly and Dependency plugins
  • Use the 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:

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

风启觞 2024-10-09 06:07:14

在 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>

使用 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>

现在在您的 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>

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

In the jar module, exclude the file from the 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

<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:

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

冷血 2024-10-09 06:07:14

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

  1. 在父模块中创建 log4j 配置资源。
  2. 在所有pom模块中调用log4j的依赖。

详细步骤:

  1. 使用 common-config 创建项目
  2. 在 src/main/resources 文件夹中放入 log4j 或 logback 配置文件
  3. 在本地 Maven 存储库中安装工件以供其他项目使用
  4. 在后续添加依赖作为普通 Maven 依赖 。
<dependency> 
  <groupId>com.your.group.id</groupId> 
  <artifactId>common-config</artifactId> 
  <scope>provided</scope> 
</dependency>

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

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.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文