calembredaine
calembredaine

Reputation: 93

Maven transitive dependency not loaded

Problem: When I import the jar of a custom logging library in another application, I would like that the custom library contains the dependency on slf4j without to have to put this dependency in the application.

I have a Maven project my-logger that depends on log4j-slf4j-impl, on the pom.xml of my-logger :

<dependency>
  <groupId>org.apache.logging.log4j</groupId>
  <artifactId>log4j-slf4j-impl</artifactId>
  <version>2.12.1</version>
</dependency>

In my Spring Boot project my-app, I import my-logger :

<dependency>
  <groupId>org.test</groupId>
  <artifactId>my-logger</artifactId>
  <version>100</version>
</dependency>

But in IntelliJ I don't see any dependency under org.test:my-logger:100

And I have to add a log4j-slf4j-impl dependency in my-app pom.xml or I get a

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder"

The pom.xml of my-logger :

<?xml version="1.0" encoding="UTF-8"?>
<project
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
        xmlns="http://maven.apache.org/POM/4.0.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.test</groupId>
    <artifactId>my-logger</artifactId>
    <version>100</version>
    <packaging>jar</packaging>
    <name>my-logger</name>
    <description>Logging app</description>

    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-slf4j-impl</artifactId>
            <version>2.12.1</version>
        </dependency>

    </dependencies>
</project>

The pom.xml of my-app :

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
        <relativePath/>
    </parent>
    <groupId>com.example</groupId>
    <artifactId>my-app</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>my-app</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>11</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

         <dependency>
             <groupId>org.test</groupId>
             <artifactId>my-logger</artifactId>
             <version>100</version>
         </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

Upvotes: 3

Views: 2671

Answers (2)

calembredaine
calembredaine

Reputation: 93

When I did a maven deploy (see https://maven.apache.org/plugins/maven-deploy-plugin/deploy-file-mojo.html) to nexus I was using the option generatePom=true and the generated .pom file didn’t contain the transitive dependencies. When I added the option pomFile=pom.xml the transitive dependencies were added to the .pom file generated and it worked.

Upvotes: 2

J Fabian Meier
J Fabian Meier

Reputation: 35823

I guess that the spring-boot-starter-parent changes the scope of log4j-slf4j-impl to something like provided. You can figure that out by looking at mvn dependency:tree where you will find the resulting scope and version of log4j-slf4j-impl.

Upvotes: 1

Related Questions