Search code examples
javagradlekeycloakfatjar

Excluding dependency tree from configurations.runtimeClasspath in gradle


I have the following gradle dependencies:

dependencies {
    implementation 'org.keycloak:keycloak-core:17.0.0'
    implementation 'org.keycloak:keycloak-server-spi:17.0.0'
    implementation 'org.keycloak:keycloak-server-spi-private:17.0.0'
    implementation 'org.keycloak:keycloak-services:17.0.0'

    implementation 'com.amazonaws:amazon-kinesis-producer:0.14.10'
}

I need to create a fat jar that contains the jars for all the transitive dependencies of 'com.amazonaws:amazon-kinesis-producer:0.14.10', but not the rest. So far I have this task to copy all the jars into a directory:

task copyToLib(type: Copy) {
    into "jars"
    duplicatesStrategy "exclude"
    from configurations.runtimeClasspath.findAll { file ->
        file.name != "keycloak-services-17.0.0.jar" && file.name != "keycloak-core-17.0.0.jar"
                && file.name != "keycloak-server-spi-17.0.0.jar" && file.name != "keycloak-server-spi-private-17.0.0.jar"
                && file.name != "keycloak-common-17.0.0.jar"
    }
}

jar { dependsOn copyToLib }

it excludes all the Keycloak based specific jars that I don't want, but I also want the transitive dependencies for those to not be included. Is there some easy way to remove the entire tree starting from a dependency node?

Side note: I cannot just remove those dependencies from my dependencies {} section because I need them for IntelliJ and to compile the code, while I need them to be excluded when adding the jars to a Docker image because the Keycloak base image already contains all of those and there are conflicts when I add those in addition.


Solution

  • Looks like that can be achieved using the following:

        from configurations.runtimeClasspath
                .exclude(group: 'org.keycloak', module: 'keycloak-core')
                .exclude(group: 'org.keycloak', module: 'keycloak-services')
                .exclude(group: 'org.keycloak', module: 'keycloak-server-spi-private')
                .exclude(group: 'org.keycloak', module: 'keycloak-server-spi')