Gradle: How to exclude JAR from a WAR?

isobretatel picture isobretatel · Dec 20, 2013 · Viewed 24.5k times · Source

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'
}

Answer

John Vint picture John Vint · Dec 20, 2013

From the Gradle documentation

The War plugin adds two dependency configurations: providedCompile and providedRuntime. Those configurations have the same scope as the respective compile and runtime configurations, except that they are not added to the WAR archive.

So, in other words, adding an entry to providedCompile or providedRuntime will cause that dependency to be excluded from the war file.

  • use providedCompile if you have source that relies on some classes for compiling
  • use providedRuntime if you use it for testing and not compiling.

http://www.gradle.org/docs/current/userguide/war_plugin.html

Example

providedCompile "javax.servlet:servlet-api:2.5"