How to Exclude Jars from a Grails 1.2.x WAR

Saturday, February 27, 2010

I have a Grails application that we’re developing and we’re targeting JBoss 5.1 as the production server. Our CI tool (Atalassian Bamboo) produced a production WAR for me which I tried to deploy. Of course I got numerous exceptions and stack traces because I had a copy of servlet-api.jar and xercesImpl.jar already packaged inside my WAR which conflicted with JBoss’ server libs classpath. If you need to remove any artifacts from the Grails build (i.e. grails war), you can write a closure in the bottom of the conf/BuildConfig.groovy such as

//bottom of conf/BuildConfig.groovy
grails.war.resources = { stagingDir ->

   File libDir = new File(stagingDir, 'WEB-INF/lib')

   def deleteJars = { jarNameStart ->
      libDir.eachFile { file ->
         if (file.name.startsWith(jarNameStart)) {
            file.delete()
            println "deleted jar $file"
         }
      }
   }

   deleteJars 'servlet-api' // conflicts with jboss 5.1
   deleteJars 'xercesImpl' // conflicts with jboss 5.1
}

You will now have a WAR without servlet-api.jar and xercesImpl.jar packaged in the WEB-INF/lib

Feel free to modify to fit your circumstance and environment.

Top