Run sbt project in debug mode with a custom configuration - scala

I want to introduce a debug mode in my sbt 0.11 project using a special configuration.
I've tried to implement this using the following code but unfortunately, it doesn't seems to work as expected. I'm launching debug:run but the run doesn't suspends as expected.
object Test extends Build {
lazy val root = Project("test", file("."))
.configs( RunDebug )
.settings( inConfig(RunDebug)(Defaults.configTasks):_*)
.settings(
name := "test debug",
scalaVersion := "2.9.1",
javaOptions in RunDebug += "-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005",
fork in RunDebug := true
)
lazy val RunDebug = config("debug").extend( Runtime )
}

Ok that works with the following :
object Test extends Build {
lazy val root = Project("test", file("."))
.configs( RunDebug )
.settings( inConfig(RunDebug)(Defaults.configTasks):_*)
.settings(
name := "test debug",
scalaVersion := "2.9.1",
javaOptions in RunDebug ++= Seq("-Xdebug", "-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005"),
fork in RunDebug := true
)
lazy val RunDebug = config("debug").extend( Runtime )
}
now I can run my code in debug mode using debug:mode.

for simply running sbt project in debug mode , just do
JAVA_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005
and then
sbt run would run SBT in debug mode, you can create a remote debug configuration in eclipse and connect to it.
this is a rather lame, but useful when you have a multi module play project and want to run one of the modules in the debug mode

In Intellij IDEA, I just boot the program in Dedug mode and it seems to work properly without further configuration.

Related

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

no tests were executed for sbt integration test in system folder

I want to run sbt system:test to run the integration tests in system folder. Here's the build.sbt changes, but it the console log still says No tests were executed. any ideas why? thanks!!
lazy val `myproj` = (project in file(".")).enablePlugins(PlayScala).configs(SystemTest).settings(
inConfig(SystemTest)(Defaults.testSettings),
// other settings here
)
lazy val SystemTest = config("system") extend Test describedAs "System tests"
Try this
lazy val root = (project in file("."))
.enablePlugins(PlayScala)
.configs(SystemTest)
.settings( inConfig(SystemTest)(Defaults.testSettings) : _*)
lazy val SystemTest = config("system") extend(Test)
scalaSource in SystemTest := baseDirectory.value / "/system"
The last line is to give the path of the folder where yours test cases are.
You can see this blog as well.
Hope this helps!
Play/SBT offer You integration tests "out of the box" with command sbt it:test and below configuration:
lazy val root = (project in file("."))
.configs(IntegrationTest)
.settings(
Defaults.itSettings,
)

Set java options for running playframework project in sbt

I can run with overriden java options my playframework application using:
run -Dprop1=val1 -Dprop2=val2 . It works perfectly.
But when I'm trying to do it via sbt build - props are not available.
So, I have scala file where my propject defined:
object PrjBuild extends Build {
val runSettings = Seq(
fork in run := true,
(javaOptions in run) ++= Seq(
"-Dprop1=val1",
"-Dprop2=val2")
)
lazy val root = Project(id = "my-play-project",
base = file("."),
settings = Seq(
// some options here ...
routesGenerator := InjectedRoutesGenerator
) ++ runSettings
).enablePlugins(PlayScala)
}
Please advice what am I doing wrong.
I was relying on sbt fork documentation:
http://www.scala-sbt.org/0.13/docs/Forking.html
But actually I would prefer to work without forking as run -Dp1=v1 does not use forking. I want to perform same props setup but programmatically.
When you use run without forking, you are using the same JVM as SBT.
If you want some props to be available in this JVM, you need to start SBT with them:
sbt -Dprop1=val1 -Dprop2=val2

hot swap in sbt project without play-plugin

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.

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)