Home > Net >  Why does sbt try to pull my interproject dependency?
Why does sbt try to pull my interproject dependency?

Time:02-23

I have a multi-project build with a build.sbt that looks as follows:

import lmcoursier.CoursierConfiguration
import lmcoursier.definitions.Authentication

ThisBuild / version := "0.1.0-SNAPSHOT"

ThisBuild / scalaVersion := "2.12.12"

val adoMavenUsername = "."
val adoMavenPassword = "ADO_PAT"
val adoRepoIdWithView = "ADO-id"

val adoMavenRepos = Vector(
  MavenRepository(adoRepoIdWithView, s"https://adoMavenHost/adoOrganization/adoProject/_packaging/${adoRepoIdWithView.replace("@", "@")}/maven/v1")
)

val adoAuthentication =
  Authentication(user = adoMavenUsername, password = adoMavenPassword)
    .withOptional(false)
    .withHttpsOnly(true)
    .withPassOnRedirect(false)

val coursierConfiguration = {
  val initial =
      CoursierConfiguration()
        .withResolvers(adoMavenRepos)
        .withClassifiers(Vector("", "sources"))
        .withHasClassifiers(true)

  adoMavenRepos.foldLeft(initial) {
    case (conf, repo) ⇒
      conf.addRepositoryAuthentication(repo.name, adoAuthentication)
  }
}

lazy val mainSettings = Seq(
  organization := "org.some",
  csrConfiguration := coursierConfiguration,
  updateClassifiers / csrConfiguration := coursierConfiguration
)

lazy val root = (project in file("."))
  .settings(mainSettings: _*)
  .settings(
    name := "sbt-test",
  ).aggregate(core, util)

lazy val core = (project in file("core"))
  .settings(mainSettings: _*)
  .settings(
    name := "core",
  ).dependsOn(util)

lazy val util = (project in file("util"))
  .settings(mainSettings: _*)
  .settings(
    name := "util"
  )

For some reason, coursier attempts to download the util package externally during the core/update task. This is not what I want, as it should resolve it internally as part of the project. The package is not added to libraryDependencies, so I'm baffled why it would attempt the download. The above example will fail because the Azure DevOps credentials are and Maven repository are incorrect, but it shows the attempt to download util.

It seems somehow related to this Github issue.

CodePudding user response:

The default CoursierConfiguration constructor sets the interProjectDependencies property to an empty Vector. To fix this, manually add resolvers on top of sbt's csrConfiguration taskKey using .withResolvers.

This is what the solution looks like applied to my question, largely based on this Github comment:

val adoMavenUsername = "."
val adoMavenPassword = "ADO_PAT"
val adoRepoIdWithView = "ADO-id"
val adoMavenHost      = "pkgs.dev.azure.com"

val adoMavenRepos = Vector(
  MavenRepository(adoRepoIdWithView, s"https://$adoMavenHost/adoOrganization/adoProject/_packaging/$adoRepoIdWithView/maven/v1")
)

lazy val mainSettings = Seq(
  organization := "org.some",
  csrConfiguration := {
    val resolvers = csrResolvers.value    adoMavenRepos
    val conf = csrConfiguration.value.withResolvers(resolvers.toVector)
    val adoCredentialsOpt = credentials.value.collectFirst { case creds: DirectCredentials if creds.host == adoMavenHost => creds }
    val newConfOpt = adoCredentialsOpt.map { adoCredentials =>
      val auths =
        resolvers
          .collect {
            case repo: MavenRepository if repo.root.startsWith(s"https://$adoMavenHost/") => {
              repo.name ->
                Authentication(adoCredentials.userName, adoCredentials.passwd)
            }
          }
      auths.foldLeft(conf) { case (conf, (repoId, auth)) => conf.addRepositoryAuthentication(repoId, auth) }
    }
    newConfOpt.getOrElse(conf)
},
  updateClassifiers / csrConfiguration := coursierConfiguration
)

  • Related