Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are "resources" folders in SBT projects for?

Tags:

scala

sbt

In SBT project folders hierarchy I am to put my Scala sources in src/main/scala and tests in src/tests/scala. What am I meant to put into src/main/resources and src/tests/resources?

like image 914
Ivan Avatar asked Oct 06 '10 00:10

Ivan


2 Answers

Everything in that directory gets packed into the .jar created when you call package.

This means you can use it for images, sound files, text, anything that's not code but is used by your code.

like image 93
Dylan Lacey Avatar answered Sep 28 '22 05:09

Dylan Lacey


Here's an example of copying a text file stored in resource to a local file system:

  def copyFileFromResource(source: String, dest: File) {
    val in = getClass.getResourceAsStream(source)
    val reader = new java.io.BufferedReader(new java.io.InputStreamReader(in))
    val out = new java.io.PrintWriter(new java.io.FileWriter(dest))
    var line: Option[String] = None
    line = Option[String](reader.readLine)
    while (line != None) {
      line foreach { out.println }
      line = Option[String](reader.readLine)
    }
    in.close
    out.flush
  }
like image 44
Eugene Yokota Avatar answered Sep 28 '22 05:09

Eugene Yokota