【问题标题】:How do dependencies work in Scala?依赖项在 Scala 中是如何工作的?
【发布时间】:2017-03-03 09:39:18
【问题描述】:

对于我现有的项目,我正在从 scalaVersion 2.10.5 迁移到 2.11.7 和 sbtVersion 1.13.9。

当前迁移的项目是Sales,它有Sales-common、Sales-read、Sales-write模块。它有一个依赖项目Core。但我没有核心项目代码,我只发布了 artefact jars(我有 myapp-core-read_2.10-2.2.33.jar,2.10 是 Scala 版本)。

我的项目是这样的

销售

common

    build.sbt

read

    build.sbt

write

    build.sbt

build.sbt

到目前为止,使用 scala 2.10.5 一切都可以在实时使用时正常运行。

更改版本后,我运行sbt cleansbt update。我遇到了这样的未解决的依赖问题:

[warn]  ::::::::::::::::::::::::::::::::::::::::::::::
[warn]  ::          UNRESOLVED DEPENDENCIES         ::
[warn]  ::::::::::::::::::::::::::::::::::::::::::::::
[warn]  :: mycompany#myapp-core-read_2.11;2.2.33: not found
[warn]  :: mycomapny#myapp-core-write_2.11;2.2.33: not found
[warn]  ::::::::::::::::::::::::::::::::::::::::::::::

我的Sales buld.sbt是这样的:

name := "myapp-sales"

organization in ThisBuild := "com.mycompany"

scalaVersion in ThisBuild  := "2.11.7"

//crossScalaVersions in Thisq
//Build := List("2.10.5", scalaVersion.value)
//crossPaths := false

scalacOptions in Compile in ThisBuild ++= Seq("-unchecked", "-deprecation", "-encoding", "utf8", "-language:postfixOps", "-language:implicitConVersions")

lazy val common = Project("myapp-sales-common", file("common"))

lazy val read = Project("myapp-sales-read", file("read")).configs(IntegrationTest).settings(Defaults.itSettings: _*).dependsOn(common, write)

lazy val write = Project("myapp-sales-write", file("write")).configs(IntegrationTest).settings(Defaults.itSettings: _*).dependsOn(common)


conflictWarning in ThisBuild := ConflictWarning.disable

parallelExecution in Test in ThisBuild := false

parallelExecution in IntegrationTest in ThisBuild := false

javacOptions in Compile in ThisBuild ++= Seq("-source", "1.6", "-target", "1.6")


//Remove SNAPSHOT check from the release process (for now until Squants gets a release)
releaseProcess := releaseProcess.value.filterNot(_ == ReleaseTransformations.checkSnapshotDependencies)

我的read模块build.sbt

libraryDependencies ++= Seq(
  "com.mycompany"         %% "myapp-core-read"            % myappsales.CoreVersion            % "compile", // disable using the Scala version in output paths and artifacts,
  "com.mycompany"         %% "myapp-core-write"           % myappsales.CoreVersion            % "compile",
  "com.mycompany"         %% "myapp-registration-common"  % myappsales.RegistrationVersion    % "compile",
  "com.mycompany"         %% "myapp-load-common"          % myappsales.LoadVersion            % "compile",
  "com.mycompany"         %% "myapp-core-write-test"      % myappsales.CoreVersion            % "it, test",
  "com.mycompany"         %% "myapp-core-test"            % myappsales.CoreVersion            % "it, test"
)

libraryDependencies ++= Seq(
  "com.typesafe"               % "config"                                % myappsales.TypeSafeConfigVersion % "compile",
  "org.json4s"                 %% "json4s-native"                         % Versions.Json4s         % "compile",
  "io.spray"                    % "spray-routing"                         % Versions.Spray          % "compile",
  "com.typesafe.akka"          %% "akka-actor"                            % Versions.Akka           % "compile",
  "com.typesafe.akka"          %% "akka-remote"                           % Versions.Akka           % "compile"
    exclude ("io.netty", "netty")
)

//Assemby settings
test in assembly := {}
assemblyShadeRules in assembly := Seq(
  ShadeRule.rename("play.api.libs.iteratee.**" -> "shade.play.api.libs.iteratee.@1")
    .inLibrary("play" % "play-iteratees_2.10" % "2.1-RC2")
    .inLibrary("org.reactivemongo" % "reactivemongo_2.10" % "0.8.1-SNAPSHOT"),
  ShadeRule.rename("scala.concurrent.stm.**" -> "shade.scala.concurrent.stm.@1")
    .inLibrary("org.scala-stm" % "scala-stm_2.10.0" % "0.6")
    .inLibrary("play" % "play-iteratees_2.10" % "2.1-RC2")
)

assemblyMergeStrategy in assembly := {
  case "application.conf" => MergeStrategy.concat
  case path => MergeStrategy.defaultMergeStrategy(path)
}

//Make assembly a publishable artifact
artifact in (
  Compile, assembly) := {
  val art = (artifact in (Compile, assembly)).value
  art.copy(`classifier` = Some("assembly"))
}

addArtifact(artifact in (Compile, assembly), assembly)

如何使它与现有的 myapp-core-read_2.10-2.2.33.jar jar 一起使用?

【问题讨论】:

    标签: scala sbt akka


    【解决方案1】:

    重要提示: Scala 2.10 和 2.11 不是二进制兼容的,因此您必须重新编译您所依赖的模块

    问题答案:

    这似乎与 SBT 如何解决依赖关系有关。当您像这样声明依赖项时:

    "com.mycompany" %% "myapp-core-read" % myappsales.CoreVersion  
    

    声明中的%%部分意味着SBT会自动将scala版本附加到库名中,所以你的依赖就变成了

    group: com.mycompany
    artifactId: myapp-core-read_2.11
    version: `your version`
    

    您的 myapp-core-read 似乎没有使用 scala 2.11 编译,因此 SBT 找不到正确的版本。

    您可以通过使用% 并手动应用正确的后缀来避免这种情况,因此您的依赖关系将变为:

    "com.mycompany" % "myapp-core-read_2.10" % myappsales.CoreVersion
    

    话虽如此,我认为 scala 2.10 和 2.11 不是二进制兼容的,因此您可能必须使用 2.11 重新编译 myapp-core-read 模块。

    【讨论】:

    • 哇,很棒的信息,几乎解决了我的问题。谢谢丹尼斯·罗斯卡
    • 我删除了一个 %,但它没有在寻找 "myapp-core-read_2.10" % myappsales.CoreVersion。它正在寻找 "myapp-core-read" % myappsales.CoreVersion。能否请您帮助我如何使用本地存储库中的现有 jar。
    • @Ravi 如果您删除一个%,您必须自己添加后缀。因此,在依赖声明中,您必须将myapp-core-read 更改为myapp-core-read_2.10。无论如何,正如我在答案中所说,您很可能还必须使用 2.11 重新编译 myapp-core-read。
    猜你喜欢
    • 2015-11-14
    • 2013-05-28
    • 1970-01-01
    • 2019-10-17
    • 2013-02-24
    • 2021-06-11
    • 2011-04-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多