Exclude jar file from War


(Firas Fafi) #1

Hello, I have a problem with “servlet-api.jar” which causes errors in my tomcat6:

com.myproject.server.services.auth.ApiFilter cannot be cast to javax.servlet.Filter
        at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:275)
        at org.apache.catalina.core.ApplicationFilterConfig.setFilterDef(ApplicationFilterConfig.java:422)
        at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:115)
        at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4072)
        at org.apache.catalina.core.StandardContext.start(StandardContext.java:4726)
        at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:799)
        at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:779)
        at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:601)
        at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:943)
        at org.apache.catalina.startup.HostConfig.deployWARs(HostConfig.java:778)
        at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:504)
        at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1317)
        at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:324)
        at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:142)
        at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1065)
        at org.apache.catalina.core.StandardHost.start(StandardHost.java:840)
        at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1057)
        at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:463)
        at org.apache.catalina.core.StandardService.start(StandardService.java:525)
        at org.apache.catalina.core.StandardServer.start(StandardServer.java:754)
        at org.apache.catalina.startup.Catalina.start(Catalina.java:595)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:606)
        at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)
        at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)

According to others forums the solution is to remove this jar from the war file (declare it as “providedCompile” or “ProvidedRuntime”). How can I set this knowing that I have used configurations for declaring this dependencie as you can see below:

configurations {
  myproject{transitive=false}
  ....
}
dependencies
{
myproject JavaxGID+'.servlet:servlet-api:'+servletApiVersion
....
}

Should I declare a Provided configurations then I extend from it? Thank you for answers :slight_smile:


(Carlo Luib-Finetti) #2

yes, declare a provided configuration (why would you extend from it?)

configurations {
 provided
}

As you need the servlet api as a dependency, have it assigned to the provided configuration:

dependencies {
   compile....
   runtime ...
  provided (libraries.servletApi)
}

And for the WAR section, do it like this

war {
   classpath
= configurations.runtime - configurations.provided
    ...
}

(Firas Fafi) #3

Thank you “Carlo.lf”. Well using extends will saving me some lines. Finally I will mix your solution with mine.