SBT-修改资源库地址

做大数据开发, 用scala的都会用到sbt来打包依赖, 但是sbt默认配置里连接的国外地址, 根本就下不下来依赖包, 而且还有的地址是https的, 连接都被拒绝了, 所以我们就来修改sbt默认的配置.

首先, 我的系统是mac, 不管是用idea装的scala插件里带的sbt, 还是用Homebrew安装的sbt, 都会在用户目录下有个.sbt文件夹, 执行ll -a即可看到.

方法一

进入.sbt文件夹, 创建一个名叫repositories的文件, 默认应该是没有的, 并加入以下内容, 来覆盖默认配置

1
2
3
4
5
6
7
8
[repositories]
local
aliyun: http://maven.aliyun.com/nexus/content/groups/public/
maven-central: http://repo1.maven.org/maven2/
sbt-maven-releases: http://repo.scala-sbt.org/scalasbt/maven-releases/, bootOnly
sbt-maven-snapshots: http://repo.scala-sbt.org/scalasbt/maven-snapshots/, bootOnly
typesafe-ivy-releases: http://repo.typesafe.com/typesafe/ivy-releases/, [organization]/[module]/[revision]/[type]s/[artifact](-[classifier]).[ext], bootOnly
sbt-ivy-snapshots: http://repo.scala-sbt.org/scalasbt/ivy-snapshots/, [organization]/[module]/[revision]/[type]s/[artifact](-[classifier]).[ext], bootOnly

可见其中加入了阿里云的maven地址, 之前用过开源中国的, 后来他们干不下去了, 这次阿里云接盘, 应该不会轻易的狗带.

方法二

理论上添加以上配置后即可覆盖原来的默认配置, 然而我的sbt好像死活就是不行, 所以我还有第二个比较hacker的方法, 既然默认配置覆盖不掉, 那我就把默认配置改了.

我的sbt是用Homebrew安装的, 默认安装位置在 /usr/local/Cellar/sbt, 此处有一个bin文件夹和一个conf文件夹, 我们要破坏的jar包就是bin下的sbt-launch.jar文件.
tar解压sbt-launch.jar文件可以得到以下文件结构:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
total 72
drwxr-xr-x 3 wdp staff 102B 5 12 2016 META-INF
-rwxr-xr-x 1 wdp staff 59B 8 17 2015 module.properties
drwxr-xr-x 3 wdp staff 102B 5 12 2016 org
drwxr-xr-x 3 wdp staff 102B 1 3 19:58 sbt
-rwxr-xr-x 1 wdp staff 681B 5 12 2016 sbt.boot.properties0.10.0
-rwxr-xr-x 1 wdp staff 681B 5 12 2016 sbt.boot.properties0.10.1
-rwxr-xr-x 1 wdp staff 681B 5 12 2016 sbt.boot.properties0.11.0
-rwxr-xr-x 1 wdp staff 681B 5 12 2016 sbt.boot.properties0.11.1
-rwxr-xr-x 1 wdp staff 681B 5 12 2016 sbt.boot.properties0.11.2
-rwxr-xr-x 1 wdp staff 675B 5 12 2016 sbt.boot.properties0.11.3
-rwxr-xr-x 1 wdp staff 959B 5 12 2016 sbt.boot.properties0.13.0
-rwxr-xr-x 1 wdp staff 690B 5 12 2016 sbt.boot.properties0.7
drwxr-xr-x 61 wdp staff 2.0K 5 12 2016 scala
drwxr-xr-x 3 wdp staff 102B 5 12 2016 xsbt
drwxr-xr-x 24 wdp staff 816B 5 12 2016 xsbti

默认的配置文件就在sbt文件夹下的sbt.boot.properties文件, 对此文件内的内容进行替换, 如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
[scala]
version: ${sbt.scala.version-auto}

[app]
org: ${sbt.organization-org.scala-sbt}
name: sbt
version: ${sbt.version-read(sbt.version)[1.0.0-M4]}
class: ${sbt.main.class-sbt.xMain}
components: xsbti,extra
cross-versioned: ${sbt.cross.versioned-false}
resources: ${sbt.extraClasspath-}

[repositories]
local
aliyun: http://maven.aliyun.com/nexus/content/groups/public
maven-central: http://repo1.maven.org/maven2/
sbt-maven-releases: http://repo.scala-sbt.org/scalasbt/maven-releases/, bootOnly
sbt-maven-snapshots: http://repo.scala-sbt.org/scalasbt/maven-snapshots/, bootOnly
typesafe-ivy-releases: http://repo.typesafe.com/typesafe/ivy-releases/, [organization]/[module]/[revision]/[type]s/[artifact](-[classifier]).[ext], bootOnly
sbt-ivy-snapshots: http://repo.scala-sbt.org/scalasbt/ivy-snapshots/, [organization]/[module]/[revision]/[type]s/[artifact](-[classifier]).[ext], bootOnly

[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}

修改完成, 再执行jar -cfM ./sbt-launch.jar .打成jar包, 把bin目录下的原始jar包替换掉即可, 这次保证妥妥的.

附上我改好的jar包下载地址, 送给懒得自己动手的人们, 传送门 -> sbt-launch.jar

打赏
  • 版权声明: 本博客所有文章除特别声明外,均采用 Apache License 2.0 许可协议。转载请注明出处!
  • © 2017-2023 王丹鹏
  • Powered by Hexo Theme Ayer
  • 冀ICP备15029707号

请我喝杯咖啡吧~

支付宝
微信