variations of sbt directory structure - scala

The standard sbt directory structure is like this:
src
main
java
scala
test
java
scala
I have only one java file, and I would like to simplify this structure by eliinating the intermediate java and scala directories so that the .scala files appear directly under main and test. I tried that and had problems with objects not found and such. Is there a way to configure sbt to make this work? Thanks.

Change the default Scala source directory
https://www.scala-sbt.org/1.x/docs/Howto-Customizing-Paths.html#Change+the+default+Scala+source+directory
scalaSource in Compile := baseDirectory.value / "src" / "main"
scalaSource in Test := baseDirectory.value / "src" / "test"
javaSource in Compile := baseDirectory.value / "src" / "main"
javaSource in Test := baseDirectory.value / "src" / "test"
The Scala source directory can be the same as the Java source directory.

Related

Is it possible to have in SBT a Test Fixture source folder?

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

Scala source external to SBT project folder

When creating an SBT scala project in ItelliJ, it wants me to put all source code in the src/main/scala folder of the project folder. However, I want to put the source code somewhere else independent or particular projects, build-tools, IDEs, etc.
How do I tell it to get the source from arbitrary locations?
You can use the following settings to change the location where SBT looks for your Scala sources:
// Change Scala source directory. Default is:
// scalaSource in Compile := baseDirectory.value / "src" / "main" / "scala"
scalaSource in Compile := file("<Path to your sources...>")
// Change Scala test source directory. Default is:
// scalaSource in Test := baseDirectory.value / "src" / "test" / "scala"
scalaSource in Test := file("<Path to your test sources...>")
For further information (including how to specify other source, library and resource directories), refer to the official SBT documentation for customizing paths.

How can I make `javaSource` directory different from `sourceDirectory` / main / java?

I have old and big Java project in which I want to start using Scala. All Java sources are stored in /src. Unfortunately I cannot move them to standard /src/main/java directory because there are tons of old Ant scripts and etc.
So my goal is to have new Scala source and resources in /srcnew and keep Java stuff in /src. So I wrote following in build.scala
sourceDirectory := baseDirectory.value / "srcnew",
javaSource := baseDirectory.value / "src",
And it doesn't work :) SBT doesn't see source in javaSource at all.
What am I missing ?
These are the examples from "Customizing paths" in the SBT docs:
scalaSource in Compile := baseDirectory.value / "src"
scalaSource in Test := baseDirectory.value / "test-src"
javaSource in Compile := baseDirectory.value / "src"
javaSource in Test := baseDirectory.value / "test-src"
So in your case it would be
sourceDirectory in Compile := baseDirectory.value / "srcnew"
javaSource in Compile := baseDirectory.value / "src"

Setting `unmanagedResourceDirectories` breaks deployment of resources from `resourceDirectory`

I have a little bit odd setup. My Java sources are located at
/src
There are .properties files next to some Java classes. I want them to be packaged to final jar.
Scala sources and resources follow SBT convention and are located at
/srcnew/main/[scala|resources]
Here is how my build.scala looks like
sourceDirectory := baseDirectory.value / "srcnew",
unmanagedResourceDirectories in Compile := Seq(baseDirectory.value / "src"),
includeFilter in unmanagedResources := "*.properties",
javaSource in Compile := baseDirectory.value / "src",
Even though resourceDirectory still points to /srcnew/main/resources resources don't make it to final jar (built with sbt-assembly).
I also tried
unmanagedResourceDirectories := Seq(baseDirectory.value / "src", baseDirectory.value / "srcnew/main/resources"),
and surprisingly it doesn't help either.
Turned out that problem was in line
includeFilter in unmanagedResources := "*.properties"
instead it should be
includeFilter in unmanagedResources := ((includeFilter in unmanagedResources).value || "*.properties") -- "*.java",
the only thing I don't understand is why filter on unmanagedResources affects resourceDirectory. Does anyone know ?

Changing Scala sources directory in SBT

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