首页 文章

如何让sbt使用本地maven代理存储库(Nexus)?

提问于
浏览
70

我有一个sbt(Scala)项目,目前从Web上提取工件 . 我们希望转向一个可以缓存工件的企业标准化Nexus存储库 . 从Nexus文档中,我了解如何为Maven项目执行此操作 . 但是sbt显然使用了不同的方法 . (我知道Ivy会以某种方式参与其中,但我从未使用它,也不了解它是如何工作的 . )

如何告诉sbt和/或底层Ivy将所有依赖项用于企业Nexus存储库系统?我想要使用某种项目级配置文件的答案,以便我们的源存储库的新克隆将自动使用代理 . (即,在点目录中使用每个用户的配置文件进行捣乱是不可行的 . )

谢谢!

6 回答

  • 12

    第1步:按照Detailed Topics: Proxy Repositories中的说明进行操作,我已将其汇总并添加到下面:

    这是必需的,因为一些存储库管理器无法处理混合在一起的Ivy风格和Maven风格的存储库 .

    • 创建一个文件 repositories ,列出您的主要公司存储库以及您在步骤1中创建的任何额外存储库,格式如下所示:
    [repositories]
      my-maven-proxy-releases: http://repo.example.com/maven-releases/
      my-ivy-proxy-releases: http://repo.example.com/ivy-releases/, [organization]/[module]/(scala_[scalaVersion]/)(sbt_[sbtVersion]/)[revision]/[type]s/[artifact](-[classifier]).[ext]
    
    • 将该文件保存在主目录的 .sbt 目录中,或在sbt命令行中指定:
    sbt -Dsbt.repository.config=<path-to-your-repo-file>
    

    Good news for those using older versions of sbt :尽管至少在sbt 0.12.0启动程序jar中,旧sbt版本的启动属性文件不包含所需的行(提及 repository.config 的行),如果你仍然适用于那些版本的sbt编辑这些文件以添加所需的行,并将它们重新打包到sbt 0.12.0启动程序jar中!这是因为该功能在启动器中实现,而不是在sbt本身中实现 . 据称sbt 0.12.0 Launcher 能够启动所有版本的sbt,直到0.7!

    步骤2:要确保未使用外部存储库,请从解析程序中删除默认存储库 . 这可以通过以下两种方式之一完成:

    • 添加上面“详细主题”页面上提到的命令行选项 -Dsbt.override.build.repos=true . 这将导致您在文件中指定的存储库覆盖任何sbt文件中指定的任何存储库 . 这可能仅适用于0.12及以上的版本 - 我还没有尝试过 .

    • 在您的构建文件中使用 fullResolvers := Seq( 解析程序为您的公司maven存储库 ) ,而不是 resolvers ++=resolvers := 或您以前使用的任何内容 .

  • 5

    好的,在Mark Harrah的sbt邮件列表的帮助下,我有一个有效的答案 .

    我的构建类现在看起来像以下(加上其他一些repos):

    import sbt._
    
    //By extending DefaultWebProject, we get Jetty support
    class OurApplication(info: ProjectInfo) extends DefaultWebProject(info) {
    
      // This skips adding the default repositories and only uses the ones you added
      // explicitly. --Mark Harrah
      override def repositories = Set("OurNexus" at "http://our.nexus.server:9001/nexus/content/groups/public/") 
      override def ivyRepositories = Seq(Resolver.defaultLocal(None)) ++ repositories
    
      /* Squeryl */
      val squeryl = "org.squeryl" % "squeryl_2.8.0.RC3" % "0.9.4beta5"
    
      /* DATE4J */
      val date4j = "hirondelle.date4j" % "date4j" % "1.0" from "http://www.date4j.net/date4j.jar"
    
      // etc
    }
    

    现在,如果我从我的机器的 .ivy2/cache 目录中删除Squeryl树,sbt会尝试使用适当的URL从Nexus树中获取它 . 问题解决了!

  • 0

    您所需要的只是定义一个属性文件 sbt.boot.properties ,它允许您:

    • 重新定义常 Spring 藤缓存位置(我需要它,因为它将成为我们roaming Windows profile的一部分,这在我们商店的磁盘空间中受到严重限制 . 请参阅Issue 74

    • 定义您想要的任何其他Maven回购

    C:\HOMEWARE\apps\sbt-0.74\sbt.boot.properties
    
        [scala]
          version: 2.7.7
        #  classifiers: sources, javadoc
    
        [app]
          org: org.scala-tools.sbt
          name: sbt
          version: read(sbt.version)
          class: sbt.xMain
          components: xsbti
          cross-versioned: true
          classifiers: sources, javadoc
    
        [repositories]
          local
          my-nexus: http://my.nexus/nexus/content/repositories/scala-tools/, [organization]/[module]/[revision]/[type]s/[artifact](-[classifier]).[ext]
          maven-local
        #  sbt-db: http://databinder.net/repo/, [organization]/[module]/[revision]/[type]s/[artifact](-[classifier]).[ext]
        #  maven-central
        #  scala-tools-releases
        #  scala-tools-snapshots
    
        [boot]
         directory: project/boot
         properties: project/build.properties
         prompt-create: Project does not exist, create new project?
         prompt-fill: true
         quick-option: true
    
        [log]
         level: debug
    
        [app-properties]
         project.name: quick=set(test), new=prompt(Name)[p], fill=prompt(Name)
         project.organization: new=prompt(Organization)[org.vonc]
         project.version: quick=set(1.0), new=prompt(Version)[1.0], fill=prompt(Version)[1.0]
         build.scala.versions: quick=set(2.8.0.RC2), new=prompt(Scala version)[2.8.0.RC2], fill=prompt(Scala version)[2.8.0.RC2]
         sbt.version: quick=set(0.7.4), new=prompt(sbt version)[0.7.4], fill=prompt(sbt version)[0.7.4]
         project.scratch: quick=set(true)
         project.initialize: quick=set(true), new=set(true)
    
        [ivy]
         cache-directory: C:\HOMEWARE\projects\.ivy2\cache
    

    注意:此 sbt.boot.properties 文件的灵感来自:

    我评论了任何外部Maven存储库定义,并添加了对我自己的Nexus Maven存储库的引用 .

    可以按以下方式之一以增加的优先顺序配置启动程序:替换jar中的/sbt/sbt.boot.properties文件 . 将一个名为sbt.boot.properties的配置文件放在类路径上 . 将它放在没有/ sbt前缀的类路径根目录中 . 在命令行上指定备用配置的位置 . 这可以通过以下方式完成:将位置指定为系统属性sbt.boot.properties,或者作为前缀为'@'的启动程序的第一个参数 . 系统属性的优先级较低 . 相对路径的解析是:首先针对当前工作目录,然后针对用户的主目录,然后针对包含启动器jar的目录 . 如果这些尝试都不成功,则会生成错误 .


    定义一个sbt.bat包装器(in为了确保指定你的 sbt.boot.properties ),例如:

    C:\HOMEWARE>more C:\HOMEWARE\bin\sbt.BAT
    @echo off
    set t=%~dp0
    set adp0=%t:C:\="%"
    
    set SBT_DIR=%adp0%..\apps\sbt-0.74
    dir C:\%SBT_DIR%\sbt-launch-0.7.4.jar
    # if needed, add your proxy settings
    set PROXY_OPTIONS=-Dhttp.proxyHost=my.proxy -Dhttp.proxyPort=80xx -Dhttp.proxyUser=auser -Dhttp.proxyPassword=yyyy
    set JAVA_OPTIONS=-XX:+CMSClassUnloadingEnabled -XX:MaxPermSize=256m -Xmx512M -cp C:\HOMEWARE\apps\sbt-0.74\sbt-launch-0.7.4
    set SBT_BOOT_PROPERTIES=-Dsbt.boot.properties="sbt.boot.properties"
    cmd /C C:\HOMEWARE\apps\jdk4eclipse\bin\java.exe %PROXY_OPTIONS% %JAVA_OPTIONS% %SBT_BOOT_PROPERTIES% -jar C:\HOMEWARE\apps\sbt-0.74\sbt-launch-0.7.4.jar %*
    

    你的sbt将从下载文件 only

    • 你的Nexus

    • 您当地的Maven回购 .


    刚刚在家中使用旧的Nexus opensource 1.6进行了测试,我运行了java 1.6,sbt07.4

    C:\Prog\Java\jdk1.6.0_18\jre\bin\java  -Xmx512M -Dsbt.boot.properties=sbt.boot.properties - jar "c:\Prog\Scala\sbt\sbt-launch-0.7.4.jar"
    

    这给了:

    [success] Build completed successfully.
    C:\Prog\Scala\tests\pp>sbt
    Getting Scala 2.8.0 ...
    downloading http://localhost:8081/nexus/content/repositories/scala/org/scala-lang/scala-compiler/2.8.0/scala-compiler-2.
    8.0.jar ...
            [SUCCESSFUL ] org.scala-lang#scala-compiler;2.8.0!scala-compiler.jar (311ms)
    downloading http://localhost:8081/nexus/content/repositories/scala/org/scala-lang/scala-library/2.8.0/scala-library-2.8.
    0.jar ...
            [SUCCESSFUL ] org.scala-lang#scala-library;2.8.0!scala-library.jar (185ms)
    :: retrieving :: org.scala-tools.sbt#boot-scala
            confs: [default]
            2 artifacts copied, 0 already retrieved (14484kB/167ms)
    [info] Building project test 0.1 against Scala 2.8.0
    [info]    using sbt.DefaultProject with sbt 0.7.4 and Scala 2.7.7
    

    如果我在sbt.boot.properties文件中尝试一个有趣的值:

    C:\Prog\Scala\tests\pp>sbt
    Getting Scala 2.9.7 ...
    
    :: problems summary ::
    :::: WARNINGS
                    module not found: org.scala-lang#scala-compiler;2.9.7
            ==== nexus: tried
              http://localhost:8081/nexus/content/repositories/scala/org/scala-lang/scala-compiler/2.9.7/scala-compiler-2.9.7.pom
              -- artifact org.scala-lang#scala-compiler;2.9.7!scala-compiler.jar:
              http://localhost:8081/nexus/content/repositories/scala/org/scala-lang/scala-compiler/2.9.7/scala-compiler-2.9.7.jar
    

    所以它确实限制了我定义的两个回购:

    [repositories]
    nexus:  http://localhost:8081/nexus/content/repositories/scala
    nexus2: http://localhost:8081/nexus/content/repositories/scala, [organization]/[module]/[revision]/[type]s/[artifact](-[classifier]).[ext]
    

    (我评论了其他一切: localmaven-local ,...)

    如果我评论所有存储库并为 sbt.boot.properties 中的scala版本添加一个有趣的值(2.7.9),我会得到(就像OP一样)

    C:\Prog\Scala\tests\pp>sbt
    Error during sbt execution: No repositories defined.
    

    如果我把2.7.7(虽然仍然有所有repo评论),是的,它不会产生错误:

    C:\Prog\Scala\tests\pp>sbt
    [info] Building project test 0.1 against Scala 2.8.0
    [info]    using sbt.DefaultProject with sbt 0.7.4 and Scala 2.7.7
    

    But that's only because it already had downloaded scala2.8.0 during my previous tries.
    如果我从 project/boot 目录中删除该库,那么它将抛出异常:

    [info]    using sbt.DefaultProject with sbt 0.7.4 and Scala 2.7.7
    > C:\Prog\Scala\tests\pp>sbt
    Error during sbt execution: No repositories defined.
            at xsbt.boot.Pre$.error(Pre.scala:18)
            at xsbt.boot.Update.addResolvers(Update.scala:197)
    ...
            at xsbt.boot.Boot$.main(Boot.scala:15)
            at xsbt.boot.Boot.main(Boot.scala)
    Error loading project: Error during sbt execution: No repositories defined.
    
  • 6

    编辑sbt_home / conf“sbtconfig.txt”中的配置文件

    加两行

    -Dsbt.override.build.repos=true
    -Dsbt.repository.config="C:/Program Files (x86)/sbt/conf/repo.properties"
    

    repo.properties的内容是

    [repositories]
        local
        public: http://222.vvfox.com/public  <-fix this ,write your local nexus group url
    
  • 31

    好吧,这已经让我烦恼了一段时间,所以我发现一个人在github上为maven编写了一个名为maven-sbt的SBT插件,所以你所要做的就是将它包含在你的插件项目中并使你的项目与maven.MavenDependencies和所有您的操作,例如更新和发布 - 本地工作与您当地的maven . 关于这一点的好处是,如果你像我一样,你的组织就是全部 . 所以,你们所有的lib都在你们当地的maven回购中,但是如果你出于某种原因首先用sbt构建,那么你们也开始在常 Spring 藤中获得一堆或者 jar . 多么浪费空间和时间,因为你仍然需要为你的maven构建获取它们 .

    也就是说,我希望这可以内置到sbt中,所以我不需要将它添加到每个项目中 . 也许至少作为处理器 . 他在一件事情中提到我想将它添加到0.9但我无法找到它 .

  • 9

    我收到此错误是因为我在 ~/.sbt/repositories 中有一个空白文件 . 将存储库添加到文件并删除文件都解决了问题 .

相关问题