when specify the scalaVersion and sbtVersion to resolve the plugin dependencies via “extra”,it doesn't work - plugins

I'm trying to add a plugin like this :
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.11.2" extra ("scalaVersion" -> "2.10", "sbtVersion" -> "0.13"))
But when I start sbt session ,the search path still be
https://repo.scala-sbt.org/scalasbt/sbt-plugin-releases/com.eed3si9n/sbt-assembly/scala_2.12/sbt_1.0//0.11.2/ivys/ivy.xml
the scalaVersion and sbtVersion still is the one which I'm using.
Could you tell me What should I do?
Thank you!

That is what addSbtPlugin does: it adds the appropriate scalaVersion and sbtVersion to the mentioned artifact (in your case overriding what you have specified manually).
If you want to have full control, do not use addSbtPlugin:
libraryDependencies += "com.eed3si9n" % "sbt-assembly" % "0.11.2" extra ("scalaVersion" -> "2.10", "sbtVersion" -> "0.13")
BUT beware, usually this sort of thing will not work if you fail to add the right attributes appropriate for your SBT version at hand.
In your case, you are apparently using SBT 1.x, which in turn uses Scala 2.12. Trying to use an artifact build with Scala 2.10, ie. for SBT 0.13.x, will break.

Related

Error on scalatest compilation in IDEA

I am trying to compile Scala project which contains scalatest.
It compiles normal on sbt
sbt
> compile
> test:compile
, but when I am trying to build it with IDEA, it shows the following error:
Error:(37, 11) exception during macro expansion:
java.lang.NoSuchMethodError: org.scalactic.BooleanMacro.genMacro(Lscala/reflect/api/Exprs$Expr;Ljava/lang/String;Lscala/reflect/api/Exprs$Expr;)Lscala/reflect/api/Exprs$Expr;
at org.scalatest.AssertionsMacro$.assert(AssertionsMacro.scala:34)
assert((ElementMeasures.baseElementDistance(mEl1, mEl2) - 0.33333).abs < 0.001)
^
for each assert function in test.
build.sbt file contains following:
name := "ner-scala"
organization := "ml.generall"
version := "1.0-SNAPSHOT"
scalaVersion := "2.11.8"
libraryDependencies += "org.scalactic" %% "scalactic" % "3.0.0"
libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.0" % "test"
...
It may also mean that you have more than one versions of scalatest registered. I came into pretty same issue with compile-time error on assert
I just ran into the same problem and as Alexey described (he should get the upvote but I don't have enough reputation to upvote or comment - thank you Alexey), it seems that it was caused by having multiple versions of scalatest in my project. I was able to fix it by specifically excluding the older scalatest from the library that brought it in (and please note that the exclude needs to specify the scala binary version, e.g. _2.11 etc.!):
...exclude("org.scalatest", "scalatest_2.11")
There had also been a warning in the event log before the exclude:
SBT project import
[warn] Multiple dependencies with the same organization/name but different versions.
[warn] * org.scalatest:scalatest_2.11:(2.2.6, 3.0.1)
I think your IntelliJ is missing library scalatest
from IntelliJ, go to Project Structure -> Project Settings -> Libraries -> + symbol -> From Maven -> search for scalatest with correct version
after adding scalatest library for IntelliJ, assert error should disappear.
This is not a guaranteed solution, just give it a try :)
Sometimes, there are incompatibilities between how Intellij IDEA compiles scala, versus how sbt compiles scala. What you can try doing, is ask the IDEA to compile as sbt, and not as it does by default.
In the sbt view, you need to open the sbt settings.
Once it is opened you can enable the "builds" checkbox. As stated by IDEA, it is recomended for sbt build configurations that cause compilation nin IDEA to not work correctly.
Excluding just the org.scalatest group did not work for me. On analyzing my mvn dependencies I had to exclude the scalactic_{scala_binary_version} group as well.
<exclusion>
<artifactId>scalactic_2.11</artifactId>
<groupId>org.scalactic</groupId>
</exclusion>
I had a similar issue, but excluding org.scalatest did not fix it.
Instead, I excluded org.scalactic:
"com.stackoverflow" %% "troublesome-library" % "1.0.420" excludeAll(ExclusionRule(organization="org.scalactic"))

How to determine what dependency is forcing the scala version to be 2.10 when scalaVersion is set to 2.11.2

I'm trying to build the lunatech-securesocial-poc project with scala 2.11.2 and I've updated the scalaVersion in projects/Build.scala. This project depends on securesocial, which I've built locally with 2.11.2 and named its artifact version master-SNAPSHOT. I've updated the dependency in lunatech-securesocial-poc's project to use this version of securesocial. However, SBT (activator) fails to compile because it is looking for ws.securesocial#securesocial_2.10;master-SNAPSHOT. How can I find out what is causing the scala version to be overridden to 2.10. I want 2.11.2. Obviously some dependency is forcing it, but I want to find out what that dependency is, and fix it.
I've tried adding:
dependencyOverrides += "org.scala-lang" % "scala-library" % scalaVersion.value
evictionWarningOptions := EvictionWarningOptions.default.withWarnTransitiveEvictions(true).withWarnDirectEvictions(true).withWarnScalaVersionEviction(true)
ivyScala := ivyScala.value map { _.copy(overrideScalaVersion = true) }
to my Build.scala and I'm using SBT version 0.13.7. I don't see why it is so hard for SBT to just abort and tell me that XXX dependency is preventing it from using 2.11.2 and requiring 2.10.
Anyone?
You can use sbt-dependency-graph plugin to find this out.
The issue turned out to be in my Build.scala. While I defined scalaVersion and used it in setting up dependencies, I didn't pass a setting to the Project that overrode the default scalaVersion, which evidently is the one used to build activator (2.10.4). So despite thinking I had set scalaVersion, I hadn't really.

Why does SBT 0.12.2 resolve plugins with Scala 2.9.2 and ignore scalaVersion in build.sbt?

SBT 0.12.2 always attempts to resolve plugins using Scala 2.9.2 when using the %% syntax on plugin imports.
I have tried setting older versions of Scala in build.sbt, newer versions, etc. Even deleting target folder each time... nothing seems to make a difference.
name := "Game"
version := "1.0"
scalaVersion := "2.9.1" // SBT is ignoring the scala version
SBT is recursive, so you need to specify scala version for project, that build your project. Another words, you need to add appropriate scalaVersion to the plugins.sbt file.
For all plugins in your project, you set scalaVersion in project/plugins.sbt file that configures the build project definition for your project and where you define plugins.
$ cat project/plugins.sbt
scalaVersion := "2.9.3"
There's however a way to set up a more specific version of sbt and Scala for a plugin.
Instead of using addSbtPlugin that accepts a single ModuleID (constructed with % and %%), use addSbtPlugin(dependency: ModuleID, sbtVersion: String) or even addSbtPlugin(dependency: ModuleID, sbtVersion: String, scalaVersion: String), e.g.
$ cat project/plugins.sbt
// It doesn't exist and it's only for demo purposes
addSbtPlugin("com.timushev.sbt" % "sbt-updates" % "0.1.0", "0.12.2", "2.5")

How does one get sbt-idea to work in scala-2.10 project?

I had a lot of trouble getting sbt-idea to work in my Scala 2.10 project.
I tried compiling sbt-idea from its git repo, making sure that to have set
scalaVersion := "2.10.0-RC5"
in build/Build.scala, and using publish-local command to compile it in git. But I nevertheless keep getting
[error] sbt.IncompatiblePluginsException: Binary incompatibility in plugins detected.
when I then use that in my published version, say by simply adding
addSbtPlugin("com.github.mpeltonen" % "sbt-idea" % "1.3.0-SNAPSHOT")
to the project/plugins.sbt file.
Don't think you need to build SBT for Scala 2.10. I keep my gen-idea and eclipse project generators in the global build.sbt file and it works for all my projects (or so it seems ;-)
I'm using Ubuntu, so where the SBT config files are saved on your computer may be different.
Create a folder called plugins under the hidden sbt directory. On Linux this is located at ~/.sbt (where tilde is an alias for your home directory). So now you should have ~/.sbt/plugins
Then create a file called build.sbt under this directory and add the following to it:
resolvers += "Sonatype snapshots" at "http://oss.sonatype.org/content/repositories/snapshots/"
resolvers += "Sonatype releases" at "https://oss.sonatype.org/content/repositories/releases/"
addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "2.1.0")
addSbtPlugin("com.github.mpeltonen" % "sbt-idea" % "1.2.0-SNAPSHOT")
To test, I just generated a scala 2.10 project with it, and it seems fine.
Oh, the file above also adds support for the eclipse command in SBT if you want to generate Scala-IDE projects.
I was able to use an older version of gen-idea by adding the following to project/plugins.sbt in the project itself:
import sbt._
import Defaults._
libraryDependencies += sbtPluginExtra(
m = "com.github.mpeltonen" % "sbt-idea" % "1.2.0", // Plugin module name and version
sbtV = "0.12", // SBT version
scalaV = "2.9.2" // Scala version compiled the plugin
)

How to change SBT's rules on generating URLs for Maven repositories?

By default, the Scala Built Tool (SBT) has a set of rules on how to generate URLs when looking up dependencies. For example, if I have the following build file,
// Project settings
name := "MyProject"
version := "0.1"
organization := "com.me"
scalaVersion := "2.8.1"
// Dependencies
libraryDependencies ++= Seq(
"com.google.guava" %% "guava" % "r09"
)
// Repositories
resolvers += "Maven Central Server" at "http://repo1.maven.org/maven2"
Then SBT attempts to find guava at the following URL,
http://repo1.maven.org/maven2/com/google/guava/guava_2.8.1/r09/guava_2.8.1-r09.pom
However, the library I'm looking for in this case isn't even made for Scala, so combining the Scala version just doesn't make sense here. How can I tell SBT what the format is for generating URLs for use with Maven repositories?
EDIT
While it seems that it is possible to edit the layout like so,
Resolver.url("Primary Maven Repository",
new URL("http://repo1.maven.org/maven2/"))( Patterns("[organization]/[module]/[module]-[revision].[ext]") )
the "[module]" keyword is predefined to be the (artifact id)_(scala version) and the "[artifact]" keyword is just "ivy", leaving me back at square one.
As far as I remember "%%" appends the scala version and "%" does not. Try
libraryDependencies ++= Seq(
"com.google.guava" % "guava" % "r09"
)
Check last one paragraph (Custom Layout) of official sbt wiki here.
Basically SBT allows you to use this syntax:
resolvers += Resolver.url("my-test-repo", url)( Patterns("[organisation]/[module]/[revision]/[artifact].[ext]") )