Preloader image

Shading the container and the application has some challenges like merging correctly resources (META-INF/services/ typically).

Here is a maven shade plugin configuration working for most cases:

          <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
          <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
          <transformer implementation="org.apache.openwebbeans.maven.shade.OpenWebBeansPropertiesTransformer" />
          <filter> <!-- we don't want JSF to be activated -->
see TomEE Embedded page for more information about tomee embedded options.
this shade uses TomEE Embedded but you can do the same with an Application Composer application.
if you have META-INF/web-fragment.xml in your application you will need to merge them in a single one in the shade. Note that tomcat provides one which can be skipped in this operation since it is there only as a marker for jasper detection.

Then just build the jar:

mvn clean package

And you can run it:

java -jar myapp-1.0-SNAPSHOT.jar

Fat Jars with Gradle

With gradle you can rely on either jar plugin, fatjar plugin or shadowjar plugin. Last one is likely the closer to maven shade plugin so that’s the one used for next sample:

// run $ gradle clean shadowJar
import org.apache.openwebbeans.gradle.shadow.OpenWebBeansPropertiesTransformer

buildscript {
    repositories {
    dependencies {
        classpath 'com.github.jengelman.gradle.plugins:shadow:1.2.3'
        classpath 'org.apache.openwebbeans:openwebbeans-gradle:1.7.0'

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

group 'org.apache.tomee.demo.gradle'
version '1.0-SNAPSHOT'

apply plugin: 'idea'
apply plugin: 'java'

sourceCompatibility = 1.8

repositories {

dependencies {
    compileOnly 'org.projectlombok:lombok:1.16.10'
    compile 'org.apache.tomee:tomee-embedded:7.0.2-SNAPSHOT'

// customize exclusions depending your app

// first the not used dependencies like JSF, JAXWS, JMS ones
def excludedDependenciesGroups = [
        // gradle is buggy with poms, scope provided and optional I think
        // tomee jaxws, jms, etc...

// then cxf+tomee specific dependencies so we need to be more precise than the group
// to not exclude everything
def excludedDependenciesArtifacts = [
shadowJar {
    classifier = 'bundle'

    // merge SPI descriptors
    append 'META-INF/cxf/bus-extensions.txt'

    // switch off JSF + JMS + JAXWS
    exclude 'META-INF/faces-config.xml'
    dependencies {
        exclude(dependency {
            excludedDependenciesGroups.contains(it.moduleGroup) ||

    // ensure we define the expected Main (if you wrap tomee main use your own class)
    manifest {
        attributes 'Main-Class': 'org.apache.tomee.embedded.FatApp'

Then run:

gradle clean build shadowJar

and you’ll get build/libs/demo-gradle-tomee-embedded-shade-1.0-SNAPSHOT-bundle.jar ready to run with:

java -jar build/libs/demo-gradle-tomee-embedded-shade-1.0-SNAPSHOT-bundle.jar --as-war --simple-log=true

Fat Wars

Fat Wars are executable wars. Note they can be fancy for demos but they have the drawback to put the server in web resources at packaging time (to ensure the war is actually an executable jar) so adding a filter preventing these files to be read can be needed if you don’t already use a web technology doing it (a servlet bound to /*).

Here how to do a fat war:

  <!-- can be uber (for all), jaxrs, jaxws for lighter ones -->

  <!-- ...your dependencies as usual... -->

        <dependentWarExcludes />
            <excludes />

Then just build the war:

mvn clean package

And you can run it:

java -jar myapp-1.0-SNAPSHOT.war