Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure sbt to load resources when running application?

Tags:

scala

sbt

My code (Java) reads an image from jar:

Main.class.getResourceAsStream("/res/logo.png")

Everything runs fine (if I start the app after packaging it into a jar). But when I run it using sbt's run task, it returns me null instead of needed stream.

Running this from sbt console also gives null:

getClass.getResourceAsStream("/res/logo.png")

Is there a way to tell sbt to put my resources on classpath?

EDIT:

I set the resources dir to be same as source dir:

build.sbt:
resourceDirectory <<= baseDirectory { _ / "src" }

When I loaded sbt's `console' and ran the following:

classOf[Main].getProtectionDomain().getCodeSource()

I got the location of my classes, but it does not contain neither res folder nor any of my resource files.

Seems that sbt copies resources only to the resulting jar, and does not copy them to classes dir. Should I modify compile task to move these resources files to classes dir?

EDIT2:

Yes, when I manually copy the resource file to classes dir, I can easily access it from console. So, how should I automate this process?

EDIT3:

It seems that sbt is just unable to see my resource folder - it does not add files to resulting jar file, actually!

Solution:

resourceDirectory in Compile <<= baseDirectory { _ / "src" }
like image 429
Rogach Avatar asked Dec 21 '11 10:12

Rogach


People also ask

What does %% mean in sbt?

This is part of SBT which play uses as a build tool. Specifically this is an import statement. The percent symbol % is a actually a method used to build dependencies. The double percent sign %% injects the current Scala version - this allows you to get the correct library for the version of scala you are running.

What does sbt reload do?

object Reload extends CancelWatch with Product with Serializable. Action that indicates that the watch should pause while the build is reloaded. This is used to automatically reload the project when the build files (e.g. build. sbt) are changed.

How do I run a jar file in sbt?

A JAR file created by SBT can be run by the Scala interpreter, but not the Java interpreter. This is because class files in the JAR file created by sbt package have dependencies on Scala class files (Scala libraries), which aren't included in the JAR file SBT generates.


1 Answers

I can't give you a full solution right now, but there is a setting called resourceDirectories to which you could add the res folder.

[EDIT] For me it didn't work also if the resource was in the standard resource folder. Please try it that way:

Main.class.getClassLoader().getResourceAsStream("icon.png")

[EDIT2] This is the full build script (build.scala) which works if your resource is in src/main/java:

import sbt._
import Keys._

object TestBuild extends Build {

  lazy val buildSettings = Seq(
    organization := "com.test",
    version := "1.0-SNAPSHOT",
    scalaVersion := "2.9.1"
  )

  lazy val test  = Project(
    id = "test",
    base = file("test"),
    settings = Defaults.defaultSettings ++ Seq(resourceDirectory in Compile <<= javaSource in Compile)
  )
}
like image 122
Christian Avatar answered Sep 21 '22 07:09

Christian