Skip to content

Instantly share code, notes, and snippets.

@ArtificialPB
Created February 13, 2019 21:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ArtificialPB/fa629f321fd3f9d74cc77078da4b4bab to your computer and use it in GitHub Desktop.
Save ArtificialPB/fa629f321fd3f9d74cc77078da4b4bab to your computer and use it in GitHub Desktop.

Uber JAR support - Pack and run microservice as uber JAR

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
}

Execution of the Uber JAR build

To build the project as Uber JAR execute the command below.

gradle clean buildKumuluzee

Run

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment