user2749903
user2749903

Reputation: 1305

Gradle 7 create a fat jar

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.

Upvotes: 9

Views: 6494

Answers (1)

Thomas K.
Thomas K.

Reputation: 6780

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.

Upvotes: 10

Related Questions