How to Build and Package a Java Application?
Understand how to compile and package your Java project into a deployable JAR file using Maven or Gradle.
📌 Why Do I Need to Build My Java Application?
Discloud does not compile Java projects automatically.
You must:
✔ Compile your code into .class files.
✔ Package everything into a single .jar file (including dependencies).
✔ Ensure the META-INF/MANIFEST.MF file correctly defines the Main-Class.
🔧 Choosing a Build Tool
Maven
Large projects, dependency-heavy apps
pom.xml
Gradle
Modern projects with flexible build scripts
build.gradle or build.gradle.kts
📦 Building Your Java Application
Apache Maven is a widely used build automation tool that manages project builds, dependencies, and documentation using a Project Object Model (POM).
Add the Maven Compiler Plugin.
Ensure your pom.xml includes the Maven Compiler Plugin to specify the Java version for compilation. This prevents compatibility issues when running the project.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.9.9</version>
<configuration>
<source>17</source>
<target>17</target>
</configuration>
</plugin>Specify the Main Class.
To make your application executable, you need to specify the entry point (main method). This is done using the Maven Shade Plugin, which also ensures that all dependencies are bundled inside a single JAR file.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<!-- NOTE: Replace this with the correct path to your project's main class -->
<mainClass>com.project.example.Main</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>Build the Project.
Run the following command to clean old builds and generate the new JAR files:
mvn clean packageAfter running mvn package, Maven will create two different JAR files inside the target/ directory:
Standard JAR (Original Project JAR).
📌 Filename: original-<artifactId>-<version>.jar
📌 Example: original-com.maven.discordbot-0.0.1-SNAPSHOT.jar
📌 Contents:
Only your project's compiled code.
Does NOT include dependencies.
Do NOT use this file for execution unless dependencies are handled separately.
Fat JAR (Shaded JAR with Dependencies).
📌 Filename: <artifactId>-<version>.jar
📌 Example: com.maven.discordbot-0.0.1-SNAPSHOT.jar
📌 Contents:
Your compiled application code.
All dependencies included (JDA, Apache Commons, etc.).
Manifest file (
MANIFEST.MF) with theMain-Classentry.
Use this file for execution and deployment.
Gradle is a flexible build automation tool that uses a Groovy or Kotlin-based DSL to define the build process.
Apply the Java Plugin.
Ensure your build.gradle (Groovy) or build.gradle.kts (Kotlin) applies the Java plugin:
plugins {
id 'java'
id 'com.gradleup.shadow' version '8.3.6' // Shadow Plugin for Fat JAR
}plugins {
java
id("com.gradleup.shadow") version "8.3.6"
}Specify the Main Class.
To make the application executable, define the entry point (main method) inside the JAR manifest.
shadowJar {
archiveClassifier.set('all')
manifest {
attributes(
'Main-Class': 'com.project.Main' // Replace with your actual main class
)
}
}
tasks.build.dependsOn shadowJar // Ensure shadowJar runs when buildingimport com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
tasks.withType<ShadowJar> {
archiveClassifier.set("all")
manifest {
attributes(mapOf("Main-Class" to "com.project.Main")) // Replace with your actual main class
}
}
tasks.named("build") {
dependsOn("shadowJar")
}Build the Project.
To clean old builds and generate new JAR files, run:
gradle clean buildStandard JAR (Regular Project JAR).
📌 Filename: <project-name>.jar
📌 Example: discordbot.jar
📌 Contents:
Only your project's compiled code.
Does NOT include dependencies.
Do NOT use this file for execution unless dependencies are handled separately.
Fat JAR (Uber JAR with Dependencies).
📌 Filename: <project-name>-all.jar
📌 Example: discordbot-all.jar
📌 Contents:
Your compiled application code.
All dependencies included (JDA, Apache Commons, etc.).
Manifest file (
MANIFEST.MF) with theMain-Classentry.
Use this file for execution and deployment.
Last updated