【问题标题】:Cannot resolve package when referencing Git library with SBT使用 SBT 引用 Git 库时无法解析包
【发布时间】:2017-06-24 10:22:09
【问题描述】:

我正在尝试使用 Git 引用自定义 ScalaCheck,我的 build.sbt 文件如下

lazy val root = (project in file("."))
  .settings(
    name := "myproject",
    version := "1.0",
    scalaVersion := "2.11.8",
    libraryDependencies ++= Seq(/*other dependencies*/)
  )
  .dependsOn(sc)

lazy val sc = RootProject(uri("https://github.com/<user>/scalacheck.git"))
version in sc := "1.14.0-SNAPSHOT"

问题是我无法导入库。我只收到一个错误,即 scalacheck 不是org 的一部分。这是我的代码

import org.scalacheck._
object Main extends App {
  println("Hello world")
}

此外,到目前为止,我只有在使用 Scala 2.12 版时才能得到,但我需要使用 2.11,因为其他一些依赖项仅支持它。使用 2.11 根本无法解决依赖问题。

我在 SBT 版本 0.13.13。

这是我的 sbt 输出,

[info] Loading project definition from C:\Users\user\myproject\project
[info] Loading project definition from C:\Users\user\.sbt\0.13\staging\ec35d052aa233b07b77a\scalacheck\project
[info] Set current project to myproject (in build file:/C:/Users/user/myproject/)
> [info] Defining root/*:shellPrompt
[info] The new value will be used by no settings or tasks.
[info] Reapplying settings...
[info] Set current project to myproject (in build file:/C:/Users/user/myproject/)
[info] Defining */*:sbtStructureOutputFile
[info] The new value will be used by no settings or tasks.
[info] Reapplying settings...
[info] Set current project to myproject (in build file:/C:/Users/user/myproject/)
[info] Defining */*:sbtStructureOptions
[info] The new value will be used by no settings or tasks.
[info] Reapplying settings...
[info] Set current project to myproject (in build file:/C:/Users/user/myproject/)
[info] Applying State transformations org.jetbrains.sbt.CreateTasks from C:/Users/user/.IntelliJIdea2016.3/config/plugins/Scala/launcher/sbt-structure-0.13.jar
[info] Reapplying settings...
[info] Set current project to myproject (in build file:/C:/Users/user/myproject/)
[info] Updating {file:/C:/Users/user/myproject/}root...
[info] Resolving org.scala-lang#scala-library;2.11.8 ...
[info] Resolving default#scalacheck_2.11;1.14.0-SNAPSHOT ...
[info] Resolving default#scalacheck_2.11;1.14.0-SNAPSHOT ...
[warn]  module not found: default#scalacheck_2.11;1.14.0-SNAPSHOT
[warn] ==== local: tried
[warn]   C:\Users\user\.ivy2\local\default\scalacheck_2.11\1.14.0-SNAPSHOT\ivys\ivy.xml
[warn] ==== public: tried
[warn]   https://repo1.maven.org/maven2/default/scalacheck_2.11/1.14.0-SNAPSHOT/scalacheck_2.11-1.14.0-SNAPSHOT.pom
[info] Resolving org.scalaj#scalaj-http_2.11;2.3.0 ...
[info] Resolving com.fasterxml.jackson.module#jackson-module-scala_2.11;2.7.2 ...
[info] Resolving org.scala-lang#scala-reflect;2.11.8 ...
[info] Resolving com.fasterxml.jackson.core#jackson-core;2.7.2 ...
[info] Resolving com.fasterxml.jackson.core#jackson-annotations;2.7.2 ...
[info] Resolving com.fasterxml.jackson.core#jackson-databind;2.7.2 ...
[info] Resolving com.fasterxml.jackson.module#jackson-module-paranamer;2.7.2 ...
[info] Resolving com.thoughtworks.paranamer#paranamer;2.8 ...
[info] Resolving ch.qos.logback#logback-classic;1.1.7 ...
[info] Resolving ch.qos.logback#logback-core;1.1.7 ...
[info] Resolving org.slf4j#slf4j-api;1.7.20 ...
[info] Resolving com.typesafe.scala-logging#scala-logging_2.11;3.5.0 ...
[info] Resolving org.slf4j#slf4j-api;1.7.21 ...
[info] Resolving com.softwaremill.quicklens#quicklens_2.11;1.4.8 ...
[info] Resolving org.scalacheck#scalacheck_2.11;1.13.4 ...
[info] Resolving org.scala-sbt#test-interface;1.0 ...
[info] Resolving org.scala-lang#scala-compiler;2.11.8 ...
[info] Resolving org.scala-lang.modules#scala-xml_2.11;1.0.4 ...
[info] Resolving org.scala-lang.modules#scala-parser-combinators_2.11;1.0.4 ...
[info] Resolving jline#jline;2.12.1 ...
[warn]  ::::::::::::::::::::::::::::::::::::::::::::::
[warn]  ::          UNRESOLVED DEPENDENCIES         ::
[warn]  ::::::::::::::::::::::::::::::::::::::::::::::
[warn]  :: default#scalacheck_2.11;1.14.0-SNAPSHOT: not found
[warn]  ::::::::::::::::::::::::::::::::::::::::::::::
[warn] 
[warn]  Note: Unresolved dependencies path:
[warn]      default:scalacheck_2.11:1.14.0-SNAPSHOT
[warn]        +- myproject:myproject_2.11:1.0
[trace] Stack trace suppressed: run 'last root/*:update' for the full output.
[trace] Stack trace suppressed: run 'last root/*:ssExtractDependencies' for the full output.
[error] (root/*:update) sbt.ResolveException: unresolved dependency: default#scalacheck_2.11;1.14.0-SNAPSHOT: not found
[error] (root/*:ssExtractDependencies) sbt.ResolveException: unresolved dependency: default#scalacheck_2.11;1.14.0-SNAPSHOT: not found
[error] Total time: 3 s, completed 2017-feb-10 09:14:53

.sbt/0.13/staging/ec35d052aa233b07b77a/scalacheck 包含完整的 repo 和我认为是目标目录中的构建,但大多数是指 Scala 版本 2.12 的构建。

后续编辑:我认为这个 SBT 的错误报告涉及https://github.com/sbt/sbt/issues/2901

【问题讨论】:

    标签: scala sbt scalacheck


    【解决方案1】:

    假设您的&lt;user&gt;/scalacheck.git 是原始rickynils/scalacheck.git 的分支,问题是scalacheck 的当前主分支 是targetted to scala 2.12.0

    由于您的 scalaVersion 是 2.11.8,因此不支持当前版本的 scalacheck。

    因此,将您的build.sbtscalaVersion 更改为2.12.0 即可解决问题。

    另外,您可以使用sbt projects 来检查 scalacheck 是否包含三个项目:

    $ sbt projects
      js
      jvm
    * scalacheck
    

    jsscalajs,所以你需要的是jvm

    因此您必须使用ProjectRef 而不是RootProject 来指定项目。

    lazy val root = (project in file("."))
      .settings(
        name := "myproject",
        version := "1.0",
        scalaVersion := "2.12.0",
        libraryDependencies ++= Seq(/*other dependencies*/)
      )
      .dependsOn(sc)
    
    lazy val sc = ProjectRef(uri("https://github.com/<user>/scalacheck.git"),"jvm")
    version in sc := "1.14.0-SNAPSHOT"
    

    同样将https://github.com/&lt;user&gt;/scalacheck.git 中的build.sbtscalaVersion 更改为2.11.8 可能会起作用,但如果它使用2.12.0 的新功能将无法编译。

    如果您想使用 scala 2.11.8,可以使用 #GIT_TAG 语法指定旧版本。

    ProjectRef(uri("https://github.com/rickynils/scalacheck.git#1.13.4"),"jvm")
    

    【讨论】:

    • 首先,ScalaCheck build.sbt 中的crossScalaVersions 设置不能解决这个问题吗?其次,虽然 sbt 确实找到了 scalacheck,但我仍然在 Main 中收到导入错误 scalacheck is not part of org。另外,我可以使用 sbt 指定较旧的 ScalaCheck 版本,而不是使用 2.12(因为我的依赖项之一需要 2.11)?
    • @davl 我更新了答案,如何指定较旧的 scalacheck 并修复导入错误。是的,正如你所说,crossScalaVersions 在 scalacheck 的build.sbt 中指定,因此它应该在2.11.8 中运行,但也许crossScalaVersions 不打算用于RootProjectProjectRef,并且仅用于部署。我不知道真相。
    猜你喜欢
    • 2014-07-08
    • 2020-04-24
    • 2016-12-27
    • 2011-03-01
    • 1970-01-01
    • 2019-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多