Gradle
JAR
Runnable JAR
Build Automation
Java Development

Creating runnable JAR with Gradle

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Creating a runnable JAR with Gradle involves a series of steps aimed at configuring and packaging your Java application into an executable JAR file. This article will walk you through the necessary steps, providing technical explanations and examples to help you understand the process.

Introduction to Gradle

Gradle is a powerful build automation tool used primarily for Java projects. It combines the best features of Apache Ant and Apache Maven and introduces Groovy-based domain-specific language (DSL) which helps in writing build scripts in a more human-readable way.

Steps to Create a Runnable JAR with Gradle

Step 1: Setting up a Gradle Project

First, ensure you have Gradle installed on your system. To check your Gradle version, run:

bash
gradle -v

Create a new directory for your project and navigate into it. Then, initialize a new Gradle project:

bash
gradle init

Follow the prompts and use the default settings unless you need specific configurations.

Step 2: Configuring the Build Script

Your project should contain a build.gradle file. This file is central to configuring your build. To create a runnable JAR, you need to:

  1. Apply the Java plugin to enable Java-related tasks.
  2. Declare dependencies required by your application.
  3. Define the main class that contains the main method.

Here is an example of a build.gradle file:

groovy
1plugins {
2    id 'java'
3}
4
5group 'com.example'
6version '1.0-SNAPSHOT'
7
8sourceCompatibility = 1.8
9
10repositories {
11    mavenCentral()
12}
13
14dependencies {
15    implementation 'com.google.guava:guava:31.0.1-jre'
16    testImplementation 'junit:junit:4.13.2'
17}
18
19jar {
20    manifest {
21        attributes(
22            'Main-Class': 'com.example.Main' // Replace with your main class
23        )
24    }
25
26    from {
27        configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
28    }
29}

Step 3: Adding the Main Class

Create a src/main/java directory structure in your project directory. Inside src/main/java, create a package (e.g., com/example) and add your main class:

java
1package com.example;
2
3public class Main {
4    public static void main(String[] args) {
5        System.out.println("Hello, World!");
6    }
7}

The build.gradle script above specifies com.example.Main as the main class, which should align with the package and class names you define.

Step 4: Building the JAR

Run the following Gradle command to build your project and create the JAR file:

bash
gradle clean build

The clean task deletes the previous build files, while the build task compiles and packages your application. The output JAR will be located in the build/libs directory.

Step 5: Running the JAR

To run the JAR file, use the Java command:

bash
java -jar build/libs/[your-jar-name].jar

Replace [your-jar-name] with the actual name of the generated JAR file.

Additional Details

Customizing the JAR

Gradle allows you to further customize the JAR task:

  • Include Additional Files: You can include resources or additional files in the JAR by specifying them in the from statement.
  • Custom Manifest Entries: Add more attributes in the manifest section to modify the JAR's metadata.

Handling Dependencies

When you declare dependencies using Gradle, they are automatically included in the JAR file using the from block, which collects the runtime classpath. This ensures that your application has all the required libraries at runtime.

Key Points Summary

Here's a summary table outlining key points about creating a runnable JAR with Gradle:

Key PointsDescription
Gradle InitializationUse gradle init to set up a project structure.
Build ConfigurationDefine main class and dependencies in build.gradle.
Source Code LocationPlace source files under src/main/java.
Build ProcessUse gradle clean build to compile and package.
ExecutionRun with java -jar followed by JAR file path.

Conclusion

Creating a runnable JAR with Gradle is a straightforward process that involves setting up a project, configuring the build script, and using Gradle's tasks to compile and package your application. By following these steps and customizing the build as needed, you can easily manage Java applications and their dependencies.


Course illustration
Course illustration

All Rights Reserved.