maven-assembly-plugin每个slf4j实现一个jar

时间:2014-05-15 07:49:05

标签: java maven slf4j maven-assembly-plugin

在maven项目中我想生成其他*-nodep.jar个文件,但是这些文件应该包含slf4j API的misc实现。

麻烦在于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并提供包含misc slf4j imlpementations的多个*-nodep.jar包?

我的测试项目配置如下:

的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的{​​{1}}部分提供其他slf4j实现,但这些在组装过程中似乎完全被忽略。

1 个答案:

答案 0 :(得分:0)

由于没有志愿者,请让我记录下我的结果。

我决定采用问题中提出的解决方案,并在我的junit测试中接受警告。