Gradle fat jar does not contain libraries

Harold L. Brown :

I have created a simple Gradle Java project. The build.gradle file looks like this:

plugins {
    id 'java'
}

repositories {
    mavenCentral()
}

dependencies {
    implementation group: 'org.apache.commons', name: 'commons-lang3', version: '3.10'
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.0'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.6.0'
}

test {
    useJUnitPlatform()
}

task customFatJar(type: Jar) {
    archiveBaseName = 'fat-jar'
    from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
    with jar
}

I am creating the fat jar according to https://www.baeldung.com/gradle-fat-jar.

However, the resulting jar does not contain the commons-lang3 library. It only contains the project's class files.

Why isn't my library included in the fat jar?

Bjørn Vester :

The guide from Baeldung is outdated. I suggest following the guide in the official user guide instead: https://docs.gradle.org/current/userguide/building_java_projects.html#sec:java_packaging

They are currently suggesting this:

task uberJar(type: Jar) {
    archiveClassifier = 'uber'

    from sourceSets.main.output

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

You can customize the name like you already did, if you don't like using properties like archiveClassifier.

If you are interested in why the Baeldung version doesn't work for you, it is because they collect dependencies from the deprecated configuration called compile. You are using the newer implementation instead, so compile is empty. However, instead of simply changing compile to implementation, it is recommended to use runtimeClasspath (like in the user guide) as this will correctly handle dependencies that are scoped to only the compile phase or as runtime only. While you don't have any of those now, you may in the future.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Gradle 7 create a fat jar

Why does Gradle jar of jars result in duplicate libraries?

Advantages to a Fat Jar vs an application bundle in Gradle

Gradle: Build 'fat jar' with Spring Boot Dependencies

Why is there no easy way to create a fat Jar with Gradle?

Fat Jar expands dependencies with Gradle Kotlin DSL

Gradle task to create fat runnable jar

Jar made by gradle's jar task is not "fat" enough

Extract required libraries into generated Jar with Gradle

Gradle Uber (Fat) Jar and java.lang.NoClassDefFoundError in Runtime

Creating a fat jar using Kotlin and Gradle - compile vs implementation?

How do I create an executable fat jar with Gradle with implementation dependencies

Gradle get what repository a dependency was resolved from (smart fat jar)

Make NON-FAT jar in Gradle on-demand

gradle does not import local java libraries

Intellij fat one jar artifact does not generate a working output

What exactly does a jar file contain?

Does jar file not contain imported packages and classes?

How to add shared libraries to my jar by gradle 2.2.1?

How to find Boost libraries that does not contain any platform specific code

Why won't gradle jar build a jar that gradle build does?

Gradle DSL Method not found: 'multiDexEnabled'; using gradle that does not contain the method

What is a fat JAR?

Deploy WAR or "fat" JAR?

Renaming a fat jar with Maven

How to create a fat jar?

How to run a gradle JUnit test job without source files but with a fat jar?

log4j.properties gets overwritten when creating a "fat-JAR" using Gradle

Create fat jar from ktor Kotlin multiplatform project with Kotlin Gradle DSL

TOP Ranking

HotTag

Archive