How can I exclude a dependency from another dependency?


(​​​) #1

I’m trying to include the Spark web framework in my project, however it uses slf4j-simple which doesn’t support the logback file. What I am trying to do is exclude slf4j-simple and then include slf4j-api and logback-classic as separate dependencies. Someone posted the build script to do that here but it’s for Maven. Here it is:

<dependency>
    <groupId>com.sparkjava</groupId>
    <artifactId>spark-core</artifactId>
    <version>2.1</version>
    <exclusions>
        <exclusion>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
        </exclusion>
    </exclusions>
</dependency>

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.12</version>
</dependency>

<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
    <version>1.1.2</version>
</dependency>

<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-core</artifactId>
    <version>1.1.2</version>
</dependency>

and here is my current build.gradle showing a bit of what I’ve tried:

plugins {
    id 'com.github.johnrengelman.shadow' version '2.0.3'
    id 'java'
}

group 'com.pikachu.webaddon'
version '1.0-SNAPSHOT'

sourceCompatibility = 1.8

repositories {

    mavenCentral()

    maven {
        name = 'spigotmc-repo'
        url = 'https://hub.spigotmc.org/nexus/content/groups/public/'
    }

    maven {
        url 'https://raw.githubusercontent.com/bensku/mvn-repo/master/'
    }

    maven {
        url 'https://repo.destroystokyo.com/repository/maven-public/'
    }

    maven {
        url 'http://maven.sk89q.com/repo'
    }

    maven {
        url 'http://nexus.hc.to/content/repositories/pub_releases'
    }

    maven {
        url 'https://jitpack.io'
    }

}


dependencies {
    compileOnly 'org.spigotmc:spigot-api:1.12.1-R0.1-SNAPSHOT'
    testCompile group: 'junit', name: 'junit', version: '4.12'
    compileOnly 'ch.njol:skript:2.2-dev33'
    compile('com.sparkjava:spark-core:2.7.2') {
        exclude group: 'org.slf4j', module: 'slf4j'
    }
}


shadowJar {
    dependencies {
        exclude(dependency('org.slf4j:slf4j-simple:1.7.21'))
    }
}

(James Justinic) #2

It’s not clear what you’re trying to accomplish here. Your attempted exclude (slf4j) doesn’t match what you’ve said you’re trying to exclude (slf4j-simple). The Gradle equivalent of the Maven spark-core reference would be:

compile('com.sparkjava:spark-core:2.1') {
    exclude group: 'org.slf4j', module: 'slf4j-simple'
}

However, your posted build.gradle specifies com.sparkjava:spark-core:2.7.2, which doesn’t depend on slf4j-simple at all. The slf4j-simple dependency was removed starting with 2.4, so excluding it with recent versions won’t change anything.


(​​​) #3

I did try slf4j-simple, too but I gave it another shot just now and it is still showing in the built jar… For reference, here’s my current build script and a screenshot of the built jar’s contents after running gradlew shadow

plugins {
    id 'com.github.johnrengelman.shadow' version '2.0.3'
    id 'java'
}

group 'com.pikachu.webaddon'
version '1.0-SNAPSHOT'

sourceCompatibility = 1.8

repositories {

    mavenCentral()

    maven {
        name = 'spigotmc-repo'
        url = 'https://hub.spigotmc.org/nexus/content/groups/public/'
    }

    maven {
        url 'https://raw.githubusercontent.com/bensku/mvn-repo/master/'
    }

    maven {
        url 'https://repo.destroystokyo.com/repository/maven-public/'
    }

    maven {
        url 'http://maven.sk89q.com/repo'
    }

    maven {
        url 'http://nexus.hc.to/content/repositories/pub_releases'
    }

    maven {
        url 'https://jitpack.io'
    }

}


dependencies {
    compileOnly 'org.spigotmc:spigot-api:1.12.1-R0.1-SNAPSHOT'
    testCompile group: 'junit', name: 'junit', version: '4.12'
    compileOnly 'ch.njol:skript:2.2-dev33'
    compile('com.sparkjava:spark-core:2.7.2') {
        exclude group: 'org.slf4j', module: 'slf4j-simple'
    }
}


shadowJar {
    dependencies
}

image


(James Justinic) #4

You said that you are trying to exclude slf4j-simple. However, the JAR built from what you shared does not contain slf4j-simple. The dependency versions you’re using don’t even depend on slf4j-simple, so even if you remove the exclude, slf4j-simple won’t be included.

With the information you’ve provided, there doesn’t appear to be any way that your problem is actually related to slf4j-simple. The information about excluding slf4j-simple in Maven is only applicable to older versions of the Spark web framework than what you’re using. The exclude you have would work if that’s what you needed. If you share whatever error you’re seeing, it may help determine what you might actually need to change.


(​​​) #5

So you think one of my other dependencies is including it?

If you share whatever error you’re seeing

I don’t get any errors, it compiles succesfully


(James Justinic) #6

I don’t think anything is including slf4j-simple in your build at all. I think you’re confusing information you found about a slf4j-simple issue that applies to older versions of the Spark web framework with what you’re seeing.

My understanding is that you don’t want:

  • :white_check_mark: slf4j-simple - You don’t have it, even without the exclude.

But you want:

  • :white_check_mark: slf4j-api - You do have it, it’s a transitive dependency of spark-core
  • :x: logback-classic - Not a direct or transitive dependency of your dependencies {}

Are you actually seeing a problem, or are you just expecting to see something different than what you’re seeing before adding logback-classic?


(​​​) #7

I see what you’re saying now. I guess my issue is with the logback file itself - thank you for the help