Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle: How to exclude JAR from a WAR?

Tags:

java

gradle

jar

war

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'
}
like image 693
isobretatel Avatar asked Dec 20 '13 16:12

isobretatel


People also ask

What is transitive dependency in Gradle?

Transitive dependencyA variant of a component can have dependencies on other modules to work properly, so-called transitive dependencies. Releases of a module hosted on a repository can provide metadata to declare those transitive dependencies. By default, Gradle resolves transitive dependencies automatically.


3 Answers

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"

like image 168
John Vint Avatar answered Sep 20 '22 17:09

John Vint


I'm doing it this way.

war {   
    rootSpec.exclude("**/some-*.jar")
}
like image 28
d-sauer Avatar answered Sep 19 '22 17:09

d-sauer


I realised that providedCompile sometimes introduced dependencies issues for me, e.g. some classes are not found. I then figured out a more flexible solution.

We can just configure required dependencies as 'compile' and then exclude specific jars from the war file by using the following war configuration:

war {
    classpath = classpath.filter { file ->
        println file.name
        (
            !file.name.startsWith('gwt-dev') &&
            !file.name.startsWith('gwt-user') &&
            !file.name.startsWith('guava-gwt') &&
            !file.name.startsWith('gwtbootstrap3') &&
            !file.name.startsWith('gwtbootstrap3-extras') &&
            !file.name.startsWith('servlet-api')
        )
    }
}

So far, I found this is the cleanest solution for me. I hope it helps.

like image 43
Jake W Avatar answered Sep 20 '22 17:09

Jake W