【发布时间】:2014-05-15 07:49:05
【问题描述】:
在 maven 项目中,我想生成额外的 *-nodep.jar 文件,但是这些文件应该包含 slf4j API 的杂项实现。
问题在于,对于maven-assembly-plugin,我需要提供项目类路径上的所有依赖项。这导致测试:
------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.AppTest
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/home/peterb/.m2/repository/org/slf4j/slf4j-log4j12/1.7.7/slf4j-log4j12-1.7.7.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/home/peterb/.m2/repository/org/apache/logging/log4j/log4j-slf4j-impl/2.0-rc1/log4j-slf4j-impl-2.0-rc1.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory]
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.171 sec
听起来 log4j 是随机选择的(可能类路径顺序在这里很重要)。
但是我如何在我的测试中使用 log4j 并提供多个 *-nodep.jar 打包了 misc slf4j imlpementations 的包?
我的测试项目配置如下:
pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com</groupId>
<artifactId>foo</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>foo</name>
<properties>
<slf4j-build>1.7.7</slf4j-build>
<log4j-build>1.2.17</log4j-build>
<log4j2-build>2.0-rc1</log4j2-build>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
<version>4.11</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j-build}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j-build}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j-build}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>${log4j2-build}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>${log4j2-build}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j2-build}</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>nodep</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<appendAssemblyId>true</appendAssemblyId>
<descriptors>
<descriptor>src/main/assembly/log4j2-nodep.xml</descriptor>
<descriptor>src/main/assembly/log4j-nodep.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
log4j-nodep.xml:
<assembly
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
<id>log4j-nodep</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<includes>
<include>com:foo:jar</include>
<include>org.slf4j:slf4j-api:jar</include>
<include>org.slf4j:slf4j-log4j12:jar</include>
<include>log4j:log4j:jar</include>
</includes>
<unpack>true</unpack>
<fileMode>0644</fileMode>
<useProjectArtifact>true</useProjectArtifact>
<scope>provided</scope>
</dependencySet>
</dependencySets>
</assembly>
log4j2-nodep.xml:
<assembly
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
<id>log4j2-nodep</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<includes>
<include>com:foo:jar</include>
<include>org.slf4j:slf4j-api:jar</include>
<include>org.apache.logging.log4j:log4j-slf4j-impl:jar</include>
<include>org.apache.logging.log4j:log4j-api:jar</include>
<include>org.apache.logging.log4j:log4j-core:jar</include>
</includes>
<unpack>true</unpack>
<fileMode>0644</fileMode>
<useProjectArtifact>true</useProjectArtifact>
<scope>provided</scope>
</dependencySet>
</dependencySets>
</assembly>
我尝试仅向maven-assembly-plugin 的<dependencies> 部分提供其他slf4j 实现,但在组装过程中似乎完全忽略了这些实现。
【问题讨论】:
标签: java maven slf4j maven-assembly-plugin