sbt升级至1.7.3后addRepositoryAuthentication方法失效,求替代方案
sbt 1.7.3 中 GitLab 私有库认证配置替代方案
问题背景
原 sbt 1.7.2 配置 GitLab 私有库认证的代码如下:
scalaVersion := "2.13.13" /*** Gitlab private lib repository configuration ****/ resolvers += "gitlab" at "https://my-gitlab-repo.fun" val token = "token" val auth:Authentication = Authentication(Seq(("Private-Token",token))) csrConfiguration ~= (_.addRepositoryAuthentication("gitlab",auth)) updateClassifiers / csrConfiguration ~= (_.addRepositoryAuthentication("gitlab",auth))
升级到 sbt 1.7.3 后,首先遇到 Authentication 构造方法重载错误,修改为 Authentication("Private-Token",token) 后,又出现如下错误:
build.sbt:59: error: value addRepositoryAuthentication is not a member of lmcoursier.CoursierConfiguration csrConfiguration ~= (_.addRepositoryAuthentication("gitlab",auth)) ^ [error] Type error in expression
解决方案
sbt 1.7.3 升级了 Coursier 相关依赖,addRepositoryAuthentication 方法被移除,需通过修改 repositoryAuth 字段配置仓库认证,同时调整 Authentication 的构造逻辑以适配新版本。
正确配置代码如下:
scalaVersion := "2.13.13" /*** Gitlab private lib repository configuration ****/ val gitlabRepo = "gitlab" at "https://my-gitlab-repo.fun" resolvers += gitlabRepo val token = "token" // 新版本 Authentication 构造,显式使用 HttpAuthentication import lmcoursier.Authentication import lmcoursier.authentication.HttpAuthentication val auth = Authentication( HttpAuthentication("Private-Token", token) ) // 通过 repositoryAuth 字段添加仓库与认证的映射,替代原 addRepositoryAuthentication 方法 csrConfiguration ~= { cfg => cfg.copy( repositoryAuth = cfg.repositoryAuth + (gitlabRepo.name -> auth) ) } updateClassifiers / csrConfiguration ~= { cfg => cfg.copy( repositoryAuth = cfg.repositoryAuth + (gitlabRepo.name -> auth) ) }
关键变更说明
- Authentication 构造逻辑:新版本需显式使用
HttpAuthentication创建认证实例,替代原直接传入键值对序列的写法。 - 仓库认证配置方式:
CoursierConfiguration的repositoryAuth是 Map 类型,通过向该 Map 添加仓库名称与认证实例的映射,完成私有库认证配置。
内容的提问来源于stack exchange,提问作者capo11
相关产品推荐
相关产品推荐

