Exclude resource files from jar file created with SBT

I use SBT to build my scala projects. In addition to scala source files I have some resource files in my project. Those are some configuration files and localization resource files.

When I build the jar file using SBT package command all of those resource files also get included in the jar file. When those resource files are included inside the jar, modifying them becomes difficult. I have to extract the jar file, edit them and create the jar file again.

Ideal way of handling resource files are not to include them in the jar file, rather keep them separately and include them in the class path when running the application. How to exclude them from SBT package task was the problem I had. After some googling I found out that this can be achieved by including mapping task in the build. Following is sample build file which uses mapping to exclude various types of resource files.


val excludeFileRegx = """(.*?)\.(properties|props|conf|dsl|txt|xml)$""".r

  lazy val myapp = Project(id = "myapp", base = file("myapp"),
    settings = baseSettings ++ Seq(
      name := "My App",
      mappings in (Compile, packageBin) ~= { (ms: Seq[(File, String)]) =>
        ms filter {
          case (file, toPath =>{
            val shouldExclude = excludeFileRegx.pattern.matcher(file.getName).matches
           // println("===========" + file + "  " + shouldExclude)
            !shouldExclude
          }
        }
      },
        libraryDependencies ++= Seq (dispatch),
      libraryDependencies ++= testDependencies))

 

Reader Comments

Hi. How do you define the variable baseSettings?. Thanks

@Armando

My baseSetting looks follows.


val ProjectVersion = "0.0.1-SNAPSHOT"
val Organization = "org.sansoft.test"

val ScalaVersion = "2.10.2"
val ScalaBinaryVersion = "2.

lazy val baseProfileSettings = Defaults.defaultSettings ++ Seq(
version := ProjectVersion,
organization := Organization,
scalaVersion := ScalaVersion,
scalaBinaryVersion := ScalaBinaryVersion,
scalacOptions += "-deprecation",
scalacOptions += "-unchecked",
parallelExecution in Test := false
)

thanks ;)