How to copy resources with scala + play + sbt - scala

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/

Related

Can I create a proto jar for scalaVersion 2.11/2.12 and use it within the same sbt under different sub-project?

I have a set of .proto files (protobuf) which I generate java from using scalapb. I also have in the same sbt 2 sub-projects, one is scalaVersion 2.11 compatible (can't upgrade it to 2.12 due to missing packages) and the other one is scala 2.12.
I created a sub-project to hold my proto, and by default 2.12 is used and my 2.12 sub-project can use it, but my 2.11 can't.
I set the crossScalaVersions to 2.11/2.12, I compiled my project with both, which passed, but then even then I was unable to get the 2.11 sub-project to find that code.
I am "wondering" if that is something supported, or if there is a track I could use a single location to hold my .proto yet have my 2 sub-projects using the same sbt file use those.
lazy val scala212 = "2.12.13"
lazy val scala211 = "2.11.12"
lazy val supportedScalaVersion = List(scala212, scala211)
ThisBuild / scalaVersion := scala212
lazy val root = (project in file("."))
.aggregate(proto, subproject1, subproject2)
.settigns(
crossScalaVersions := Nil,
publish / skip := true
)
lazy val proto = project
.settings(
crossScalaVersions := supportedScalaVersions,
name := "proto",
libraryDependencies += "com.trueaccord.scalapb" %% "scalapb-runtime" % com.trueaccord.scalapb.compiler.Version.scalapbVersion % "protobuf",
PB.targets in Compile := Seq(
scalapb.gen(grpc = false) -> (sourceManaged in Compile).value / "protobuf"
)
)
lazy val subproject1 = project
.dependsOn(proto)
lazy val subproject2 = project
.settings(
scalaVersion := scala211
)
.dependsOn(proto)
So, from the above, if I do sbt "+ proto" I can compile both versions. If I do sbt subproject1/compile it works too. Using sbt subproject2/compile fails indicating that it cannot find the 2.11:proto jar file.
Either, I would like the above somehow to work nicely, or any other trick that I could generate the code from the same proto location but within subproject1/subproject2 would be appreciated.
You could try the sbt-projectmatrix plugin:
https://github.com/sbt/sbt-projectmatrix
The idea is to have separate sbt subprojects for the different Scala versions, so you can simply reference the relevant subproject when calling dependsOn.
I think this plugin is going to end up in sbt some day as it's a much better solution in general than the current built-in stateful cross compilation support, and it's developed by Eugene Yokota, who is also an sbt developer.

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.

Multi module Scala Play 2.3 conf location

I've been tasked with rewriting an old ant build script to SBT. As it happens, our suite is built up of 3 modules:
A Play 2.3 front-end webserver;
A back-end for retrieving data from various other systems;
A middle module containing some shared classes for database access and business logic.
Below an excerpt of my Build.scala file can be found:
val sharedSettings = Seq(
organization := <organization here>,
version := "1.2.5",
scalaVersion := "2.11.1",
libraryDependencies ++= libraries,
unmanagedJars in Compile ++= baseDirectory.value / "lib",
unmanagedJars in Compile ++= baseDirectory.value / "src",
unmanagedJars in Compile ++= baseDirectory.value / "test"
)
lazy val middle = project.settings(sharedSettings: _*)
lazy val back = project.settings(sharedSettings: _*).dependsOn(middle)
lazy val front =
project
.enablePlugins(play.PlayScala)
.settings(sharedSettings: _*)
.settings(scalaSource in Compile := baseDirectory.value / "app")
.settings(
routesImport ++= Seq(
"scala.language.reflectiveCalls", // Removes warnings when using multiple routes files
"com.asml.cerberus.front.toolbox.Binders._")
)
.dependsOn(middle % "compile->compile;test->test")
I've got my application.conf in the ./front/conf/ directory. Unfortunately, if I now run sbt, it looks for a ./conf/application.conf file. (I've tested this by moving the conf directory.)
Is there any way how I can tell SBT/Play to use the front module's conf directory in stead?
In case that helps, we have a wrapper script around activator (sbt) activatorWrapper:
#!/bin/bash
activator -Dconfig.file=front/conf/application.conf
Then you can start your application with :
$ ./activatorWrapper
You can use system properties to specify an alternative config file. See here for the details.

How to enable SbtWeb in not-play project?

I have a single-project build, implemented in Build.scala file with the following settings:
scala
lazy val root = Project(
id = ProjectInfo.name,
base = file("."),
settings = Project.defaultSettings
++ Revolver.settings
++ Revolver.enableDebugging(port = 5050)
++ Twirl.settings
++ // more tasks omitted
++ Seq(
mainClass in Compile := Some(launcherClassName),
mainClass in Revolver.reStart := Some(launcherClassName),
javaOptions in Revolver.reStart ++= List(
"-XX:PermSize=256M",
"-XX:MaxPermSize=512M",
"-Dlogback.debug=false",
"-Dlogback.configurationFile=src/main/resources/logback.xml"
),
resolvers ++= projectResolvers,
libraryDependencies ++= Dependencies.all,
parallelExecution in Test := false,
)
)
I would like to add sbt-web managed assets processing for the project, as I want to handle coffeescript, less and so on.
I added sbt-coffeescript plugin straight to plugins.sbt file in project folder and actually got it working. So now when I run web-assets:assets I have a coffeescript sample file in /src/main/coffeescript/foo.coffee and it gets compiled to target/web/coffeescript/main/coffeescript/foo.js.
Unfortunately, nothing gets processed when I simply run compile or run task. How do I enable processing of assets during compile in development workflow?
The issue you're having is that the old-style of specifying dependencies in projects does not work with AutoPlugins (which is what the WebPlugin is).
Specifically:
val foo = Project(
id = "ok"
base = file("ok")
settings = defaultSettings // BAD!
)
i.e. if you manually place settings on the Project, you're telling sbt "I Know EVERY setting I want on this project, and I want to completely override the defaults."
The load order of sbt settings is:
AutoPlugins (Core settings now come from AutoPlugins)
Settings defined in Project instances
Settings defined in build.sbt files in the base directory of a project.
The above code is re-applying ALL of the sbt default settings from 0.13.x series, which will overwrite anything that the AutoPlugins previously enabled. This is by design, as any other mechanism wouldn't be "correct".
If you're migrating to using AutoPlugins, simply modify your build to be:
lazy val root = Project(
id = ProjectInfo.name,
base = file("."))
settings =
// NOTICE we dropped the defaultSettings
Revolver.settings
++ Revolver.enableDebugging(port = 5050)
++ Twirl.settings
++ // more tasks omitted
++ Seq(
mainClass in Compile := Some(launcherClassName),
mainClass in Revolver.reStart := Some(launcherClassName),
javaOptions in Revolver.reStart ++= List(
"-XX:PermSize=256M",
"-XX:MaxPermSize=512M",
"-Dlogback.debug=false",
"-Dlogback.configurationFile=src/main/resources/logback.xml"
),
resolvers ++= projectResolvers,
libraryDependencies ++= Dependencies.all,
parallelExecution in Test := false,
)
)
To run assets generation on compilation I did this:
settings = ... ++ Seq(
pipelineStages := Seq(rjs),
(compile in Compile) <<= compile in Compile dependsOn (stage in Assets),
// ...
)
Than when I run compile, stage command is also executed, thus running sbt-web's pipeline.
The question for me is how to make generated assets to become available as part of managed resources (I'm trying to get sbt-web working with xsbt-web-plugin and liftweb)

Change output directory of sbt

I want to change my output directory for some generated files, in this case generated objects from an XSD-Schema.
Here is part of my Build file.
val main = PlayProject(appName, appVersion, appDependencies, mainLang = SCALA,
settings = Defaults.defaultSettings ++ buildInfoSettings ++ scalaxbSettings
).settings(
sourceGenerators in Compile <+= buildInfo,
buildInfoKeys := Seq[BuildInfoKey](name, version, scalaVersion, sbtVersion),
buildInfoPackage := "hello",
packageName in scalaxb in Compile := "models",
sourceGenerators in Compile <+= scalaxb in Compile
)
This code puts my generated files into the below directory:
target/scala-2.10/src_managed/main/models/
How can I change my buildfile to output the files to below instead?
/app/models/
Check out the sourceManaged setting key. Any source generator tasks will generally put stuff in the file specified by that setting.
source-managed - target/scala-2.10/src_managed
compile:source-managed - target/scala-2.10/src_managed/main
test:source-managed - target/scala-2.10/src_managed/test
Note that the "compile" and "test" values base themselves off of the base "source-managed" value, which is in turn based on the value of cross-target, which is based on the value of target and a few others.
You can easily change the value of the compile:source-managed setting in an sbt build definition with the setting
sourceManaged in Compile := file("app/models")
If you want to base your setting off of another setting, like the project's base directory, you could use something more like
sourceManaged in Compile <<= baseDirectory { _ / "app/models" }
Of course, you can find plenty of info on using settings here: http://www.scala-sbt.org/release/docs/Getting-Started/More-About-Settings
edit: Looks like that link is dead. It's been a few years so I'm not 100% sure, but this is probably close to what the original link talked about: SBT 0.13 -
Build definition or SBT 1.0 - Build definition