question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Document using the Shadow plugin as an alternative to Boot's fat jars when using Gradle

See original GitHub issue

I found the following is required in Gradle to get a fat jar working when cannot use the spring-boot-gradle-plugin plugin:

import com.github.jengelman.gradle.plugins.shadow.transformers.*

// Can't use Boot's nested jar packaging due to environment constrained classloader
apply plugin: 'com.github.johnrengelman.shadow'

shadowJar {
  zip64 true

  exclude 'META-INF/*.SF'
  exclude 'META-INF/*.DSA'
  exclude 'META-INF/*.RSA'

  // Required for Spring
  mergeServiceFiles()
  transform(AppendingTransformer) { resource = 'reference.conf'  }
  transform(AppendingTransformer) { resource = 'META-INF/spring.handlers'  }
  transform(AppendingTransformer) { resource = 'META-INF/spring.schemas'  }
  transform(AppendingTransformer) { resource = 'META-INF/spring.tooling'  }
  transform(PropertiesFileTransformer) { paths = ['META-INF/spring.factories' ] }

  // ...
}

The docs should strongly recommend the shadow / shade plugins for environmentally constrained classloaders¹ because resource transformation is a requirement. Without it, essential beans such as converters, property placeholder configurers, etc. go missing from Spring Boot’s autoconfiguration (i.e. spring.factories). The result is very difficult to diagnose.

¹environmentally constrained classloaders: A system classloader not under your control.

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:13 (5 by maintainers)

github_iconTop GitHub Comments

44reactions
unilamacommented, Jul 7, 2016

Final working example should be like that, strategy is required if you are using several components that contains spring.factories:


import com.github.jengelman.gradle.plugins.shadow.transformers.*
shadowJar {

    // Required for Spring
    mergeServiceFiles()
    append 'META-INF/spring.handlers'
    append 'META-INF/spring.schemas'
    append 'META-INF/spring.tooling'
    transform(PropertiesFileTransformer) {
        paths = ['META-INF/spring.factories' ]
        mergeStrategy = "append"
    }

}

I hope it will save someone’s lot of time 😃

6reactions
raj-saxenacommented, Apr 1, 2020

@unilama thanks for your comment. I was looking for this as Google’s Dataflow also doesn’t work with Spring’s Fat-jar. For KotlinDSL, here’s what I had to do to make it work -

plugins {
    id("com.github.johnrengelman.shadow") version "5.2.0"
}

...

tasks.withType<ShadowJar> {
    isZip64 = true
    // Required for Spring
    mergeServiceFiles()
    append("META-INF/spring.handlers")
    append("META-INF/spring.schemas")
    append("META-INF/spring.tooling")
    transform(PropertiesFileTransformer().apply {
        paths = listOf("META-INF/spring.factories")
        mergeStrategy = "append"
    })
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

what's the gradle alternative to a fat JAR? - java - Stack Overflow
The easiest way to create a fatjar in gradle, is to use the shadow plugin available via the plugin portal. see ...
Read more >
Creating fat JARs using the Ktor Gradle plugin
The Ktor Gradle plugin allows you to create and run an executable JAR that includes all code dependencies (fat JAR).
Read more >
Self-contained, executable Jars with Gradle and Shadow
Describes how to use Gradle and it's Shadow plugin to create a self contained, executable Jar. No more classpath management.
Read more >
Introduction - Imperceptible Thoughts
Shadow is a Gradle plugin for combining a project's dependency classes and resources into a single output Jar. The combined Jar is often...
Read more >
How to include dependencies in jar? - #21 by shrinathk
You might be interested in using the Shadow plugin. It generates a “fat-jar”, which is a Jar with the necessary dependencies. Note that...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found