SBT + IntellIj Not Recognising My Integration Test Directory - scala

I am trying to set up integration test folder for my project. So in I added the following to my SBT build definition:
.configs(IntegrationTest)
.settings(
Defaults.itSettings,
IntegrationTest / dependencyClasspath :=
(IntegrationTest / dependencyClasspath).value ++ (Test / exportedProducts).value,
Keys.fork in IntegrationTest := false,
unmanagedSourceDirectories in IntegrationTest := (baseDirectory in IntegrationTest)(base => Seq(base / "it")).value,
resourceDirectory in IntegrationTest := baseDirectory.value / "it/resources",
parallelExecution in IntegrationTest := false
)
The folder structure looks like:
src
|-it
|-scala
But if I start an sbt session and do it:test it does not pick up any of the tests. Also Intellij is not marking the it directory green as well which is the colour of test folders. How do I set this up?

It is most likely that this line is incorrect:
unmanagedSourceDirectories in IntegrationTest := (baseDirectory in IntegrationTest)(base => Seq(base / "it")).value
In majority of the cases, if you are not doing any exotic custom integration setup, the default settings that come with sbt are fine. See this post.

Related

Importing assets from an NPM Package

Say I want to include font-awesome in my webapp. So I define my build.sbt as follows:
val commonSettings = Seq(
name := "repro",
version := "1.0",
scalaVersion := "2.12.8",
unmanagedSourceDirectories in Compile +=
baseDirectory.value / ".." / "shared" / "src" / "main" / "scala"
)
val client = project.in(file("client"))
.settings(commonSettings: _*)
.settings(
npmDependencies in Compile ++= Seq(
"font-awesome" -> "4.7.0",
),
mainClass in Compile := Some("app.App"),
scalaJSUseMainModuleInitializer := true,
webpackBundlingMode := BundlingMode.LibraryOnly(),
)
.enablePlugins(ScalaJSPlugin)
.enablePlugins(ScalaJSBundlerPlugin)
val server = project.in(file("server"))
.settings(commonSettings: _*)
.settings(
npmAssets ++= NpmAssets.ofProject(client) { nodeModules =>
(nodeModules / "font-awesome").allPaths
}.value
)
.enablePlugins(WebScalaJSBundlerPlugin)
Can I configure this project so that my "package" command will then include the css in my target/webapp folder? Or is there another command I have to use?
In addition to your configuration, you have to add the following settings to the server project:
.settings(
scalaJSProjects := Seq(client),
pipelineStages in Assets := Seq(scalaJSPipeline),
managedClasspath in Runtime += (packageBin in Assets).value,
WebKeys.packagePrefix in Assets := "public/"
)
The first line introduces a dependency between the server project and the assets produced by the client project. The scalaJSProjects settings is introduced by the sbt-web-scalajs plugin.
The second line integrates the assets produced by the client project into the Web assets managed by sbt-web.
The third line tells sbt to include the assets produced by the sbt-web plugin to the classpath of the server.
The last line is optional, it simply puts the produced assets into the public/ resource directory, so that they are not mixed with other classpath resources which are not meant to be exposed to the outside world.
With this configuration, you can build the production assets with the following command:
> server/web-assets:package
Or, from a build file, by using the packageBin in Assets task.
This will produce a target/scala-2.12/repro_2.12-1.0-web-assets.jar file containing the JavaScript bundle produced by Webpack on your client project, as well as the font-awesome/ directory.

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

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 ?

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