I try to add another source directory to my play project with
def temporarySources = Def.setting { (baseDirectory in Compile).value / "temporary" }
and then add to settings
(unmanagedSources in Compile) += temporarySources.value
after running sbt test, sbt complains , that the directory doesn't exist, although i'm sure it is there (i can cd to the directory)
I guess you need unmanagedSourceDirectories and not unmanagedSources, try the following setting:
unmanagedSourceDirectories in Compile += (baseDirectory.value / "temporary"),
unmanagedSourceDirectories in Test += (baseDirectory.value / "temporary")
Related
Reading the following SBT documentation:
https://www.scala-sbt.org/1.x/docs/Appending-Values.html
And also
https://www.scala-sbt.org/1.x/docs/Classpaths.html
I can get on how to create a test source folder with only utils classes to be consume both by the tests in the same project and in all other projects that depend on it.
I tried the following but got compilation issues:
Test / sourceDirectories := Seq( baseDirectory.value / "testFixtures", baseDirectory.value / "test")
And also the following but this time sbt does not find any tests in the project:
sourceDirectory in Test := baseDirectory.value / "test"
Test / unmanagedSourceDirectories += baseDirectory.value / "testFixtures"
any idea ?
Thanks
I've changed the default src directory according to: http://www.scala-sbt.org/0.13.2/docs/Howto/defaultpaths.html
to:
scalaSource in Compile := baseDirectory.value / "src/main"
javaSource in Compile := baseDirectory.value / "src/main"
scalaSource in Test := baseDirectory.value / "src/test"
javaSource in Test := baseDirectory.value / "src/test"
resourceDirectory in Compile := baseDirectory.value / "res"
resourceDirectory in Test := baseDirectory.value / "res/test"
Now whenever intellij/idea reloads it adds back main-2.11 and test-2.11 folders.
I want to get rid of those, but I've not found a way for now. Any ideas?
edit: I've already deleted the whole .idea and other folders for IntelliJ and re-imported the project with the .sbt file. Still no luck. On every startup or change of the .sbt these annoying folders are re-created. Grrr!
The problem might rather lie with sbt. If you open sbt console and enter
show sourceDirectories
the result might still include the scala-2.11 folders.
If so the following lines would fix that:
sourceDirectories in Compile <<= (sourceDirectories in Compile) { dirs =>
dirs.filterNot(_.absolutePath.endsWith("-2.11"))
}
sourceDirectories in Test <<= (sourceDirectories in Test) { dirs =>
dirs.filterNot(_.absolutePath.endsWith("-2.11"))
}
I am using sbt [0.13] to compile a play [2.2] project using scala [2.10.3]. I have .sql files and scala files used for database migrations. The directory structure looks like:
app
|-> db
|-> migration
|-> V1__init.scala
|-> V2__newTable.sql
When I run compile from the play console (REPL), the scala file (V1__init.scala) is compiled to a .class and copied to the classes folder. But the .sql file is not moved.
I tried adding unmanagedResourceDirectories in Compile <++= baseDirectory { dir => Seq(dir/"app/db/migration") ++ Seq(dir/"db/migration") } but it did not copy the files. The whole block looks like:
val main = play.Project(appName, appVersion, appDependencies).settings(
scalaVersion := "2.10.3",
scalacOptions ++= Seq("-feature"), // enable feature warnings
unmanagedResourceDirectories in Compile <++= baseDirectory { dir => Seq(dir/"app/db/migration") ++ Seq(dir/"db/migration") }
)
I also tried using copyResources, but couldn't get that to work. Described here: http://www.playframework.com/documentation/2.0/SBTSettings
So does anyone know how I can copy the sql files to the classes folder?
Thanks!
UPDATE
I got IO.copyDirectory(new java.io.File("app/db/migration"), new java.io.File("target/scala-2.10/classes/db/migration"), true) to copy the files, but the destination is hard-coded and will change when I update scala
val main = play.Project(appName, appVersion, appDependencies).settings(
scalaVersion := "2.10.3",
scalacOptions ++= Seq("-feature"), // enable feature warnings
unmanagedResourceDirectories in Compile <+= scalaSource in Compile,
excludeFilter in unmanagedResources in Compile := "*.scala" || "*.java"
)
You can check easily the contents in the class folder with:
sbt clean full-classpath && ls target/scala-2.10/classes/db/migration/
I'm having quite a few troubles pointing at a custom directory for Scala source-files in SBT.
I would like sbt to compile scala-files from a given directory instead of the regular src/main/scala directory.
I have tried both defining a .sbt and .scala project files, setting baseDirectory, scalaSource (and scalaSource s in the .scala file). I've also toyed around with everything from system-absolute to relative paths but nothing seems to work. It cannot locate any .scala file under the specified directory.
What are the proper ways to handle this?
Try this in build.sbt:
scalaSource in Compile <<= (sourceDirectory in Compile)(_ / "foo")
This will result in a directory src/main/foo for Scala sources. If you want to use some arbitrary directory, go for this:
scalaSource in Compile := file("/Users/heiko/tmp")
Update answer for SBT 0.13.13 ->
sourceDirectory in Compile := (baseDirectory( _ / "foo" )).value
And to add a source directory (instead of just replacing it) also for SBT 0.13.13 ->
unmanagedSourceDirectories in Compile += (baseDirectory( _ / "foo" )).value
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)
)
}