Gradle -> How to omit some jars from WEB-INF/lib

Robert Christian picture Robert Christian · Apr 5, 2013 · Viewed 9.9k times · Source

I have a subproject with a war spec that looks like this:

war {
    from('resources')  {
        include '*.properties'
        into 'WEB-INF/classes/'
    }    
    webXml = file('src/main/webapp/WEB-INF/web.xml')
}

Works great. Creates a single, fat war file that is deployable to Tomcat. Problem is, when deploying to TomEE and JBoss, I run into conflicts (ie with Javax Servlet, Jersey, etc). So I want to exclude a set of jars from being war'd. I looked at the Gradle war documentation, and it looks like I need to use excludes for this. I tried it two different ways, and the jars are not excluded from the war:

war {

    // copy properties file in classes so that 
    // they may be loaded from classpath
    from('resources')  {
        include '*.properties'
        into 'WEB-INF/classes/'
    }

    // specify web xml
    webXml = file('src/main/webapp/WEB-INF/web.xml')

    // remove jars that conflict with TomEE
    exclude '**/javax.inject-1.jar'
    exclude '**/javax.servlet-2.5.0.v201103041518.jar'
    exclude '**/servlet-api-2.5.jar'
    exclude '**/validation-api-1.0.0.GA.jar'

}

This is in a subproject (karyon-examples) within the NetFlix/karyon project hosted on github. The dependencies in the subproject look like this:

dependencies {
    compile 'org.slf4j:slf4j-api:1.7.0'
    runtime 'org.slf4j:slf4j-simple:1.7.0'
    compile project(':karyon-extensions')
    compile project(':karyon-admin-web')
}

And I want to avoid editing things like compile versus runtime dependencies, especially in other files and subprojects. In fact the jars I am trying to exclude above are benign when running with jetty and regular tomcat.

I simply want to exclude these jars without complicating the build scripts. What am I missing?

Thanks

Answer

Rene Groeschke picture Rene Groeschke · Apr 5, 2013

the obvious way to go is to move the dependencies you don't want to have in your war from the compile configuration to providedCompile and from runtime to providedRuntime. the provided configurations are added to your build when the war plugin is applied.

One other note on your snippet above: I think the exclude does not work because you referencing the target path in your exclude statements instead you should just reference it by /servlet-api-2.5.jar.