KumuluzEE (since version 2.4.0) provides support for packing and running microservices as uber JARs. It also includes a Gradle task that correctly packages the microservice.
To package a KumuluzEE microservice into an uber JAR, you need to add the following task declarations into your REST module build.gradle:
dependencies {
kumuluzeeLoader 'com.kumuluz.ee:kumuluzee-loader:3.1.0'
}
task repackageKumuluzee {
/*Unpack and copy kumuluzee loader*/
copy {
from zipTree(configurations.kumuluzeeLoader.singleFile).matching { include "**/*.class" }
into "$buildDir/classes/java/main"
outputs.upToDateWhen { false }
}
/*Create boot loader properties*/
file("$buildDir/classes/java/main/META-INF/kumuluzee").mkdirs()
/*Change to correct main class*/
file("$buildDir/classes/java/main/META-INF/kumuluzee/boot-loader.properties").text = "main-class=com.kumuluz.ee.EeApplication"
}
/**
* Configure a different path using -PwebappDir="RELATIVE_PATH" argument when running the task.
* */
task copyOrCreateWebapp {
def webapp_path = Paths.get("$buildDir", "classes", "webapp")
if (!Files.isDirectory(webapp_path)) {
def sourceWebappPath = project.hasProperty("webappDir") ? Paths.get(projectDir.path, project.property("webappDir").toString()) : Paths.get(projectDir.path, "src", "main", "webapp")
println("Source Webapp path: " + sourceWebappPath)
if (Files.isDirectory(sourceWebappPath)) {
copy {
from sourceWebappPath
into webapp_path
}
}
if (!Files.isDirectory(webapp_path)) {
Files.createDirectories(webapp_path)
}
}
}
/**
* Builds the kumuluzee UberJAR.
* */
task buildKumuluzee {
jar {
baseName = "kumuluz-server"
archiveName = "${baseName}-${version}.${extension}"
manifest {
attributes "Main-Class": "com.kumuluz.ee.loader.EeBootLoader"
}
/*Copy project dependencies into lib folder as jars*/
into("lib") {
from configurations.runtimeClasspath
}
}
dependsOn repackageKumuluzee
dependsOn copyOrCreateWebapp
finalizedBy build
}
/**
* Run the application in an executable JAR archive runtime.
* */
task runJar(type: JavaExec) {
main = "-jar"
args jar.archivePath
}
To build the project as Uber JAR execute the command below.
gradle clean buildKumuluzee
Start the application using the following command:
java -jar ${project.build.finalName}.jar
or through Gradle:
gradle runJar
Command line example:
java -jar my-app-1.0.0-SNAPSHOT.jar