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 package
After 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-Class
entry.
Use this file for execution and deployment.
Last updated