I'm trying to use Scala IDE. I'm totally new to Scala.
HelloSpec.scala:
import org.scalatest._
class HelloSpec extends FlatSpec with Matchers {
"The Hello object" should "say hello" in {
Hello.greeting shouldEqual "hello"
}
}
import org.scalatest._ is marked by an error:
object scalatest is not a member of package org
I've tried to google it a lot but still don't understand what's wrong. My project structure looks right:
src/main/scala:
----Hello.scala
src/test/scala:
----HelloSpec.scala
build.sbt
plugins.sbt
build.sbt:
import Dependencies._
ThisBuild / scalaVersion := "2.12.8"
ThisBuild / version := "0.1.0-SNAPSHOT"
ThisBuild / organization := "com.example"
ThisBuild / organizationName := "example"
lazy val root = (project in file("."))
.settings(
name := "$name$",
libraryDependencies += scalaTest % Test
)
plugins.sbt:
addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "4.0.0")
I haven't build the project BTW. Is that could be the reason? I don't know how to build the project from Scala IDE, and if I build it from Windows Command Prompt then it doesn't influence Scala IDE.
I assume your project structure is as below,
src/
main/
resources/
<files to include in main jar here>
scala/
<main Scala sources>
test/
resources
<files to include in test jar here>
scala/
<test Scala sources>
build.sbt
plugins.sbt
This is simple hello class
object Hello {
def greeting: String = {
"hello"
}
}
I created the unit testing as below
import org.scalatest._
class HelloSpec extends FlatSpec with Matchers {
"The Hello object" should "say hello" in {
Hello.greeting shouldEqual "hello"
}
}
So, you need just to add scala test in dependcies. Below is simply sbt file
name := "Hello Test"
version := "0.1"
scalaVersion := "2.11.8"
libraryDependencies ++= Seq(
"org.scalatest" %% "scalatest" % "3.0.6" % "test")
Related
I want to create a sbt plugin
this is my project
build.sbt file:
lazy val root = (project in file(".")).
settings(
name := "test-plagin",
version := "0.1.0",
organization := "com.test",
scalaVersion := "2.13.0",
sbtPlugin := true,
)
main file with task
import sbt.{AutoPlugin, TaskKey}
object HelloPlugin extends AutoPlugin {
object autoImport {
val sayHello: TaskKey[Unit] = TaskKey("saying hello")
}
import autoImport._
override def projectSettings = Seq(
sayHello := {
println("hello")
}
)
}
During compiling I get an error:
java.lang.NoClassDefFoundError: scala/collection/immutable/StringOps
When I change the version to 2.12.6 - compiling is success.
How I can fix error in 2.13?
sbt is written in Scala 2.12
https://github.com/sbt/sbt/blob/develop/project/Dependencies.scala#L9
https://github.com/sbt/sbt/issues/5032
So you should use Scala 2.12 for sbt plugins.
I'm trying to workaround creating sbt AutoPlugins.
I want to create plugin which will autoloading all his dependencies, so I use NoTrigger policy.
I wrote my own AutoPlugin which must execute assembly task from sbt-assembly and look like:
settings in /build.sbt
name := "sbt-myplugin"
version := "0.0.1"
organization := "com.org"
scalaVersion := "2.10.6"
sbtPlugin := true
sbtVersion := "0.13.11"
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.14.3")
plugin code in /src/main/scala/myplugin/MyPlugin.scala
package myplugin
import sbt._
import sbtassembly.AssemblyPlugin
import sbtassembly.AssemblyPlugin.autoImport._
object MyPlugin extends AutoPlugin{
override def trigger = noTrigger
override def requires = AssemblyPlugin
object autoImport {
val myAssembly = taskKey[File]("Assembled file")
}
import autoImport._
override lazy val projectSettings = Seq(
myAssembly := assembly.value
)
}
Then i'm create artifact with sbt clean compile publishLocal
After this I created test project which will use my plugin.
settings for this project in /project/plugins.sbt
logLevel := Level.Warn
resolvers += "Local Ivy Repository" at "file://"+Path.userHome.absolutePath+"/.ivy2/local"
addSbtPlugin("com.academmedia.ias" % "sbt-pkplace" % "0.0.1")
settings in /biuld.sbt
name := "test-project"
version := "1.0"
scalaVersion := "2.11.8"
lazy val root = (project in file(".")).enablePlugins(myplugin.MyPlugin)
Now I'm expecting able to use MyPlugin task myAssembly but my project sbt is unable to download project settings with error:
[error] java.lang.NoClassDefFoundError: sbtassembly/AssemblyPlugin$
What am I doing wrong?
Thanks for answer!
I can't figure out how to do benchmarks of Scala programs in Intellij with JMH.
Here's what I've done so far:
Added the JMH SBT-Plugin
// build.sbt
name := "Project"
version := "1.0"
scalaVersion := "2.11.8"
enablePlugins(JmhPlugin)
// project/plugins.sbt
logLevel := Level.Warn
addSbtPlugin("pl.project13.scala" % "sbt-jmh" % "0.2.10")
Here is the plugin's website
Created a benchmark class
// src/main/scala/MyBenchmark.scala
import org.openjdk.jmh.annotations.Benchmark
class MyBenchmark {
#Benchmark
def test(): Unit = println("test")
}
Created an SBT-Task in Intellij
But after running the task I just get an exception:
Annotation generator had thrown the exception.
java.lang.NullPointerException
at org.openjdk.jmh.generators.reflection.RFClassInfo.getPackageName(RFClassInfo.java:51)
at org.openjdk.jmh.generators.core.BenchmarkGenerator.validateBenchmark(BenchmarkGenerator.java:243)
at org.openjdk.jmh.generators.core.BenchmarkGenerator.generate(BenchmarkGenerator.java:90)
....
What am I doing wrong?
Try to add package name in the benchmark class.
Because JMH is complaining about package name is not found.
org.openjdk.jmh.generators.reflection.RFClassInfo.getPackageName(RFClassInfo.java:51)
you should add the line like:
package your.path;
The following build.sbt file works, but it defines the dependencies of all subprojects:
name := "myproject"
version := "1.0"
scalaVersion := "2.11.8"
libraryDependencies ++= Seq(
"org.scalafx" %% "scalafx" % "8.0.60-R9"
)
lazy val aLib = (project in file("lib/a"))
lazy val bLib = (project in file("lib/b"))
.dependsOn(aLib)
.dependsOn(cLib)
lazy val cLib = (project in file("lib/c"))
.dependsOn(aLib)
lazy val myApp = (project in file("myapp"))
.dependsOn(aLib)
.dependsOn(bLib)
.dependsOn(cLib)
.aggregate(aLib, bLib, cLib)
Since each subproject (directories lib/a, lib/b, lib/c, myapp) has its own build.sbt file, I would like to use those build files to define the individual dependencies of each project.
I tried to move the dependsOn/aggregate statements to the subprojects' build files, but I am not able to make it work that way. What is the recommended way?
I have a multi module project in IntelliJ, as in this screen capture shows, contexProcessor module depends on contextSummary module.
IntelliJ takes care of everything once I setup the dependencies in Project Structure.
However, when I run sbt test with the following setup in build.sbt, I got an error complaining that it can't find the packages in contextSummary module.
name := "contextProcessor"
version := "1.0"
scalaVersion := "2.11.7"
libraryDependencies += "org.scalatest" % "scalatest_2.11" % "2.2.2" % "test"
How to teach sbt that the missing modules are found?
I could use the build.sbt file in the main root directory.
lazy val root = (project in file(".")).aggregate(contextSummary, contextProcessor)
lazy val contextSummary = project
lazy val contextProcessor = project.dependsOn(contextSummary)
Reference: http://www.scala-sbt.org/0.13.5/docs/Getting-Started/Multi-Project.html
For testing only one project, I can use project command in sbt.
> sbt
[info] Set current project to root (in build file:/Users/smcho/Desktop/code/ContextSharingSimulation/)
> project contextProcessor
[info] Set current project to contextProcessor (in build file:/Users/smcho/Desktop/code/ContextSharingSimulation/)
> test
For batch mode as in How to pass command line args to program in SBT 0.13.1?
sbt "project contextProcessor" test
I think a simple build.sbt might not be enough for that.
You would need to create a more sophisticated project/Build.scala like that:
import sbt._
import sbt.Keys._
object Build extends Build {
lazy val root = Project(
id = "root",
base = file("."),
aggregate = Seq(module1, module2)
)
lazy val module1 = Project(
id = "module1",
base = file("module1-folder"),
settings = Seq(
name := "Module 1",
version := "1.0",
scalaVersion := "2.11.7",
libraryDependencies += "org.scalatest" % "scalatest_2.11" % "2.2.2" % "test"
lazy val module2 = Project(
id = "module2",
base = file("module2-folder"),
dependencies = Seq(module1),
settings = Seq(
name := "Module 2",
version := "1.0",
scalaVersion := "2.11.7",
libraryDependencies += "org.scalatest" % "scalatest_2.11" % "2.2.2" % "test"
}