hot swap in sbt project without play-plugin - scala

When I am using play framework, every time I've changed the code, it will take effect automatically by re-compile the code.
However, when I'm using sbt to run a project without play-plugin, it won't take effect.
I'm wondering if there were a way to make sbt project hot swap the changed code.
My build.sbt is as below:
version in ThisBuild := "1.0-SNAPSHOT"
scalaVersion in ThisBuild := "2.11.6"
lazy val `frontend` = (project in file("frontend")).
enablePlugins(PlayScala).
enablePlugins(DockerPlugin).
settings(
name := "frontend",
libraryDependencies ++= Dependencies.frontend
).dependsOn(`api`).aggregate(`api`)
lazy val `backend` = (project in file("backend")).
enablePlugins(JavaAppPackaging).
enablePlugins(DockerPlugin).
settings(
name := "backend",
libraryDependencies ++= Dependencies.backend ++ Seq(cache, ws)
).dependsOn(`api`).aggregate(`api`)
lazy val `api` = (project in file("api")).
settings(
name := "api",
libraryDependencies += ws
)
And what I have configured in intellij idea is like below as a sbt task(I can't post images by now):
"project backend" ~run
However, every time I've changed the code in backend, It won't take effect after I've call backend from the frontend.
I'm wondering how I can solve the problem. Thanks for your guys' help.

You can have sbt automatically recompiling any changes by invoking it like this:
sbt ~compile

If you use ~run, on every change the changeed classes will be compiled and project rerun again.
If it does not work, you might explain more about your project and structure.

Open two SBT window.
The one run ~compile, and another run ~run.
Hope it will be help.

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.

Sbt generated docker container fails to package subproject

I have a multi-project build.sbt file, with projects like so:
lazy val utils = (project in file("utils"))
.settings(
Seq(
publishArtifact := false
)).[...]
lazy val api = (project in file("api"))
.dependsOn(utils)
.settings(commonSettings: _*)
.enablePlugins(JavaAppPackaging, DockerPlugin)
.settings(publish := {})
.settings(
Seq(
packageName in Docker := "my-api",
dockerBaseImage := "java:8",
mainClass in Compile := Some("com.path.to.Main"),
publishArtifact := false,
unmanagedJars in Compile += file("jars/somejars.jar")
))
API is built on top of Finch framework. I create a docker image for the API using sbt api/docker:publishLocal and then run it locally. However, it seems like the utils subproject classes are not packaged with the final container, and as a result I am getting multiple
java.lang.ClassNotFoundException:
types of exceptions. For a similar project that doesn't have a subproject dependency, everything runs smoothly and I have no problems.
Am I missing something in the plugin configuration? I thought .dependsOn() should be taking care of providing dependent classes in the project docker image.
Answering my own question, but turns out this is a default behaviour of sbt-native-packager, or rather sbt, when a dependent project has publishArtifact := false setting.
A workaround that worked for me was changing the above to publish/skip := true.
More on this issue can be found here: https://github.com/sbt/sbt-native-packager/issues/1221

How to clear sbt cache?

I'm using sbt 1.3.9 and I need to update some libraries that have changed its code but the version stays the same. When I tried to run sbt update command nothing happened the library not downloaded.
I have sbt.build file that looks like the following:
name := """project name"""
organization := "com.example"
version := "1.0-SNAPSHOT"
lazy val root = (project in file(".")).enablePlugins(PlayJava)
lazy val usr = sys.env("MVN_USER")
scalaVersion := "2.13.1"
javacOptions ++= Seq("-source", "11", "-target", "11")
resolvers ++= Seq(
"Jfrog Artifacts".at("https://artifactory.jfrog.com/")
)
credentials += Credentials(
...
)
updateOptions := updateOptions.value.withCachedResolution(false)
updateOptions := updateOptions.value.withLatestSnapshots(false)
libraryDependencies ++= Seq(
guice,
javaWs,
ehcache,
"com.google.api-client" % "google-api-client" % "1.30.7",
"org.apache.commons" % "commons-lang3" % "3.9",
"redis.clients" % "jedis" % "3.2.0"
)
how can I clear sbt cache?
You can just delete the v1 folder.
The default cache location is platform-dependent:
on Linux, ~/.cache/coursier/v1. This also applies to Linux-based CI
environments, and FreeBSD too.
on OS X, ~/Library/Caches/Coursier/v1.
on Windows, %LOCALAPPDATA%\Coursier\Cache\v1, which, for user Alex,
typically corresponds to
C:\Users\Alex\AppData\Local\Coursier\Cache\v1.
I assuming that you are developing something locally and doing publishLocal - normal repository wouldn't let you override published dependency and snapshots are not cached (sbt checks if newer appeared every time you need to build sth).
In such case start using snapshot versions for the future and/or go to ~/.ivy2/your.organisation/library_scalaVersion and remove whole directory with "bad" version. If library is fetched by Maven (with sbt, unlikely these days) it the same idea but with ~/.m2.
I had experienced this while using IntelliJ. After closing IntelliJ, I used to clear .idea from project folder and reimport project into IntelliJ was helping me.
You can use PrettyClean to clean the all of dev tools caches including SBT.
PrettyClean also cleans the SBT project's target folder.

SBT: How to set common scala version for multiproject

I have a multi-SBT-project in IntellJ Idea. My SBT file in the root dir looks like this:
name := "PlayRoot"
version := "1.0"
lazy val shapeless_learn = project.in(file("shapeless_learn")).dependsOn(common)
lazy val scalaz_learn = project.in(file("scalaz_learn")).dependsOn(common)
lazy val common = project.in(file("common"))
lazy val root = project.in(file(".")).aggregate(common, shapeless_learn, scalaz_learn)
scalaVersion := "2.11.7"
Then I have folders for each of the projects: ./common, ./shapeless_learn, ./scalaz_learn and each has its own build.sbt there. But for some reason I require to put in each of the subproject build.sbt files the line scalaVersion := "2.11.7".
If I forget to do that, the build fails with the message:
Error:Unresolved dependencies: common#common_2.10;0.1-SNAPSHOT: not found
See complete log in ...
For some reason if I do not specify that my scala version is 2.11.7, sbt falls back to 2.10 and tries to find common project that is built for 2.10 which I do not have.
I always keep forgetting adding scalaVersion := "2.11.7" to the newly created project and it keeps bugging me. I also would prefer sbt generating build.sbt with some default data, but instead it requires me not to forget to create it manually.
Is there any way I could set the single scala version for all projects and sub-projects in a single place? I figured that I could add a separate lazy val commonSettings = Seq { scalaVersion := "2.11.7" } in a root definition. And for each lazy val project definition I should add in the end .settings(commonSettings). This is nice, but still doesn't look beautiful enough - I should do this for every project definition. Is there a better way?
Is there any way I could create a template for a newly created project, so when I just put line lazy val newProject = ..., it will put an appropriate build.sbt file there with the contents I want?
Use
scalaVersion in ThisBuild := "2.11.7"
in the root build.sbt.

How to run playframework sample in Intelllij 13?

I am using Intellij 13 Ultimate and want to create a Play Framework sample. But I am unable to build this project because it always throws this error:
object index is not a member of package views.html
Ok(views.html.index("Your new application is ready."))
I have tried this on both Mac and Windows platforms, but the error is always the same.
How can I solve this?
The generation works just fine and all the paths are correctly added to the build. However the routes are not (yet) compiled by the plugins (scala+playframework), thus the reverse routing classes generated by play are not available for intellij.
You will have the same problem with the templates.
If you run a play compile (or a sbt compile), the classes will be generated and intellij should be able to pick them up and compile your project.
I found a trick here.
If you generate the play-scala project using activator command line activator [new my_first_project play-scala]. You'll get the following sbt build file.
name := """my_first_project"""
version := "1.0-SNAPSHOT"
lazy val root = (project in file(".")).enablePlugins(PlayScala)
scalaVersion := "2.11.7"
libraryDependencies ++= Seq(
jdbc,
cache,
ws,
"org.scalatestplus.play" %% "scalatestplus-play" % "1.5.1" % Test
)
resolvers += "scalaz-bintray" at "http://dl.bintray.com/scalaz/releases"
But IF you create a project from intellj using NewProject->Scala-Play 2.x, you will get the following sbt.
name := "my_second_project"
version := "1.0"
lazy val `my_second_project` = (project in file(".")).enablePlugins(PlayScala)
scalaVersion := "2.11.7"
libraryDependencies ++= Seq( jdbc , cache , ws , specs2 % Test )
unmanagedResourceDirectories in Test <+= baseDirectory ( _ /"target/web/public/test" )
resolvers += "scalaz-bintray" at "https://dl.bintray.com/scalaz/releases"
fork in run := false
after combine them. Ignore the name. And I set for in run to false
name := "my_second_project"
version := "1.0"
lazy val `my_second_project` = (project in file(".")).enablePlugins(PlayScala)
scalaVersion := "2.11.7"
libraryDependencies ++= Seq( jdbc , cache , ws , specs2 % Test )
unmanagedResourceDirectories in Test <+= baseDirectory ( _ /"target/web/public/test" )
resolvers += "scalaz-bintray" at "https://dl.bintray.com/scalaz/releases"
fork in run := false
After doing that. Open the activator-generated project with Intellj, configure a run with Play 2.x run. Everything goes well.
By the way, if you open the activator-generated project before you change the sbt file. You might need to rm -r .idea
Hope that helps.
Running "play idea" in the new project root fixed it for me. The project should compile and run after reloading.
I only have this problem with projects created through Idea's New Project menu.
I have the same problem: in Idea 13 there are highlight errors there, but in Idea 12 everything is ok. The reason is that there is no Scala plugin for Idea 13, so for now it's impossible to get it to work in Idea 13.
The solution is to install Idea 12, go to Preferences -> Plugins, type "Scala" in find box, and install Scala plugin.