sbt - add unmanaged resources to console - scala

In sbt, I want to add a config directory to the runtime class-path (but not export it as part of package). So I have this:
unmanagedClasspath in Runtime += baseDirectory.value / "config"
This works fine for sbt run, but apparently is not on the class-path if I use sbt console.
How can I add this directory to the class-path for the console task without it showing up in the exported jar?
Edit: I also tried the following, but I still cannot get the resources:
unmanagedClasspath in (Compile, console) += baseDirectory.value / "config"

Actually adding the following does work:
unmanagedClasspath in Compile += baseDirectory.value / "config"
I had found the contents in "config" only because the package was created previously and using sbt clean package shows that now the contents of "config" will not be packaged any longer but do appear on the console class-path.

Related

"Filename too long" in sbt assembly inside a docker container

I have a scala Play project and I need to create a fat jar in the docker build time but I get this error:
[warn] Error extracting zip entry [...] (File name too long)
I tried adding the option scalacOptions ++= Seq("-Xmax-classfile-name","72") in build.sbt but doesn't works. I tried also appending -Xmax-classfile-name=72 to sbt assembly with the same result.
As I need to do it in docker build time, I can't use a mounted volume as mentioned here https://github.com/sbt/sbt-assembly/issues/69#issuecomment-196901781
What do I need to do to fix this?
In /project/plugins.sbt
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.13.0")
and in build.sbt
assemblyJarName in assembly := "jarname.jar"
target in assembly := baseDirectory.value
Then run command "assembly" from project root and should generate the jar file.

How to add custom directory to Scala SBT project?

We have conf directory in our project which is fine, but one of the new components requires its own config to be placed into config directory and we can't change that requirement. In my build.scala I added the line:
unmanagedResourceDirectories in Compile += baseDirectory.value / "config",
In SBT when I run $ inspect tree package-src I can see the config directory under compile:unmanagedResourceDirectories section which is under compile:packageSrc::mappings section.
Then I try to check the deployment package locally by running $ publishLocal. The target config directory is missing in the result .zip package.
What did I miss / do wrong?
This is what I added to my Build.scala to finally solve the issue:
mappings in Universal ++= (baseDirectory.value / "config" * "*" get) map
(x => x -> ("config/" + x.getName)),

How do I add additional file to class path which is not java or scala file using SBT configuration?

How do I add additional file to classpath which is not java or scala file using SBT configuration ?
My source folder is defined like this
javaSource in Compile := baseDirectory.value / "src"
I have jawr.properties in the root of my /src folder. I'd like this file to be copied to WEB-INF/classes of my packaged app. I tried changing filter to
includeFilter in (Compile, unmanagedSources) := "*.java" || "*.scala" || "jawr.properties",
But it fails on sbt compile because it tries to compile it as java file.
I'm on SBT 0.13.6
The philosophy of SBT is to work by convention (and not by configuration) as much as possible. So the most straightforward solution, in many cases, isn't to look for the correct setting to tell SBT where your files are... But rather to figure out where SBT already expects to find them. You can check this page of the "getting started with SBT" guide for the basics.
For resource files that needs to be packaged together with compiled classes, the default directory is src/main/resources (a convention borrowed from Maven, like most of SBT's default directory structure). Similarly, files in src/test/resources are added to the classpath but only during tests.
If, for some reason, you want to use non-standard directories, you will want to have a look at this page of the documentation. For resources, the key to modify is resourceDirectory:
// resources in `resources` instead of `src/main/resources` :
resourceDirectory in Compile := baseDirectory.value / "resources"
// test resources in `test-resources` instead of `src/test/resources` :
resourceDirectory in Test := baseDirectory.value / "test-resources"
You want it be an unmanaged resource (not source)
unmanagedResourceDirectories in Compile := Seq(baseDirectory.value / "src")
includeFilter in unmanagedResources := "jawr.properties"

Setting scaladoc's output directory in SBT 0.13 (since docDirectory is deprecated)?

It seems docDirectory in Compile <<= (baseDirectory / "api") is deprecated and sbt won't start if build.sbt contains the setting.
How should I set scaladoc's output directory in sbt 0.13?
Use the following setting instead:
target in Compile in doc := baseDirectory.value / "api"

How to configure sbt to load resources when running application?

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" }
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)
)
}