尝试将项目交叉编译到 Scala 2.11 失败,并显示“加载对象时出错,缺少依赖项‘编译器镜像中的对象 scala’”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23317326/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 06:13:39  来源:igfitidea点击:

Trying to cross compile a project to Scala 2.11 fails with "error while loading Object, Missing dependency 'object scala in compiler mirror'"

scalasbtcross-compilingscala-2.11

提问by Maurício Linhares

I'm trying to compile my project to Scala 2.10 and 2.11 so that I can release versions for both scala versions, but every time I try to do it it fails with the following message:

我正在尝试将我的项目编译为 Scala 2.10 和 2.11,以便我可以发布两个 Scala 版本的版本,但每次尝试这样做时都会失败并显示以下消息:

#( 04/26/14@ 7:40 )( mauricio@Mauricios-MacBook-Pro ):~/projects/scala/[email protected]?
   sbt compile
[info] Loading project definition from /Users/mauricio/projects/scala/postgresql-netty/project
[info] Set current project to db-async-base (in build file:/Users/mauricio/projects/scala/postgresql-netty/)
[info] Compiling 61 Scala sources and 1 Java source to /Users/mauricio/projects/scala/postgresql-netty/db-async-common/target/scala-2.11/classes...
[error] error while loading Object, Missing dependency 'object scala in compiler mirror', required by /Library/Java/JavaVirtualMachines/jdk1.7.0_45.jdk/Contents/Home/jre/lib/rt.jar(java/lang/Object.class)
scala.reflect.internal.MissingRequirementError: object scala in compiler mirror not found.
    at scala.reflect.internal.MissingRequirementError$.signal(MissingRequirementError.scala:17)
    at scala.reflect.internal.MissingRequirementError$.notFound(MissingRequirementError.scala:18)
    at scala.reflect.internal.Mirrors$RootsBase.getModuleOrClass(Mirrors.scala:53)
    at scala.reflect.internal.Mirrors$RootsBase.getModuleOrClass(Mirrors.scala:66)
    at scala.reflect.internal.Mirrors$RootsBase.getPackage(Mirrors.scala:173)
    at scala.reflect.internal.Definitions$DefinitionsClass.ScalaPackage$lzycompute(Definitions.scala:161)
    at scala.reflect.internal.Definitions$DefinitionsClass.ScalaPackage(Definitions.scala:161)
    at scala.reflect.internal.Definitions$DefinitionsClass.ScalaPackageClass$lzycompute(Definitions.scala:162)
    at scala.reflect.internal.Definitions$DefinitionsClass.ScalaPackageClass(Definitions.scala:162)
    at scala.reflect.internal.Definitions$DefinitionsClass.init(Definitions.scala:1375)
    at scala.tools.nsc.Global$Run.<init>(Global.scala:1278)
    at xsbt.CachedCompiler0$$anon.<init>(CompilerInterface.scala:113)
    at xsbt.CachedCompiler0.run(CompilerInterface.scala:113)
    at xsbt.CachedCompiler0.run(CompilerInterface.scala:99)
    at xsbt.CompilerInterface.run(CompilerInterface.scala:27)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at sbt.compiler.AnalyzingCompiler.call(AnalyzingCompiler.scala:102)
    at sbt.compiler.AnalyzingCompiler.compile(AnalyzingCompiler.scala:48)
    at sbt.compiler.AnalyzingCompiler.compile(AnalyzingCompiler.scala:41)
    at sbt.compiler.AggressiveCompile$$anonfun$$anonfun$compileScala.apply$mcV$sp(AggressiveCompile.scala:99)
    at sbt.compiler.AggressiveCompile$$anonfun$$anonfun$compileScala.apply(AggressiveCompile.scala:99)
    at sbt.compiler.AggressiveCompile$$anonfun$$anonfun$compileScala.apply(AggressiveCompile.scala:99)
    at sbt.compiler.AggressiveCompile.sbt$compiler$AggressiveCompile$$timed(AggressiveCompile.scala:166)
    at sbt.compiler.AggressiveCompile$$anonfun.compileScala(AggressiveCompile.scala:98)
    at sbt.compiler.AggressiveCompile$$anonfun.apply(AggressiveCompile.scala:143)
    at sbt.compiler.AggressiveCompile$$anonfun.apply(AggressiveCompile.scala:87)
    at sbt.inc.IncrementalCompile$$anonfun$doCompile.apply(Compile.scala:39)
    at sbt.inc.IncrementalCompile$$anonfun$doCompile.apply(Compile.scala:37)
    at sbt.inc.IncrementalCommon.cycle(Incremental.scala:99)
    at sbt.inc.Incremental$$anonfun.apply(Incremental.scala:38)
    at sbt.inc.Incremental$$anonfun.apply(Incremental.scala:37)
    at sbt.inc.Incremental$.manageClassfiles(Incremental.scala:65)
    at sbt.inc.Incremental$.compile(Incremental.scala:37)
    at sbt.inc.IncrementalCompile$.apply(Compile.scala:27)
    at sbt.compiler.AggressiveCompile.compile2(AggressiveCompile.scala:157)
    at sbt.compiler.AggressiveCompile.compile1(AggressiveCompile.scala:71)
    at sbt.compiler.AggressiveCompile.apply(AggressiveCompile.scala:46)
    at sbt.Compiler$.apply(Compiler.scala:75)
    at sbt.Compiler$.apply(Compiler.scala:66)
    at sbt.Defaults$.sbt$Defaults$$compileTaskImpl(Defaults.scala:743)
    at sbt.Defaults$$anonfun$compileTask.apply(Defaults.scala:735)
    at sbt.Defaults$$anonfun$compileTask.apply(Defaults.scala:735)
    at scala.Function1$$anonfun$compose.apply(Function1.scala:47)
    at sbt.$tilde$greater$$anonfun$$u2219.apply(TypeFunctions.scala:42)
    at sbt.std.Transform$$anon.work(System.scala:64)
    at sbt.Execute$$anonfun$submit$$anonfun$apply.apply(Execute.scala:237)
    at sbt.Execute$$anonfun$submit$$anonfun$apply.apply(Execute.scala:237)
    at sbt.ErrorHandling$.wideConvert(ErrorHandling.scala:18)
    at sbt.Execute.work(Execute.scala:244)
    at sbt.Execute$$anonfun$submit.apply(Execute.scala:237)
    at sbt.Execute$$anonfun$submit.apply(Execute.scala:237)
    at sbt.ConcurrentRestrictions$$anon$$anonfun.apply(ConcurrentRestrictions.scala:160)
    at sbt.CompletionService$$anon.call(CompletionService.scala:30)
    at java.util.concurrent.FutureTask.run(FutureTask.java:262)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
    at java.util.concurrent.FutureTask.run(FutureTask.java:262)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:744)
[error] (db-async-common/compile:compile) scala.reflect.internal.MissingRequirementError: object scala in compiler mirror not found.
[error] Total time: 2 s, completed Apr 26, 2014 7:43:49 PM

My build file looks like this:

我的构建文件如下所示:

import sbt._
import Keys._

object ProjectBuild extends Build {

  val commonName = "db-async-common"
  val postgresqlName = "postgresql-async"
  val mysqlName = "mysql-async"

  lazy val root = Project(
    id = "db-async-base",
    base = file("."),
    settings = Configuration.baseSettings,
    aggregate = Seq(common, postgresql, mysql)
  )

  lazy val common = Project(
    id = commonName,
    base = file(commonName),
    settings = Configuration.baseSettings ++ Seq(
      name := commonName,
      libraryDependencies := Configuration.commonDependencies
    )
  )

  lazy val postgresql = Project(
    id = postgresqlName,
    base = file(postgresqlName),
    settings = Configuration.baseSettings ++ Seq(
      name := postgresqlName,
      libraryDependencies ++= Configuration.implementationDependencies
    )
  ) aggregate (common) dependsOn (common)

  lazy val mysql = Project(
    id = mysqlName,
    base = file(mysqlName),
    settings = Configuration.baseSettings ++ Seq(
      name := mysqlName,
      libraryDependencies ++= Configuration.implementationDependencies
    )
  ) aggregate (common) dependsOn (common)

}

object Configuration {

  val commonVersion = "0.2.13"
  val projectScalaVersion = "2.11.0"

  val specs2Dependency = "org.specs2" %% "specs2" % "2.3.11" % "test"
  val logbackDependency = "ch.qos.logback" % "logback-classic" % "1.0.13" % "test"

  val commonDependencies = Seq(
    "org.slf4j" % "slf4j-api" % "1.7.5",
    "joda-time" % "joda-time" % "2.3",
    "org.joda" % "joda-convert" % "1.5",
    "io.netty" % "netty-all" % "4.0.18.Final",
    "org.javassist" % "javassist" % "3.18.1-GA",
    specs2Dependency,
    logbackDependency
  )

  val implementationDependencies = Seq(
    specs2Dependency,
    logbackDependency
  )

  val baseSettings = Defaults.defaultSettings ++ Seq(
    scalacOptions :=
      Opts.compile.encoding("UTF8")
        :+ Opts.compile.deprecation
        :+ Opts.compile.unchecked
        :+ "-feature"
    ,
    scalacOptions in doc := Seq("-doc-external-doc:scala=http://www.scala-lang.org/archives/downloads/distrib/files/nightly/docs/library/"),
    scalaVersion := projectScalaVersion,
    crossScalaVersions := Seq(projectScalaVersion, "2.10.4"),
    javacOptions := Seq("-source", "1.6", "-target", "1.6", "-encoding", "UTF8"),
    organization := "com.github.mauricio",
    version := commonVersion,
    parallelExecution := false,
    publishArtifact in Test := false,
    publishMavenStyle := true,
    pomIncludeRepository := {
      _ => false
    },
    publishTo <<= version {
      v: String =>
        val nexus = "https://oss.sonatype.org/"
        if (v.trim.endsWith("SNAPSHOT"))
          Some("snapshots" at nexus + "content/repositories/snapshots")
        else
          Some("releases" at nexus + "service/local/staging/deploy/maven2")
    },
    pomExtra := (
      <url>https://github.com/mauricio/postgresql-async</url>
        <licenses>
          <license>
            <name>APACHE-2.0</name>
            <url>http://www.apache.org/licenses/LICENSE-2.0</url>
            <distribution>repo</distribution>
          </license>
        </licenses>
        <scm>
          <url>[email protected]:mauricio/postgresql-netty.git</url>
          <connection>scm:git:[email protected]:mauricio/postgresql-netty.git</connection>
        </scm>
        <developers>
          <developer>
            <id>mauricio</id>
            <name>Maurício Linhares</name>
            <url>https://github.com/mauricio</url>
          </developer>
        </developers>
      )
  )

}

And if you'd like to pull the source to check it's at this branch on Github.

如果你想拉取源代码来检查它在 Github 上的这个分支

Any info on trying to fix this would be very helpful.

任何有关尝试解决此问题的信息都会非常有帮助。

采纳答案by lpiepiora

The problem is very simple, in your Build.scala, you did almost all right, but in commons project you had overridden default commonDependencies. By default it contains dependency to scala-library.

问题很简单,在你的Build.scala, 你做的几乎没问题,但在 commons 项目中你已经覆盖了 default commonDependencies。默认情况下,它包含对 scala-library 的依赖。

This piece of code

这段代码

lazy val common = Project(
    id = commonName,
    base = file(commonName),
    settings = Configuration.baseSettings ++ Seq(
      name := commonName,
      libraryDependencies := Configuration.commonDependencies // #1
    )
  )

at the element marked as #1should have

在标记为#1应有的元素处

libraryDependencies ++= Configuration.commonDependencies

Ps. I've sent you a pull request with the fix.

附言。我已经向您发送了一个包含修复程序的拉取请求。

回答by Hyman Davidson

I was getting this error when trying to use the Oracle Java 9 jvm on Ubuntu with sbt. Using the Java 8 jvm worked for me, though.

尝试在带有 sbt 的 Ubuntu 上使用 Oracle Java 9 jvm 时出现此错误。不过,使用 Java 8 jvm 对我有用。

回答by Andrzej Jozwik

I think you answered before your questions Multimodule project build with SBT breaks with weird error

我想你在你的问题之前回答了使用 SBT 构建的多模块项目因奇怪的错误而中断

In this build add a line "org.scala-lang" % "scala-library" % projectScalaVersion,:

在这个构建中添加一行"org.scala-lang" % "scala-library" % projectScalaVersion,

 val commonDependencies = Seq(
    ..
    "org.joda" % "joda-convert" % "1.5",
    "org.scala-lang" % "scala-library" % projectScalaVersion,
    "io.netty" % "netty-all" % "4.0.18.Final",
    ...
  )

回答by Filippo Vitale

In Build.scalaI would add autoScalaLibrary := true:

Build.scala我会补充autoScalaLibrary := true

lazy val common = Project(
    id = commonName,
    base = file(commonName),
    settings = Configuration.baseSettings ++ Seq(
      ...
      autoScalaLibrary := true,
      ...
    )
  )

回答by smac89

This problem was solved for me when using intellij, by closing the project and reimporting it, and selecting sbt as the build system in settings.

我在使用intellij时解决了这个问题,关闭项目并重新导入它,并在设置中选择sbt作为构建系统。

Why oh why did I choose SBT?? Now I'm stuck with a very brittle build system. Thank God that atleast intellij does it right.

为什么哦为什么我选择了SBT??现在我被一个非常脆弱的构建系统困住了。感谢上帝,至少 Intellij 做得对。

sbt version: 0.13.15

scala version: 2.12.1

sbt 版本:0.13.15

Scala 版本:2.12.1