Question

in the maven project I'd like to produce additional *-nodep.jar files, however these should pack misc implementations of the slf4j API.

The trouble is that for maven-assembly-plugin I need to provide all dependencies on the project classpath. That results in tests into:

------------------------------------------------------
 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

It sounds like log4j was chosen kind of randomly (maybe classpath order matters here).

But how can I use log4j in my tests AND provide multiple *-nodep.jar packages with misc slf4j imlpementations packaged?

My test project configs follow:

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>

I tried to provide other slf4j implementations to maven-assembly-plugin's <dependencies> section only, but these seem to be completely ignored during assembly.

Was it helpful?

Solution

As no volunteers out there, let me document what I ended up with.

I decided to go for the solution suggested in the question and live with the warning in my junit tests.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top