我想在多个项目中同步log4j和logback配置文件.我有一个项目(项目A)包含log4j和logback依赖项,以及配置文件.

项目A

  • src/测试/资源

Project B has a dependency on 项目A. I would like to include the log config files in 项目A's JAR and have them automatically put in a specific target folder in Project B when resolving Maven dependencies for Project B.

I have tried maven-jar-plugin in 项目A but it doesn't seem to work for my purpose.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>3.2.2</version>
    <configuration>
        <includes>
            <include>src/test/resources/log4j2.xml</include>
            <include>src/test/resources/回写测试.xml</include>
        </includes>
    </configuration>
</plugin>

任何帮助都将不胜感激.

谢谢

UPDATE:

虽然Eugene的答案被接受了,但我需要添加一个<resources>条目,这样日志(log)配置文件就会包含在打包的JAR中.

<build>
    <resources>
        <resource>
            <directory>src/test/resources</directory>
            <includes>
                <include>log4j2.xml</include>
                <include>回写测试.xml</include>
            </includes>
        </resource>
    </resources>
</build>

Executing mvn clean compile assembly:single deploy from 项目A created and deployed the JAR with the log files included.

[INFO] --- maven-remote-resources-plugin:1.7.0:bundle (default) @ project-a ---
[INFO] Writing META-INF/maven/remote-resources.xml descriptor with 2 entries

从项目B执行mvn clean compile将文件复制到输出目录

[INFO] --- maven-remote-resources-plugin:1.7.0:process (default) @ project-b ---
[INFO] Preparing remote bundle com.my.projects:project-a:1.0-SNAPSHOT
[INFO] Copying 2 resources from 1 bundle.

推荐答案

你可以用Apache Maven Remote Resources Plugin

此插件用于从远程服务器检索资源的jar

一个非常常见的用例是需要将某些资源打包到

  1. 在项目中定义要共享的资源,或者更好地为共享资源创建单独的项目
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-remote-resources-plugin</artifactId>
                <version>1.7.0</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>bundle</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <resourcesDirectory>src/test/resources/</resourcesDirectory>
                    <includes>
                        <include>log4j2.xml</include>
                        <include>logback-test.xml</include>
                      </includes>
                </configuration>
            </plugin>
        </plugins>
    </build>

Maven将创建resources bundle file个共享资源

<remoteResourcesBundle xsi:schemaLocation="http://maven.apache.org/remote-resources/1.1.0 https://maven.apache.org/xsd/remote-resources-1.1.0.xsd"
    xmlns="http://maven.apache.org/remote-resources/1.1.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <remoteResources>
    <remoteResource>log4j2.xml</remoteResource>
    <remoteResource>logback-test.xml</remoteResource>
  </remoteResources>
  <sourceEncoding>UTF-8</sourceEncoding>
</remoteResourcesBundle>

documentation

  1. 配置其他模块以使用共享资源
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-remote-resources-plugin</artifactId>
                <version>1.7.0</version>
                <configuration>
                    <outputDirectory>[your output directory]</outputDirectory>
                    <resourceBundles>
                        <!--The resource bundles that will be retrieved and processed. For example: org.test:shared-resources:${project.version}-->
                        <resourceBundle>groupId:artifactId:version[:type[:classifier]]</resourceBundle> 
                    </resourceBundles>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>process</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

documentation

Alternative solution:
Apache Maven Dependency Plugin It can copy and/or unpack artifacts from local or remote repositories to a specified location.
It was described there Maven: Extract dependency resources before test and there Use a dependency's resources?

UPDATE:

<project>
 ...
 <build>
   ...
   <resources>
     <resource>
       <directory>[your folder 1 here]</directory>
     </resource>
     <resource>
       <directory>[your folder 2 here]</directory>
     </resource>
   </resources>
   ...
 </build>
 ...
</project>

documentation

Java相关问答推荐

Cosmos Change Feed Process Lag远远超过收集中的记录数量

try 使用Java 9或更高版本对特殊对象图进行解析时出现NullPointerException

通过合并Akka Streams中的多个慢源保持订购

如何创建同一类的另一个对象,该对象位于变量中?

为什么JAVA&S清洁器使用链表而不是并发HashSet?

JavaFX如何在MeshView中修复多个立方体?

Spring data JPA/Hibernate根据id获取一个列值

由于 list 中的权限错误,Android未生成

格式中的特定回录键-值对

使用PDFBox从PDF中删除图像

如何在JavaFX中处理多个按钮

Java SSLKeyManager出厂密码

Java堆中的许多java.time.ZoneRegion实例.ZoneId实例不应该被缓存吗?

Quarkus:运行时出现EnumConstantNotPresentException

为什么我不能建立输入/输出流?Java ServerSocket

具有最大共同前景像素的图像平移优化算法

不能在 map 上移除折线

如何在单元测试中获得我的装饰Mapstruct映射器的实例?

如何用Micrometer&;斯普肯

如何在Java中正确实现填字游戏求解器