如何为某些依赖项指定不同的解析器

How to specify a different resolver for certain dependencies

我现在需要为我的 SBT 项目指定自定义解析器,但只下载 1 或 2 个依赖项。我希望从 Maven 存储库中获取所有其他依赖项。

这是我的 build.sbt 文件:

...Project definition...

resolvers := Seq(
  "Maven" at "https://repo1.maven.org/"
)

//Akka dependencies
libraryDependencies ++= Seq(
  "com.typesafe.akka" %% "akka-actor" % akkaActorsVersion,
  "com.typesafe.akka" %% "akka-testkit" % akkaActorsVersion % Test,
  "com.typesafe.akka" %% "akka-stream" % akkaStreamsVersion,
  "com.typesafe.akka" %% "akka-stream-testkit" % akkaStreamsVersion % Test,
  "com.typesafe.akka" %% "akka-http" % akkaHttpVersion,
  "com.typesafe.akka" %% "akka-http-testkit" % akkaHttpVersion % Test,
  "com.datastax.cassandra" % "cassandra-driver-core" % "3.3.0",
  "com.typesafe.akka" %% "akka-http-spray-json" % akkaHttpVersion,
  "io.spray" %% "spray-json" % "1.3.5",
  "de.heikoseeberger" %% "akka-http-circe" % "1.23.0",
  "io.circe" %% "circe-generic" % "0.10.0",
  "com.pauldijou" %% "jwt-core" % "0.13.0",
  "com.pauldijou" %% "jwt-circe" % "0.13.0",
  "org.slf4j" % "slf4j-simple" % "1.6.4",
  "com.microsoft.azure" % "azure-storage" % "8.4.0",
  "com.datastax.cassandra" % "cassandra-driver-extras" % "3.1.4",
  "io.jvm.uuid" %% "scala-uuid" % "0.3.0",
  "org.scalatest" %% "scalatest" % "3.0.5" % "test",
  "org.cassandraunit" % "cassandra-unit" % "3.1.1.0" % "test",
  "io.monix" %% "monix" % "3.0.0-8084549",
  "org.bouncycastle" % "bcpkix-jdk15on" % "1.48"
)

resolvers := Seq("Artifactory" at "http://10.3.1.6:8081/artifactory/libs-release-local/")

Credentials += Credentials("Artifactory Realm", "10.3.1.6", ARTIFACTORY_USER, ARTIFACTORY_PASSWORD)
  
libraryDependencies ++= 
    Seq(
      "com.org" % "common-layer_2.11" % "0.3",
    )

然而,构建失败并出现错误,表明 SBT 正在尝试从 Artifactory 而不是从 Maven 获取库。 例如 Cassandra 驱动依赖

unresolved dependency: com.datastax.cassandra#cassandra-driver-extras;3.1.4: Artifactory: unable to get resource for com/datastax/cassandra#cassandra-driver-extras;3.1.4: res=http://10.3.1.6:8081/artifactory/libs-release-local/com/datastax/cassandra/cassandra-driver-extras/3.1.4/cassandra-driver-extras-3.1.4.pom

我已经搜索了 Internet 和文档,但我没有看到一个明确的方法来处理这个问题,尽管我很惊讶,因为这似乎是一个常见问题。

关于如何在 SBT 中执行 priorities/ordering 解析器的任何想法?

请注意,当你做

resolvers := Seq("resolver" at "https://path")

您正在覆盖现有的 user-defined 个附加解析器。因此,如果您正在做:

resolvers := Seq("resolver1" at "https://path1") 
resolvers := Seq("resolver2" at "https://path2")

您最终只使用了解析器 2。

为了同时拥有两个解析器,您需要执行以下操作:

resolvers ++= Seq(
  "resolver1" at "https://path1",
  "resolver2" at "https://path2"
)

SBT 根据给定解析器的顺序搜索依赖项。这意味着在给定的示例中,它将首先在 resolver1 处搜索,只有在找不到时才会转到 resolver2。

您需要了解的另一件事是,SBT 具有预定义的解析器。

您可以在以下位置阅读有关 sbt 解析器的更多信息:https://www.scala-sbt.org/1.x/docs/Resolvers.html