Creating File using `resourceGenerators` - scala

Given:
$cat build.sbt
scalaVersion := "2.12.1"
resourceGenerators in Compile += Def.task {
val file = (resourceManaged in Compile).value / "demo" / "myapp.properties"
val contents = "name=%s\nversion=%s".format(name.value,version.value)
IO.write(file, contents)
Seq(file)
}.taskValue
$sbt compile
[info] Set current project to sbt_testing (in build file:/home/kmeredith/src/sbt_testing/)
[info] Updating {file:/home/kmeredith/src/sbt_testing/}sbt_testing...
[info] Resolving jline#jline;2.14.1 ...
[info] Done updating.
[success] Total time: 1 s, completed Dec 23, 2016 9:14:50 AM
My understanding is that a myapp.properties file should been created during the compile task.
But, I saw no such file created:
$find . -name *.properties
./target/resolution-cache/default/sbt_testing_2.12/0.1-SNAPSHOT/resolved.xml.properties
Please explain to me how to generate the myapp.properties.

You have the wrong expectation regarding the triggering of the resource generation - running compile does not trigger it (compiles existing sources but not generated ones!), but if you execute test or run, it will be generated:
$sbt clean compile
[success] Total time: 0 s, completed Dec 23, 2016 5:39:28 PM
[info] Updating {file:/home/tzachz/dev/sbt_testing/}sbt_testing...
[info] Done updating.
[info] Compiling 25 Scala sources to /home/tzachz/dev/sbt_testing/target/scala-2.11/classes...
[success] Total time: 24 s, completed Dec 23, 2016 5:39:52 PM
$find . -name "*.properties"
zsh: no matches found: *.properties
$sbt run
// ...
$find . -name "*.properties"
./target/scala-2.11/resource_managed/main/demo/myapp.properties
./target/scala-2.11/classes/demo/myapp.properties

Related

Why won't sbt run ScalaTest's example test?

Please assume the normal "newbie" appologies.
I'm using scala 2.12.10; In build.sbt I added ScalaTest:
libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.8" % "test"
I'm adding some scala.js to an existing java project so my scala source paths are normal but I made some empty directories for java and resources and use a different target so there would be no collision with existing code:
javaSource in Compile := baseDirectory.value / "src/main/scalajs_java"
javaSource in Test := baseDirectory.value / "src/test/scalajs_java"
resourceDirectory in Compile := baseDirectory.value / "src/main/scalajs_resources"
resourceDirectory in Test := baseDirectory.value / "src/test/scalajs_resources"
target := baseDirectory.value / "scalajs_target"
I put the ScalaTest example file ExampleSpec.scala in src/test/scala/ExampleSpec.scala. The example ran fine using org.scalatest.run as described in http://www.scalatest.org/.
The test classpath looks reasonable, I thought:
sbt:RedsPro-ScalaJS> show test:fullClasspath
[info] * Attributed(/Users/tballard/git/redspro/reds/scalajs_target/scala-2.12/test-classes)
[info] * Attributed(/Users/tballard/git/redspro/reds/scalajs_target/scala-2.12/classes)
[info] * Attributed(/Users/tballard/.ivy2/cache/org.scala-lang/scala-library/jars/scala-library-2.12.10.jar)
[info] * Attributed(/Users/tballard/.ivy2/cache/org.scala-js/scalajs-library_2.12/jars/scalajs-library_2.12-0.6.31.jar)
[info] * Attributed(/Users/tballard/.ivy2/cache/org.scala-js/scalajs-dom_sjs0.6_2.12/jars/scalajs-dom_sjs0.6_2.12-0.9.7.jar)
[info] * Attributed(/Users/tballard/.ivy2/cache/org.scala-js/scalajs-test-bridge_2.12/jars/scalajs-test-bridge_2.12-0.6.31.jar)
[info] * Attributed(/Users/tballard/.ivy2/cache/org.scala-js/scalajs-test-interface_2.12/jars/scalajs-test-interface_2.12-0.6.31.jar)
[info] * Attributed(/Users/tballard/.ivy2/cache/org.scalatest/scalatest_2.12/bundles/scalatest_2.12-3.0.8.jar)
[info] * Attributed(/Users/tballard/.ivy2/cache/org.scalactic/scalactic_2.12/bundles/scalactic_2.12-3.0.8.jar)
[info] * Attributed(/Users/tballard/.ivy2/cache/org.scala-lang/scala-reflect/jars/scala-reflect-2.12.10.jar)
[info] * Attributed(/Users/tballard/.ivy2/cache/org.scala-lang.modules/scala-xml_2.12/bundles/scala-xml_2.12-1.2.0.jar)
After compiling, the test is where I would expect it:
> ls scalajs_target/scala-2.12/test-classes/
ExampleSpec.class ExampleSpec.sjsir JS_DEPENDENCIES org/
However, sbt apparently isn't convinced there are any tests:
sbt:RedsPro-ScalaJS> show definedTestNames
[info] *
[success] Total time: 1 s, completed Dec 1, 2019, 11:37:51 AM
sbt:RedsPro-ScalaJS> testOnly ExampleSpec
[info] Passed: Total 0, Failed 0, Errors 0, Passed 0
[info] No tests to run for Test / testOnly
[success] Total time: 1 s, completed Dec 1, 2019, 12:44:35 PM
A point in the right direction would be greatly appreciated.
You need to use %%% instead of %% when depending on ScalaTest. In general, you always need to use %%% in Scala.js.

How do I specify specific test from the build.sbt

How do I can specify the test from the build.sbt file , I wanted to run one test only and I used the filter as in the sbt docs, but it doesn't work with me, this is my code I have two test classes and in my sbt I specify test1 to be rub but it seems that the two test are running at the same time any one know what I should do ?
Test1Demo.scala
import org.scalatest.{FlatSpec, Matchers}
class Test1Demo extends FlatSpec with Matchers{
"value of x " should " be 9 " in { assert(my.App.x == 9) }
}
Test2Demo.scala
import org.scalatest.{FlatSpec, Matchers}
class Test2Demo extends FlatSpec with Matchers{
"value of y " should " be 8 " in { assert(my.App2.y == 8) }
}
build.sbt
version := "0.1"
scalaVersion := "2.12.8"
libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.5" % Test
testOptions in Test := Seq(Tests.Filter(s => s.startsWith("Test1")))
the output :
[info] Done updating.
[info] Compiling 2 Scala sources to /home/****/target/scala-2.12/classes ...
[info] Done compiling.
[info] Compiling 2 Scala sources to /home/****/target/scala-2.12/test-classes ...
[info] Done compiling.
[info] Test2Demo:
[info] value of y
[info] - should be 8
[info] Test1Demo:
[info] value of x
[info] - should be 9
[info] Run completed in 6 seconds, 365 milliseconds.
[info] Total number of tests run: 2
[info] Suites: completed 2, aborted 0
[info] Tests: succeeded 2, failed 0, canceled 0, ignored 0, pending 0
[info] All tests passed.
[success] Total time: 264 s, completed Apr 15, 2019 2:47:10 PM
If you want to run value of x test from Test1Demo:
testOnly *Test1Demo -- -z value
This sbt command will run only the tests whose name includes the substring "value".
For exact match rather than substring, use -t instead of -z.
Pay attention to -- (two -, not one)

How to resolve "Ambiguous reference to a JS library"?

I'm struggling resolving some Javascript library dependency clashes in the following build.sbt file:
lazy val root = project.in(file(".")).enablePlugins(ScalaJSPlugin)
scalaVersion := "2.11.7"
libraryDependencies ++= Seq(
"org.scala-js" %%% "scalajs-java-time" % "0.1.0",
"org.querki" %%% "jquery-facade" % "0.10"
)
jsDependencies ++= Seq(
"org.webjars.bower" % "jquery" % "3.0.0-beta1" / "jquery.js"
)
When running the fastOptJS task I'm getting the following error message:
[trace] Stack trace suppressed: run last compile:resolvedJSDependencies for the full output.
[error] (compile:resolvedJSDependencies) org.scalajs.core.tools.jsdep.JSLibResolveException: Some references to JS libraries could not be resolved:
[error] - Ambiguous reference to a JS library: jquery.min.js
[error] Possible paths found on the classpath:
[error] - META-INF/resources/webjars/jquery/3.0.0-beta1/dist/jquery.min.js
[error] - META-INF/resources/webjars/jquery/2.1.4/jquery.min.js
[error] originating from: root:compile
[error] - Ambiguous reference to a JS library: jquery.js
[error] Possible paths found on the classpath:
[error] - META-INF/resources/webjars/jquery/3.0.0-beta1/src/jquery.js
[error] - META-INF/resources/webjars/jquery/3.0.0-beta1/dist/jquery.js
[error] - META-INF/resources/webjars/jquery/2.1.4/jquery.js
[error] originating from: root:compile, root:compile
[error] Total time: 1 s, completed Mar 2, 2016 12:36:43 PM */
How can I resolve the ambiguous references to Javascript libraries in this case?
Based on this Github issue, I learned that it is possible to specify a qualifying path to disambiguate references to Javascript libraries.
Let's try to disambiguate the jquery.js reference by adding the path dist.
jsDependencies ++= Seq(
"org.webjars.bower" % "jquery" % "3.0.0-beta1" / "dist/jquery.js"
)
But running fastOptJS again returns the same error
[error] - Ambiguous reference to a JS library: jquery.min.js
[error] Possible paths found on the classpath:
[error] - META-INF/resources/webjars/jquery/3.0.0-beta1/dist/jquery.min.js
[error] - META-INF/resources/webjars/jquery/2.1.4/jquery.min.js
[error] originating from: root:compile
[error] - Ambiguous reference to a JS library: jquery.js
[error] Possible paths found on the classpath:
[error] - META-INF/resources/webjars/jquery/3.0.0-beta1/src/jquery.js
[error] - META-INF/resources/webjars/jquery/3.0.0-beta1/dist/jquery.js
[error] - META-INF/resources/webjars/jquery/2.1.4/jquery.js
[error] originating from: root:compile
[error] Total time: 2 s, completed Mar 2, 2016 7:29:08 PM
This problem is actually created by the libraryDependencies entry "org.querki" %%% "jquery-facade" % "0.10". Using the dependency graph SBT plugin we see the full dependency graph:
[info] root:root_sjs0.6_2.11:0.1-SNAPSHOT [S]
[info] +-org.querki:jquery-facade_sjs0.6_2.11:0.10 [S]
[info] | +-org.querki:querki-jsext_sjs0.6_2.11:0.6 [S]
[info] | | +-org.scala-js:scalajs-library_2.11:0.6.5 (evicted by: 0.6.7)
[info] | | +-org.scala-js:scalajs-library_2.11:0.6.7 [S]
[info] | |
[info] | +-org.scala-js:scalajs-dom_sjs0.6_2.11:0.8.0 [S]
[info] | | +-org.scala-js:scalajs-library_2.11:0.6.0 (evicted by: 0.6.7)
[info] | | +-org.scala-js:scalajs-library_2.11:0.6.5 (evicted by: 0.6.7)
[info] | | +-org.scala-js:scalajs-library_2.11:0.6.7 [S]
[info] | |
[info] | +-org.scala-js:scalajs-library_2.11:0.6.5 (evicted by: 0.6.7)
[info] | +-org.scala-js:scalajs-library_2.11:0.6.7 [S]
[info] | +-org.webjars:jquery:2.1.4
[info] |
[info] +-org.scala-js:scalajs-java-time_sjs0.6_2.11:0.1.0 [S]
[info] | +-org.scala-js:scalajs-library_2.11:0.6.6 (evicted by: 0.6.7)
[info] | +-org.scala-js:scalajs-library_2.11:0.6.7 [S]
[info] |
[info] +-org.scala-js:scalajs-library_2.11:0.6.6 (evicted by: 0.6.7)
[info] +-org.scala-js:scalajs-library_2.11:0.6.7 [S]
[info] +-org.webjars.bower:jquery:3.0.0-beta1
The output shows org.querki:jquery-facade_sjs0.6_2.11:0.10 to depend on org.webjars:jquery:2.1.4. This explains the Ambiguous reference to a JS library error message above because we still have two jquery library versions available in the listed dependencies.
What we can try next is use exclude on the library dependency.
libraryDependencies ++= Seq(
"org.scala-js" %%% "scalajs-java-time" % "0.1.0",
"org.querki" %%% "jquery-facade" % "0.10" exclude("org.webjars","jquery")
)
Running the fastOptJS task now returns
[error] (compile:resolvedJSDependencies) org.scalajs.core.tools.jsdep.JSLibResolveException: Some references to JS libraries could not be resolved:
[error] - Ambiguous reference to a JS library: jquery.js
[error] Possible paths found on the classpath:
[error] - META-INF/resources/webjars/jquery/3.0.0-beta1/src/jquery.js
[error] - META-INF/resources/webjars/jquery/3.0.0-beta1/dist/jquery.js
[error] originating from: root:compile
[error] Total time: 2 s, completed Mar 2, 2016 7:47:14 PM
So although we got rid of some errors, we are not quite there yet. I'm not 100% sure here, but it seems this error is a result in how the build.sbt of the jQuery Facade includes jQuery using jsDependencies itself:
jsDependencies += "org.webjars" % "jquery" % "2.1.4" / "jquery.js" minified "jquery.min.js"
So it seems we ran into the same problem we had at the start. A unqualified jquery.js (i.e. without a preceding path) cannot not be resolved unambiguously.
To solve this problem we can use to the jsManifestFilter setting. I found this tip in the Scala.js Gitter room.
jsManifestFilter := {
import org.scalajs.core.tools.jsdep.{JSDependencyManifest, JSDependency}
(seq: Traversable[JSDependencyManifest]) => {
seq map { manifest =>
def isOkToInclude(jsDep: JSDependency): Boolean = {
println(s"jsDep=>$jsDep")
jsDep.resourceName != "jquery.js"
}
new JSDependencyManifest(
origin = manifest.origin,
libDeps = manifest.libDeps filter isOkToInclude,
requiresDOM = manifest.requiresDOM,
compliantSemantics = manifest.compliantSemantics
)
}
}
}
Here we overwrite the jsManitestFilter setting and explicitly filter out the naked jquery.js dependency. The fastOptJS task runs fine now:
[info] Resolving org.eclipse.jetty#jetty-continuation;8.1.16.v20140903 ...
[info] Done updating.
jsDep=>JSDependency(resourceName=dist/jquery.js)
jsDep=>JSDependency(resourceName=jquery.js, minifiedResourceName=Some(jquery.min.js))
[info] Fast optimizing jsdeps/target/scala-2.11/root-fastopt.js
[success] Total time: 3 s, completed Mar 2, 2016 8:27:40 PM
Note that the added println statement also outputs the resourceName for the included dependencies.

How to run task before all tests from all modules in sbt

I have a multi-module Play! application built with sbt and I have a problem on CI server with parallel tests and evolutions: when sbt starts tests and first starts evolution script, other fails because of "Database 'default' is in inconsistent state" for a while (until evolution script stops).
I know how to execute evolutions manually and how to run them from sbt, but I don't know how can I plug this configuration to sbt to ensure evolutions are applied only once and before all tests from all modules.
For reference I'm using following for running evolutions:
object ApplyEvolutions extends App {
OfflineEvolutions.applyScript(
new File("."),
this.getClass.getClassLoader,
args.headOption.getOrElse("default"))
}
And my sbt settings:
val runEvolution = taskKey[Unit]("Applies evolutions")
lazy val runEvolutionTask = runEvolution := {
val log = streams.value.log
val dbName = "default"
Run.executeTrapExit( {
log.info(s"Applying evolutions for database $dbName")
Run.run("controllers.ApplyEvolutions",
(fullClasspath in Compile).value.map { _.data },
Seq(dbName),
log
)(runner.value)
}, log )
}
lazy val runEvolutionsBeforeTests = Seq(
Tasks.runEvolutionTask,
(test in Test) <<= (test in Test) dependsOn Tasks.runEvolution
)
tl;dr Use alias
I use SBT 0.13.2-M3.
[task-dependson]> about
[info] This is sbt 0.13.2-M3
[info] The current project is {file:/Users/jacek/sandbox/so/task-dependsOn/}task-dependson 0.1-SNAPSHOT
[info] The current project is built against Scala 2.10.3
[info] Available Plugins: sbt.plugins.IvyModule, sbt.plugins.JvmModule, sbt.plugins.GlobalModule, com.typesafe.sbt.SbtGit, com.typesafe.sbt.SbtProguard, growl.GrowlingTests, org.sbtidea.SbtIdeaPlugin, sbtman.Plugin, np.Plugin, com.timushev.sbt.updates.UpdatesPlugin
[info] sbt, sbt plugins, and build definitions are using Scala 2.10.3
Here's an example of a task - sayHelloT - that's executed once before test in all subprojects. I use Specs2 as the testing framework.
import java.util.concurrent._
lazy val a = project
lazy val b = project
lazy val sayHelloT = taskKey[Unit]("Say Hello")
sayHelloT := {
val log = streams.value.log
log.info("Sleeping for 5 secs...")
TimeUnit.SECONDS.sleep(5)
log.info("Hello, my friend")
}
libraryDependencies in ThisBuild += "org.specs2" %% "specs2" % "2.3.10" % "test"
scalacOptions in Test ++= Seq("-Yrangepos")
addCommandAlias("sht", ";sayHelloT; test")
As you can see there are two subprojects - a and b - and a single alias sht that will execute sayHelloT and only after it finishes successfully which takes 5 secs, test kicks in.
[task-dependson]> sht
[info] Sleeping for 5 secs...
[info] Hello, my friend
[success] Total time: 5 s, completed Mar 12, 2014 10:19:39 PM
[info] ASpec
[info]
[info] A project should
[info] + contain 11 characters
[info]
[info] Total for specification ASpec
[info] Finished in 205 ms
[info] 1 example, 0 failure, 0 error
[info] Passed: Total 0, Failed 0, Errors 0, Passed 0
[info] No tests to run for task-dependson/test:test
[info] Passed: Total 1, Failed 0, Errors 0, Passed 1
[info] BSpec
[info]
[info] B project should
[info] + contain 11 characters
[info]
[info] Total for specification BSpec
[info] Finished in 240 ms
[info] 1 example, 0 failure, 0 error
[info] Passed: Total 1, Failed 0, Errors 0, Passed 1
[success] Total time: 3 s, completed Mar 12, 2014 10:19:42 PM

Play 2.0 - Compilation of twitter bootstrap in test

I have a project where I want to compile twitter bootstrap as well as to prevent tests to be executed in parallel
def twitterBootstrapEntryPoints(base:File):PathFinder = {
(base / "app" / "assets" / "stylesheets" / "bootstrap" * "bootstrap.less") +++
(base / "app" / "assets" / "stylesheets" / "bootstrap" * "responsive.less") +++
(base / "app" / "assets" / "stylesheets" * "*.less")
}
val common = PlayProject(appName, appVersion,appDependencies, mainLang = SCALA).settings(
organization := appOrganization,
lessEntryPoints <<= baseDirectory(twitterBootstrapEntryPoints),
resolvers ++= commonResolvers).settings( inConfig(Test)(parallelExecution := false) : _* )
Edit:
To build twitter bootstrap, one should only compile bootstrap.less and responsive.less which import all the other files. in Compile everything works fine, when running in Test this does not work anymore, the compiler tries to compile all the .less files
This is what I see on the play console
[GottwareWeb] $ clean
[success] Total time: 0 s, completed 26 sept. 2012 17:44:07
[GottwareWeb] $ compile
[info] Updating {file:/G:/GottwareWeb/}GottwareWeb...
[info] Resolving org.hibernate.javax.persistence#hibernate-jpa-2.0-api;1.0.1.Fin
[info] Done updating.
[info] Compiling 34 Scala sources and 1 Java source to G:\GottwareWeb\target\sca
la-2.9.1\classes...
[success] Total time: 9 s, completed 26 sept. 2012 17:44:17
[GottwareWeb] $ test
[error] {file:/G:/GottwareWeb/}GottwareWeb/*:play-copy-assets: in G:\GottwareWeb
\app\assets\stylesheets\accordion.less - PlayException: Compilation error [varia
ble #baseLineHeight is undefined]
[error] {file:/G:/GottwareWeb/}GottwareWeb/compile:resources: in G:\GottwareWeb\
app\assets\stylesheets\accordion.less - PlayException: Compilation error [variab
le #baseLineHeight is undefined]
[error] Total time: 0 s, completed 26 sept. 2012 17:44:32
[GottwareWeb] $
Parallel execution of tests is set to false by default. You shouldn't have to include it in the configuration.
Documentation here: https://github.com/playframework/Play20/wiki/SBTSettings
In your setup, Play must be compiling all the less files in app/assets/stylesheets in production as well. Is this what you don't want? Or is it compiling everything in app/assets/stylesheets/bootstrap/ ?