Excluding a src file from sbt compile? - scala

I've been trying to exclude a file from "sbt run" and "sbt compile". The file is called SheetsQuickstart.java, and is in src/main/java. I'm using intelliJ's sbt shell to run these commands. (Perhaps it's not reading the build.sbt file?)
In build.sbt, I've tried lines like:
excludeFilter in (Compile, managedSources) := "SheetsQuickstart.java"
excludeFilter in managedSources := "SheetsQuickstart.java"
excludeFilter in unmanagedSources := "SheetsQuickstart.java"
excludeFilter in managedSources := "SheetsQuickstart.class"
excludeFilter in managedSources := "*.java" `
I still get compilation errors in SheetsQuickstart.java when I compile using sbt, despite having tried to exclude it.

Related

SBT-java.lang.RuntimeException: No main class detected

sbt compile gives Success
sbt run gives the error mentioned above.
My Directory Structure is a little bit different from the regular SBT structure:
Directory structure that I need... Build.sbt inside main project
Build.sbt inside SubProject
MainClass.scala
object MainClass extends App {
println("Hello world!")
}
Note: Things I have already tried in Build.sbt of main project:
1. scalaSource in (Compile, run) := baseDirectory.value / "App" / "js"
2. mainClass in (Compile, run) := Some("MainClass")
3. mainClass in (Compile, run) := Some("App/js/src/main/scala/MainClass")
I am not able to figure out the mistake?
It is not possible to declare additional projects in .sbt files that are in subdirectories. All projects have to be declared in .sbt files at the root of build.
This means that your AppJs and AppJvm never get to have any effect, and those projects do not actually exist in your build.
You'll have to declare AppJs, AppJvm, and any other project you need in the top-level build.sbt file.

Get rid of main-{scalaVersion}-Directories in sbt

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

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 ?

How to copy resources with scala + play + sbt

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/

How do I get sbt 0.10.0 to compile files in a subdirectory?

I have a build.sbt file in my project root.. all my source files live in the subdirectory src (and src/irc, src/xmpp).
Here is my build.sbt
name := "mrtoms"
organization := "chilon"
scalaVersion := "2.9.0"
version := "0.1"
libraryDependencies ++= Seq("commons-httpclient" % "commons-httpclient" % "3.1")
crossPaths := false
scalaHome := Some(file("/usr/share/scala"))
target := file("project/target")
sourceDirectory := file("src")
mainClass := Some("org.chilon.mrtoms.MrToms")
However sbt always just makes an empty jar file.
I tried putting build.sbt inside the "src" directory but then it missed out all the scala files in subdirectories of "src".
Seems that you need to provide path relative to the base directory. This should work for you (it replaces sourceDirectory := file("src")):
scalaSource in Compile <<= baseDirectory(_ / "src")
Some more information you can find in this thread:
http://groups.google.com/group/simple-build-tool/browse_thread/thread/095e87247d146fa7?fwc=1
If you want to replace the default convention then you need to override both scala and java source locations
scalaSource in Compile <<= baseDirectory(_ / "src")
javaSource in Compile <<= baseDirectory(_ / "src")