Junaid
Junaid

Reputation: 19

Making a fat jar file and adding dependencies - Eclipse IDE

I am using the Eclipse IDE, using Kryonet and LibGDX libraries.

I am currently trying to make a fat jar file - making kryonet a dependency.

How would I make a big jar file?

Here is my build.gradle file

sourceCompatibility = 1.7
sourceSets.main.java.srcDirs = [ "src/" ]
sourceSets.main.resources.srcDirs = ["../core/assets"]

project.ext.mainClassName = "uk.ac.aston.teamproj.game.desktop.DesktopLauncher"
project.ext.assetsDir = new File("../core/assets")

task run(dependsOn: classes, type: JavaExec) {
    main = project.mainClassName
    classpath = sourceSets.main.runtimeClasspath
    standardInput = System.in
    workingDir = project.assetsDir
    ignoreExitValue = true
}

task debug(dependsOn: classes, type: JavaExec) {
    main = project.mainClassName
    classpath = sourceSets.main.runtimeClasspath
    standardInput = System.in
    workingDir = project.assetsDir
    ignoreExitValue = true
    debug = true
}

task dist(type: Jar) {
    manifest {
        attributes 'Main-Class': project.mainClassName
    }
    dependsOn configurations.runtimeClasspath
    from {
        configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
    }
    with jar
}


dist.dependsOn classes

eclipse.project.name = appName + "-desktop"

Upvotes: 0

Views: 567

Answers (1)

user3212784
user3212784

Reputation: 46

Sounds like you could use the "Shadow" gradle plugin to make fat jars, it makes the process a lot simpler.

You can find detailed instructions on how to use it here

If you want an executable jar don't forget to specify the MainClass attribute. Here's an example of usage:

buildscript {
    ext {
        shadowVersion = "5.2.0"
        mainClassName = "com.yourapp.>>>CLASS_NAME_CONTAINING_YOUR_MAIN_METHOD_GOES_HERE<<<"
    }

    // Repositories for build scripts
    repositories {
        // Maven
        mavenLocal()
        mavenCentral()

        // Sonatype repositories
        maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
        maven { url "https://oss.sonatype.org/content/repositories/releases/" }
    }

    // Dependencies for build scripts
    dependencies {
        classpath "com.github.jengelman.gradle.plugins:shadow:$shadowVersion"
    }
}

apply plugin: 'com.github.johnrengelman.shadow'
apply plugin: 'java'

jar {
    manifest {
        attributes(
                "Main-Class": project.mainClassName,
        )
    }
}

This will add the following to your build:

  • Adds a shadowJar task to the project.
  • Adds a shadow configuration to the project.
  • Configures the shadowJar task to include all sources from the project's main sourceSet.
  • Configures the shadowJar task to bundle all dependencies from the runtimeClasspath configuration.
  • Configures the classifier attribute of the shadowJar task to be 'all' .
  • Configures the shadowJar task to generate a Manifest with:
    • Inheriting all configuration from the standard jar task.
    • Adds a Class-Path attribute to the Manifest that appends all dependencies from the shadow configuration

The shadowJar task will build the fat jar, optionally it will be executable if you specified a Main-Class attribute for the manifest in the jar configuration, as I did in the above example.

Upvotes: 0

Related Questions