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

nyb*_*bon 16 java maven-2 packaging maven

我正在研究一个多模块Maven项目,其结构如下:

war-module
jar-module
Run Code Online (Sandbox Code Playgroud)

war-module依赖于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中,则很难找到它,因为该目录下有许多其他jar).

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

更新:

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

nyb*_*bon 11

我通过网上搜索找到了答案.

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

  • 剪切并粘贴它们.
  • 使用Assembly和Dependency插件
  • 使用maven-remote-resources-plugin

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

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


Sea*_*oyd 6

在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>
Run Code Online (Sandbox Code Playgroud)

使用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>
Run Code Online (Sandbox Code Playgroud)

现在在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>
Run Code Online (Sandbox Code Playgroud)

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