[英]log4j configuration file in a multi-module Maven project
我正在做一個多模塊的Maven項目,它的結構是這樣的:
war-module
jar-module
war-module依賴於jar-module,打包后會將jar artifact添加到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 方法是什么?
更新:
我的實際項目有點復雜,並且有一個ear-module也依賴於jar-module(也就是jar-module可以在幾個不同的項目中獨立使用,我不能只是將文件放入war- module/src/main/resources 目錄來解決這個問題)。 而且我不想在多個項目中重復一些配置文件,例如 log4j.xml(以及其他配置文件,例如 myapp.properties)。
我通過在網上的更多搜索找到了答案。
通常,在多模塊Maven項目中共享資源有以下三種方式:
- 剪切並粘貼它們。
- 使用程序集和依賴插件
- 使用 maven-remote-resources-plugin
這是 Maven 背后的公司 Sonatype 的一篇關於在 Maven 中跨項目共享資源的博客文章,它是我需要的確切答案:
http://www.sonatype.com/people/2008/04/how-to-share-resources-across-projects-in-maven/
在 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>
現在在您的戰爭工件中,將 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 中會更容易:-)
根據我的經驗,這可以通過簡單的方式實現,只需:
詳細步驟:
<dependency>
<groupId>com.your.group.id</groupId>
<artifactId>common-config</artifactId>
<scope>provided</scope>
</dependency>
我更喜歡使用提供的范圍並在運行時從類路徑配置文件夾中提供文件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.