In ScalaDoc I want to have a link to an annotation from a library: discriminator.
My ScalaDoc:
/** Trait must be marked with [[json.schema.discriminator]] annotation. */
But ScalaDoc generation fails with the following error:
Could not find any member to link for "json.schema.discriminator".
UPD:
It appeared that errors were because of -Xfatal-warnings scalac option.
Once it got clear I found sbt-api-mappings SBT plugin which resolves all external references with javadoc.io.
You probably don't find it because you don't import the correct package.
Please note that the json.schema.discriminator class is part of the "scala-jsonschema-core" package.
Therefore you need to add to your build.sbt:
name := "StackOverflow"
version := "0.1"
scalaVersion := "2.13.6"
libraryDependencies += "com.github.andyglow" %% "scala-jsonschema-core" % "0.7.6"
libraryDependencies += "com.github.andyglow" %% "scala-jsonschema-core" % "0.7.6" classifier "javadoc"
Then you can use it the same as you tried:
/** Trait must be marked with [[json.schema.discriminator]] annotation. */
Then sbt doc works:
Related
Is it possible to add a Scala compiler plugin only when compiling test sources?
When a compiler plugin is added by calling SBT's addCompilerPlugin then a library dependency is added. The relevant methods are:
/** Transforms `dependency` to be in the auto-compiler plugin configuration. */
def compilerPlugin(dependency: ModuleID): ModuleID =
dependency.withConfigurations(Some("plugin->default(compile)"))
/** Adds `dependency` to `libraryDependencies` in the auto-compiler plugin configuration. */
def addCompilerPlugin(dependency: ModuleID): Setting[Seq[ModuleID]] =
libraryDependencies += compilerPlugin(dependency)
The question boils down if there is a withConfigurations that causes the plugin to be on the classpath only when compiling test sources. I tried Some("plugin->default(testCompile)") without success.
To answer my own question: It is possible by setting autoCompilerPlugins to false and adding the necessary -Xplugin=... Scalac option manually in the test configuration. This can be done by using the Classpaths.autoPlugins utility method.
In my case I wanted to activate the SemanticDB compiler plugin during tests only. This can be accomplished by the following SBT settings:
autoCompilerPlugins := false,
ivyConfigurations += Configurations.CompilerPlugin,
scalacOptions in Test ++= Classpaths.autoPlugins(update.value, Seq(), ScalaInstance.isDotty(scalaVersion.value)),
scalacOptions in Test += "-Yrangepos",
scalacOptions in Test += "-P:semanticdb:text:on",
i'm trying to import a dependency in my build.sbt file from here
https://github.com/dmarcous/spark-betweenness.
When i hover on the error it says:
Expression type ModuleID must confirm to Def.SettingsDefinition in SBT file
Unresolved Dependency
I am new in scala so my question may be silly.Thanks in advance
It is still unclear how your build configuration looks like, but the following build.sbt works (in the sense that it compiles and does not show the error that you mentioned):
name := "test-sbt"
organization := "whatever"
version := "1.0.0"
scalaVersion := "2.10.7"
libraryDependencies += "com.centrality" %% "spark-betweenness" % "1.0.0"
Alternatively, if you have a multi-project build, it could look like this:
lazy val root = project
.settings(
name := "test-sbt",
organization := "whatever",
version := "1.0.0",
scalaVersion := "2.10.7",
libraryDependencies += "com.centrality" %% "spark-betweenness" % "1.0.0"
)
However, you're probably going to find that it still does not work because it cannot resolve this dependency. Indeed, this library does not seem to be available neither in Maven Central nor in jcenter. It is also very old - it appears to only be published for Scala 2.10 and a very old Spark version (1.5), so most likely you won't be able to use it with recent Spark environments (2.x and Scala 2.11).
I am trying to learn the basics of Scala, scalatest, and sbt and I'm following a tutorial. This is my built.sbt file:
name := "demo-hello"
version := "0.1"
scalaVersion := "2.12.6"
libraryDependencies += "org.scalatest" % "scalatest_2.10" % "2.1.0" % "test"
I have a test that looks like this (showing this is probably unnecessary:
package demo
import org.scalatest.FunSuite
class HelloTest extends FunSuite {
test("say hello method works correctly") {
val hello = new Hello
assert(hello.sayHello("Scala") == "Hello, Scala!")
}
}
What should I do from here? I am trying to run the test but I get this error:
Error:(8, 36) can't expand macros compiled by previous versions of Scala
assert(hello.sayHello("Scala") == "Hello, Scala!")
I'm not that familiar with the % symbol btw.
FIX
I changed my build.sbt to this:
name := "demo-hello"
version := "0.1"
scalaVersion := "2.10"
libraryDependencies += "org.scalatest" % "scalatest_2.10" % "2.1.0" % "test"
Remaining questions:
So it seems downgrading to scalaVersion "2.10" worked. Why?
What is an artifact? scalatest is apparently an artifact?
Where is scalaversion 2.10 kept on my machine? It seems I only have scala 2.12. Where in my project folder is version 2.10?
Answering your questions slightly out-of-order:
2 - An "artifact" is something that's built by maven, sbt, or another build system. For Scala or Java, this is almost always a jar file. Each item in libraryDependencies specifies a file in a maven repository (a database of artifacts).
1 - Scala class files are not compatible between minor versions of Scala. When you download a Scala jar from a maven repository, the Scala version is specified as part of the artifact name. The _2.10 in your dependency declares that you wish to use the version of scalatest that's compile for Scala 2.10 - which is why you were getting an error using it in your Scala 2.12 application.
When declaring dependencies on Scala artifacts in sbt, you should always use the %% operator, which automatically appends the appropriate suffix to your artifact, like so:
// This works for any scalaVersion setting.
libraryDependencies += "org.scalatest" %% "scalatest" % "2.1.0" % "test"
3 - sbt handles downloading the appropriate runtime files for the declared version of Scala automatically.
My sbt project with scala-test is layout as standard:
sbt-example
build.sbt
src/main/scala/local/search/BinarySearch.scala
src/main/scala/local/util/Utility.scala
src/test/scala/local/search/SearchingSuite.scala
My build.sbt is very simple:
name := "helloworld"
version := "1.0"
scalaVersion := "2.11.7"
libraryDependencies ++= Seq(
"org.scalatest" %% "scalatest" % "2.2.4"
)
My BinarySearch.scala looks like:
package local.search
object BinarySearch {
// def ...
}
My SearchingSuite.scala looks like:
package local.search
import org.scalatest.FunSuite
class SearchingSuite extends FunSuite{
// call functions from BinarySearch object
}
Run commands are:
sbt compile
sbt "test-only local.search.SearchingSuite"
Then sbt raises error:
not found: value BinarySearch
What's wrong with my code? And if I need to use some object from Utility.scala, how to import in SearchingSuite.scala?
Thanks, will vote up with any answers!
Let's say you are working on class A; and class A requires class B. As long as A and B belong to two different packages, you have to import class B.
In your case, SearchingSuite and BinarySearch belong to two different packages. In particular, the former belongs to the main source package, the latter belong to the test source package. Therefore, you must import BinarySearch in SearchingSuite. Otherwise, there will be an compilation error like what you have encountered.
To use some object from Utility, you
import local.util.Utility
if Utility is a class, or
import local.util.Utility._
if Utility is an object.
If you are using some IDE, like Intellij or Eclipse, right click on the error highlight, the IDE will pop up some suggestions.
I'm finding it diffuclt to build a Play project using the sbt native packager. I don't know where to set the RPM configuration when I am given the following error:
[error] `rpmVendor in Rpm` is empty. Please provide a valid vendor for the rpm SPEC.
[error] `packageSummary in Rpm` is empty. Please provide a valid summary for the rpm SPEC.
[error] `packageDescription in Rpm` is empty. Please provide a valid description for the rpm SPEC.
I've set the following in project/plugins.sbt:
addSbtPlugin("com.typesafe.sbt" % "sbt-native-packager" % "0.8.0")
In my build.sbt:
name := """supersecretproject"""
version := "1.0-SNAPSHOT"
lazy val root = (project in file(".")).enablePlugins(PlayScala)
scalaVersion := "2.11.1"
libraryDependencies ++= Seq(
jdbc,
anorm,
cache,
ws
)
libraryDependencies += "mysql" % "mysql-connector-java" % "5.1.27"
javacOptions ++= Seq("-source", "1.6", "-target", "1.6")
tomcat()
The documentation merely states:
A rpm package needs some mandatory settings to be valid. Make sure you have these settings in your build:
rpmRelease := "1"
rpmVendor := "typesafe"
rpmUrl := Some("http://github.com/paulp/sbt-extras")
rpmLicense := Some("BSD")
Which is almost entirely useless if you don't know SBT very well! How do I "have these settings in your build:" as the documentation instructs?
I've tried adding the above "settings" to build.sbt or a separate packageSettings.sbt but with no luck as I just get the following error:
error: not found: value rpmRelease
rpmRelease := "1"
^
[error] Type error in expression
Note: I run the sbt using sbt rpm:packageBin
It sounds like the developers of that plugin are trying to not be too prescriptive, but in doing so have not given you enough information to even get started! :-(
The simplest possible solution: Copy those four settings (including the blank lines between) into your build.sbt.
A logical place is probably towards the bottom of the file, as "packaging" your app is something that happens "towards the end" of the development cycle.
Another option: SBT automatically combines the contents of all .sbt files it finds in the project root. So if you prefer, you could create a new file such as packagingSettings.sbt and put those settings in there.
Edit: Help with imports:
Whichever option you choose, you'll need to add the following imports at the top of the file (as per the getting started guide):
import com.typesafe.sbt.SbtNativePackager._
import NativePackagerKeys._