Build a Jar with Gradle

Student @ National Institute of Technology, Delhi.
Android | Kotlin | Go | Python
Search for a command to run...

Student @ National Institute of Technology, Delhi.
Android | Kotlin | Go | Python
No comments yet. Be the first to comment.
Failures are Inevitable Network calls might time out, APIs might be temporarily unavailable, or databases might become unreachable. To handle such transient errors gracefully, implementing a retry mechanism is crucial. This article will guide you thr...

In the last article, we covered data classes in Kotlin. We saw how they simplify class creation and make it faster with less coding. Now, let's explore Sealed Classes. Sealed Classes in Kotlin are a specialized form of abstract classes. They limit su...

Efficient Data Structuring in Android Development with Kotlin's Data Classes

Learn about Kotlin type hierarchy, type checks and type casts

Understanding and Utilizing Polymorphism and Inheritance in Kotlin for Effective Development

In this article, I'll show you how to build a Jar file using the Gradle build system. Let's get started.
Before we dive into the process of building a Jar, let's address whether or not you need a fat jar. But wait,
The fat jar is the jar, which contains classes from all the libraries, on which your project depends as well as the classes of the current project.
In other words, a fat jar is self-contained and when executed like java -jar my-fat-jar.jar, it will work without setting up any other dependencies.
Let's see how to set it up.
In your build.gradle.kts add the following lines:
tasks.withType<Jar>() {
// ...
from(sourceSets.main.get().output)
dependsOn(configurations.runtimeClasspath)
from({
configurations.runtimeClasspath.get()
.filter { it.name.endsWith("jar") }
.map { zipTree(it) }
})
}
and that's it. This will now build a fat jar, if in case you decide you don't need it, just delete these lines.
Building a Jar is a fairly simple process
Open the Gradle tab, go to Tasks/build/jar and create a jar file.

The jar file would most likely be created at /build/libs/

Cover Image: Photo by Dan Dennis on Unsplash