Gradle 7 create a fat jar

Viewed 4385

I need to create a fat jar from my dependencies that won't contain dependencies in scope compileOnly

dependencies {
   api 'org.slf4j:slf4j-api:1.7.26' // this must be in the jar
   compileOnly 'it.unimi.dsi:fastutil:8.2.1' // this must not be in the jar

    jar {
        from {
            configurations.compileClasspath.findAll { it.name.endsWith('jar') }.collect { zipTree(it) }
        }
    }
}

When I build the project both dependencies are present within the final jar file. How am I supposed to exclude fastutil from fat jar?

I tried to use runtimeOnly

runtimeOnly 'it.unimi.dsi:fastutil:8.2.1' // this must not be in the jar

But this causes the fastutil to be unresolvable during compile time.

I'm running Java 16 and Gradle 7.0.2.

1 Answers

Use configurations.runtimeClasspath instead of configurations.compileClasspath. compileClasspath contains all libraries required for compilation, thus fastutil is included. runtimeClasspath on the other hand excludes libraries from configuration compileOnly.

Related