在依赖项目中包含来自 JAR 的文件
Include files from a JAR in a dependent project
我想跨多个项目同步 log4j 和 logback 配置文件。我有一个包含 log4j 和 logback 依赖项以及配置文件的项目(项目 A)。
项目 A
- src/test/resources
- log4j2.xml
- logback-test.xml
项目 B 依赖于项目 A。我想在项目 A 的 JAR 中包含日志配置文件,并在解析项目 B 的 Maven 依赖项时将它们自动放入项目 B 的特定目标文件夹中。
我在项目 A 中尝试过 maven-jar-plugin
,但它似乎不适合我的目的。
<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/logback-test.xml</include>
</includes>
</configuration>
</plugin>
如有任何帮助,我们将不胜感激。
谢谢!
更新:
虽然接受了 Eugene 的回答,但我需要添加一个 <resources>
条目,以便将日志配置文件包含在打包的 JAR 中。
<build>
<resources>
<resource>
<directory>src/test/resources</directory>
<includes>
<include>log4j2.xml</include>
<include>logback-test.xml</include>
</includes>
</resource>
</resources>
</build>
从项目 A 执行 mvn clean compile assembly:single deploy
创建并部署了包含日志文件的 JAR。
[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.
请不要将日志记录配置文件放入 JAR 中,如果有人依赖您的 JAR,您的配置可能会覆盖他们的。这取决于首先加载哪个 JAR
如果你愿意,像这样。并将它们打包成 JAR
- src/main/resources
- log4j2.xml
- logback.xml
您可以使用Apache Maven Remote Resources Plugin
This plugin is used to retrieve JARs of resources from remote
repositories, process those resources, and incorporate them into JARs
you build with Maven.
A very common use-case is the need to package certain resources in a
consistent way across your organization. For example at Apache, it is
required that every JAR produced contains a copy of the Apache license
and a notice file that references all used software in a given project
- 在项目 A 中定义要共享的资源或更好地为共享资源创建单独的项目
<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 用于共享资源
${basedir}/target/classes/META-INF/maven/remote-resources.xml
<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>
- 配置其他模块使用共享资源
<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>
备选方案:
Apache Maven Dependency Plugin 它可以将 and/or 解压工件从本地或远程存储库复制到指定位置。
那里有描述 Maven: Extract dependency resources before test
还有Use a dependency's resources?
更新:
如何添加资源的示例:
<project>
...
<build>
...
<resources>
<resource>
<directory>[your folder 1 here]</directory>
</resource>
<resource>
<directory>[your folder 2 here]</directory>
</resource>
</resources>
...
</build>
...
</project>
我想跨多个项目同步 log4j 和 logback 配置文件。我有一个包含 log4j 和 logback 依赖项以及配置文件的项目(项目 A)。
项目 A
- src/test/resources
- log4j2.xml
- logback-test.xml
项目 B 依赖于项目 A。我想在项目 A 的 JAR 中包含日志配置文件,并在解析项目 B 的 Maven 依赖项时将它们自动放入项目 B 的特定目标文件夹中。
我在项目 A 中尝试过 maven-jar-plugin
,但它似乎不适合我的目的。
<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/logback-test.xml</include>
</includes>
</configuration>
</plugin>
如有任何帮助,我们将不胜感激。
谢谢!
更新:
虽然接受了 Eugene 的回答,但我需要添加一个 <resources>
条目,以便将日志配置文件包含在打包的 JAR 中。
<build>
<resources>
<resource>
<directory>src/test/resources</directory>
<includes>
<include>log4j2.xml</include>
<include>logback-test.xml</include>
</includes>
</resource>
</resources>
</build>
从项目 A 执行 mvn clean compile assembly:single deploy
创建并部署了包含日志文件的 JAR。
[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.
请不要将日志记录配置文件放入 JAR 中,如果有人依赖您的 JAR,您的配置可能会覆盖他们的。这取决于首先加载哪个 JAR
如果你愿意,像这样。并将它们打包成 JAR
- src/main/resources
- log4j2.xml
- logback.xml
您可以使用Apache Maven Remote Resources Plugin
This plugin is used to retrieve JARs of resources from remote repositories, process those resources, and incorporate them into JARs you build with Maven.
A very common use-case is the need to package certain resources in a consistent way across your organization. For example at Apache, it is required that every JAR produced contains a copy of the Apache license and a notice file that references all used software in a given project
- 在项目 A 中定义要共享的资源或更好地为共享资源创建单独的项目
<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 用于共享资源
${basedir}/target/classes/META-INF/maven/remote-resources.xml
<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>
- 配置其他模块使用共享资源
<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>
备选方案:
Apache Maven Dependency Plugin 它可以将 and/or 解压工件从本地或远程存储库复制到指定位置。
那里有描述 Maven: Extract dependency resources before test
还有Use a dependency's resources?
更新:
如何添加资源的示例:
<project>
...
<build>
...
<resources>
<resource>
<directory>[your folder 1 here]</directory>
</resource>
<resource>
<directory>[your folder 2 here]</directory>
</resource>
</resources>
...
</build>
...
</project>