Reputation: 19
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
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:
shadowJar
task to the project.shadow
configuration to the project.shadowJar
task to include all sources from the project's main sourceSet.shadowJar
task to bundle all dependencies from the runtimeClasspath
configuration.classifier
attribute of the shadowJar
task to be 'all' .shadowJar
task to generate a Manifest with:
Class-Path
attribute to the Manifest
that appends all dependencies
from the shadow configurationThe 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