log4j configuration file in a multi-module Maven project

前端 未结 3 1578
生来不讨喜
生来不讨喜 2020-12-28 17:19

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,

相关标签:
3条回答
  • 2020-12-28 18:02

    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.

    0 讨论(0)
  • 2020-12-28 18:14

    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/

    0 讨论(0)
  • 2020-12-28 18:20

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

    0 讨论(0)
提交回复
热议问题