GradleUp / shadow

Gradle plugin to create fat/uber JARs, apply file transforms, and relocate packages for applications and libraries. Gradle version of Maven's Shade plugin.
https://www.gradleup.com/shadow/
Apache License 2.0
3.69k stars 389 forks source link

Unable to exclude files from shadow jar #505

Open philliplbryant opened 5 years ago

philliplbryant commented 5 years ago

Shadow Version:

5.1.0

Gradle Version:

5.3.1

Expected Behavior:

Files should not be included in the shadow jar

Actual Behavior:

Files are included in the shadow jar

Gradle Build Script(s):

task fatJar(type: ShadowJar) { configurations = [project.configurations.runtimeClasspath] manifest.from jar.manifest archiveClassifier = 'all'

exclude '**/DEPENDENCIES*'
exclude '**/LICENSE*'
exclude '**/Log4J*'
exclude 'META-INF/NOTICE*'

mergeServiceFiles {
     // Merge the contents of multiple same named files into a single file with that name
     include 'META-INF/spring.handlers'
     include 'META-INF/spring.schemas'
     include 'META-INF/services/**/*.*'
}
with jar

}

asule90 commented 4 years ago

having same problem, have you managed to solved this?

using flyway migration, and sql files still included in the jar.

shadowJar {
    exclude '*.sql'
   ...
}
gg-dt commented 4 years ago

As this bug does not seem to get fixed I will post a workaround I used. You can use the DontIncludeResourceTransformer for simple exclusions (I have not tested this).

import com.github.jengelman.gradle.plugins.shadow.transformers.DontIncludeResourceTransformer
shadowJar {
    transform(DontIncludeResourceTransformer) {
        resource = "/path/to/remove.txt"
    }
}

If this is not enough you can write a custom transformer which removes the files. Something like this:

import com.github.jengelman.gradle.plugins.shadow.transformers.Transformer
import com.github.jengelman.gradle.plugins.shadow.transformers.TransformerContext
import shadow.org.apache.tools.zip.ZipOutputStream

// Transformer which removes files we do not want in our final jar
class FileRemoveTransformer implements Transformer {
    @Override
    boolean canTransformResource(FileTreeElement element) {
        String fullPath = element.relativePath.getPathString();
        // Add your sophisticated checks here
        if (fullPath.equals("/path/to/remove.txt") {
            return true;
        }
        return false;
    }

    @Override
    void transform(TransformerContext context) {
    }

    @Override
    boolean hasTransformedResource() {
        return true
    }

    @Override
    void modifyOutputStream(ZipOutputStream os, boolean preserveFileTimestamps) {
    }
}

and then

shadowJar {
    transform(FileRemoveTransformer)
}
swimmesberger commented 3 years ago

Is there an ETA when this bug is fixed?

joffrey-bion commented 3 years ago

This is also happening to me with Gradle 6.3 and the shadow plugin version 6.1.0.

Is this doc incorrect or is this an actual bug?

saxenaraj commented 3 years ago

tried like below : dependencies { exclude "META-INF/.SF" exclude "META-INF/.DSA" exclude "META-INF/*.RSA" exclude "*/.html" exclude "module-info." exclude "Log4j-" exclude "mime.types" exclude "VersionInfo.java" }

hope it helps

Knautiluz commented 2 years ago
shadowJar {
    minimize {
        exclude("*.dll")
    }
    shadowJar.archiveFileName = rootProject.name + '.jar'
}

This worked for me

dfernandezm commented 2 years ago

Just commenting to reassure that @Knautiluz minimize block still works to exclude files properly from the fat jar.

I needed to exclude a bunch of native-image stuff I didn't need from Micronaut for Cloud Functions and I needed to do it like this.

dfernandezm commented 2 years ago

Actually, after some further testing, a block like the above excluded too many jars from the bundle. That's not what I expected.

itsTyrion commented 2 years ago

@saxenaraj 's way worked for me I used this to exclude unneeded native libs from JNA and another lib

shadowJar {
    dependencies {
        exclude "com/sun/jna/sunos*/*"
        exclude "**/*-ppc*/*" // Linux/AIX on PowerPC(32/64(le))
        exclude "**/*-arm*/*" // 32-bit Linux on ARM 
        exclude "**/*-x86/*"  // 32-bit Win/Linux on x86
    }
}

If you don't see a change immediately, do a gradle clean.