This is answered in the documentation of the Gradle shadow plugin, section Embedding Jar Files Inside Your Shadow Jar:
[...] shadow is unable to distinguish between a jar file configured as a dependency and a jar file included in the resource folder. This means that any jar found in a resource directory will be merged into the shadow jar the same as any other dependency. If your intention is to embed the jar inside, you must rename the jar as to not end with .jar before the shadow task begins.
Unfortunately, this holds both for a JAR that is part of a dependency configuration as well as copying a JAR using a from
copy spec (which works since the shadowJar
task extends the jar
task; which in turn is a copy task and hence supports copy specs).
So, a JAR can only be embedded by renaming it. If it must be named .jar
inside the Uber JAR, I think the only option is a three-step workaround:
- Create the Uber JAR and embed JARs by renaming them; e.g., use the suffix
*.zip
.
shadowJar
{
from("${dirThatContainsSomeJARs}")
{
include '*.jar'
rename '(.+).jar', '$1.zip'
}
}
- Unpack the Uber JAR and rename all JARs that have been embedded as
*.zip
back to *.jar
.
- Repack everything again using the
jar
or zip
task.
j
, into an Uber JAR, sayu
, in two ways: either, the content ofj
is unpacked an copied intou
(usually preserving the folder structure and in addition possibly mergingMETA-INF/services
files), orj
is copied as is intou
(i.e. the file itself in an unpacked way). – MogulJ
into uber jarU
as-is (no unpacking). "Problem description" part of the question states that as well - it says that shadowJar unpacks the jars, instead of copying verbatim. I wantshadowJar
task to copy a jarexample.jar
fromsome/path/example.jar
varbatim intotarget/app-uberjar.jar
. – Krucik