custom folder structure in sbt for scala - scala

The default folder Structure of SBT is like this:
src
- main
- scala
- test
- scala
But I want to have a custom folder structure, something like this:
src
- main
- test
So my question is:
How can I customize my build.sbt file, to have a custom folder structure and have Intellij Idea recognize the new structure?

According to https://www.scala-sbt.org/1.x/docs/Howto-Customizing-Paths.html, you would need to apply the following in your build.sbt:
Compile / scalaSource := baseDirectory.value / "src/main"
Test / scalaSource := baseDirectory.value / "src/test"
I have not tried this, so not sure if this works as expected nor how far Intellij Idea supports this (though I'd expect it to fully support this).

Related

variations of sbt directory structure

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.

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.

multiproject sbt doesn't generate file structure

I am new to sbt (using sbt.version=0.13.5) created multiproject build definition as following (build.sbt):
name := "hello-app"
version in ThisBuild := "1.0.0"
organization in ThisBuild := "com.jaksky.hello"
scalaVersion := "2.10.4"
ideaExcludeFolders ++= Seq (
".idea",
".idea_modules"
)
lazy val common = (
Project("common",file("common"))
)
lazy val be_services = (
Project("be-services",file("be-services"))
dependsOn(common)
)
My expectation was that sbt will generate directory layout for the projects (based on the documentation). What happened was that just only top directories were generated (common and be-services) with target folder in it.
I tried it in batch mode sbt compile or in interactive mode - none has generated expected folder structures e.g. /src/{main, test}/{scala, java, resources}.
So either my expectations are wrong or there is some problem in my definition or some speciall setting, plugin etc.
Could some more experienced user clarify that, please?
Thanks
As #vptheron correctly points out, sbt does not generate any project directories, with the exception of the target directory when it produces compiled class files.
You might find that functionality in plugins, e.g. np. Also if you use an IDE such as IntelliJ IDEA, creating a new sbt-based project will initialize a couple of directories (such as src).

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"

How can I have a traditional src/main/scala directory in a Play Application?

I wish to move some of my normal SBT Scala code across to my Play Application. So my first thought was I'll just create a src directory and put it there, but it seems not working.
What is the right way to put normal Scala code into the Scala Play Application?
Adding
unmanagedSourceDirectories in Compile += baseDirectory.value / "src" / "main" / "scala"
in your build.sbt should do the trick.
Currently it should be done in the following way:
disablePlugins(PlayLayoutPlugin)
PlayKeys.playMonitoredFiles ++= (sourceDirectories in (Compile, TwirlKeys.compileTemplates)).value
Described here: https://www.playframework.com/documentation/2.6.x/Anatomy#Default-SBT-layout