Gradle: How to exclude JAR from a WAR?

Viewed 27554

I have a multi-project Gradle build structure, where child project depends on a JAR, which I don't want to be in WAR file. I tried "exclude" but it does not work.

The main project script:

apply plugin: 'war'
war {
    dependencies {
        runtime (project(':childProject')) {
            exclude group: 'javax.servlet.jsp', module: 'jsp-api'
        }
    }
}

The childProject script:

apply plugin: 'java'
dependencies {
    compile 'javax.servlet.jsp:jsp-api'
}
5 Answers

The default behavior of the War task is to copy the content of src/main/webapp to the root of the archive. Your webapp directory may of course contain a WEB-INF sub-directory, which may contain all the dependencies of the runtime [1] configuration to WEB-INF/lib.

So to avoid load of other jar files or to decrease war file size, you may have to exclude jars during packaging. So, try adding rootSpec.exclude("/*.jar")** to exclude jars in war file like below.

war {
    archiveName = "newproject.war"
    rootSpec.exclude("**/*.jar")
    destinationDir = buildDir
}
Related