最近在使用Idea + Scala + sbt (Macbook)做一些开发测试,但是由于默认使用国外的源,导致下载速度奇慢无比,所以想修改默认的repositories,但是怎么样才能修改默认的repositories并在Idea中使用修改后的配置呢?经过一天的折腾,总算是知道怎么新修改了,故在此备忘,也希望能帮助一些同学少走弯路。
在这一篇博客:http://blog.csdn.net/suqier1314520/article/details/41775905中有提到修改Idea使用的sbt方法,即使用自定义路径的sbt, File -> Other settings ->Default settings -> Build,Execution,Deployment -> Build Tools -> SBT -> Launcher(sbt-launcher.jar) 改为Custom 找到sbt安装路径下的sbt-launcher.jar,并在VM Parameters中添加:
-Xmx2048M
-XX:MaxPermSize=512m
-XX:ReservedCodeCacheSize=256m
-Dsbt.log.format=true
-Dsbt.global.base=D:/JavaDev/.sbt
-Dsbt.boot.directory=D:/JavaDev/.sbt/boot/
-Dsbt.ivy.home=D:/JavaDev/.ivy2
-Dsbt.repository.config=D:/JavaDev/sbt/conf/repo.properties
然后,手动创建文件D:/JavaDev/sbt/conf/repo.properties,并将如下内容加入到该文件:
- [repositories]
- local
- Nexus osc : http://maven.oschina.net/content/groups/public/
- Nexus osc thirdparty : http://maven.oschina.net/content/repositories/thirdparty/
- typesafe: http://repo.typesafe.com/typesafe/ivy-releases/, [organization]/[module]/(scala_[scalaVersion]/)(sbt_[sbtVersion]/)[revision]/[type]s/[artifact](-[classifier]).[ext], bootOnly
- typesafe2: http://repo.typesafe.com/typesafe/releases/
- sbt-plugin: http://repo.scala-sbt.org/scalasbt/sbt-plugin-releases/
- sonatype: http://oss.sonatype.org/content/repositories/snapshots
- uk_maven: http://uk.maven.org/maven2/
- ibibli: http://mirrors.ibiblio.org/maven2/
- repo2: http://repo2.maven.org/maven2/
遗憾的是,Idea创建项目,并在build.sbt添加依赖后,默认还是从repo1.maven.org的库中下载,所以速度还是很慢。
后来,我将Idea重的 Launcher(sbt-launcher.jar) 中的sbt可执行文件路径改为sbt安装目录下的sbt,并在/Users/macor/.sbt目录下,创建repositories(sbt默认会读取里面配置配好的repository地址),并将repo,properties的内容复制进去,删除repo.properties文件,再次在Idea中创建新的sbt项目,终于可以优先使用国内的repository了。

此外,还在http://9leg.com/scala/2015/10/17/scala-play-setting.html发现另外一种方法(没尝试,估计也可行):把sbt-launcher.jar 解压出来,jar里面是编译好的class文件,其中在sbt目录下有一个sbt.boot.properties文件,内容如下:
- [repositories]
- local
- typesafe-ivy-releases: https://repo.typesafe.com/typesafe/ivy-releases/, [organization]/[module]/[revision]/[type]s/[artifact](-[classifier]).[ext], bootOnly
- maven-central
-
- [boot]
- directory: ${sbt.boot.directory-${sbt.global.base-${user.home}/.sbt}/boot/}
-
- [ivy]
- ivy-home: ${sbt.ivy.home-${user.home}/.ivy2/}
- checksums: ${sbt.checksums-sha1,md5}
- override-build-repos: ${sbt.override.build.repos-false}
- repository-config: ${sbt.repository.config-${sbt.global.base-${user.home}/.sbt}/repositories}
在repositories配置项中添加自定义的国内镜像,可以是如下:
- [repositories]
- local
- oschina nexus:http://maven.oschina.net/content/groups/public/
- typesafe: http://repo.typesafe.com/typesafe/ivy-releases/, [organization]/[module]/(scala_[scalaVersion]/)(sbt_[sbtVersion]/)[revision]/[type]s/[artifact](-[classifier]).[ext], bootOnly
- maven-central
- sbt-plugins-repo: http://repo.scala-sbt.org/scalasbt/sbt-plugin-releases/, [organization]/[module]/(scala_[scalaVersion]/)(sbt_[sbtVersion]/)[revision]/[type]s/[artifact](-[classifier]).[ext]
- play: http://private-repo.typesafe.com/typesafe/maven-releases/
- sonatype-snapshots: https://oss.sonatype.org/content/repositories/snapshots
- typesafe-releases: https://repo.typesafe.com/typesafe/releases
- typesafe-ivy-releasez: https://repo.typesafe.com/typesafe/ivy-releases, [organization]/[module]/(scala_[scalaVersion]/)(sbt_[sbtVersion]/)[revision]/[type]s/[artifact](-[classifier]).[ext]
最后,把sbt.boot.properties打包进sbt-launcher.jar或者重新打包,即可修改默认的repository。