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

时间:2021-08-21 19:23:34

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模块,并在打包后将jar工件添加到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.

我真正的项目有点复杂,而且还有一个依赖于jar模块的ear-module(也就是说.jar-module可以在几个不同的项目中独立使用,而且我不能把文件置于战争中 - module / src / main / resources目录来解决这个问题)。而且我不希望在几个项目中复制一些配置文件,例如log4j.xml(以及其他配置文件,例如myapp.properties)。

3 个解决方案

#1


10  

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
  • 使用Assembly和Dependency插件
  • 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/

#2


5  

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中会更容易:-)

#3


1  

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

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

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

Hope this help you

希望这对你有所帮助

#1


10  

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
  • 使用Assembly和Dependency插件
  • 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/

#2


5  

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中会更容易:-)

#3


1  

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

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

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

Hope this help you

希望这对你有所帮助