Cleaning up a WAR in a SBT Build

Recently some of our software projects have beging using SBT to build them using Build.scala (rather than build.sbt). One of the challenges (apart from learning Scala to build a Java project!) was removing some unneeded JAR files from a WAR build. I use xsbt-web-plugin to provide support for WAR packaging. It turns out that the plugin provides a hook, webappPostProcess to manipulate the web application before generating the WAR. Using this and an example we can write some Scala to list files matching a regex and then delete the matching files

1
2
3
4
5
6
7
8
9
    webappPostProcess := {
      webappDir: File =>
        def recursiveListFiles(f: File, r: Regex): Array[File] = {
          val these = f.listFiles
          val good = these.filter(f => r.findFirstIn(f.getName).isDefined)
          good ++ these.filter(_.isDirectory).flatMap(recursiveListFiles(_,r))
        }
        recursiveListFiles(webappDir / "WEB-INF/lib/", """javax.*""".r).map( x => IO.delete(x))
    }

You could place this in your web app settings Seq to make it available to all WAR builds

Leave a Reply

Your email address will not be published. Required fields are marked *