作者 | 田伟然
回首向来萧瑟处,归去,也无风雨也无晴。
杏仁工程师,关注编码和诗词。
前言
正所谓工欲善其事必先利其器
Round 1: 十八般武艺齐上阵
添加代理
添加国内镜像源
build.sbt 配置文件中是有私有仓库的相关配置项的:
lazy val commonSettings = Seq(
//....
// ... 私有仓库
resolvers := {Resolver.url("xr-ivy-releasez", new URL("http://nexus.xxxx.com/repository/ivy-releases/"))(Resolver.ivyStylePatterns) +: resolvers.value},
resolvers := { {"xr-maven-public" at "http://nexus.xxxx.com/repository/public/"} +: resolvers.value},
// ....
)
还可以指定 ip.src 和 ip.dst 从而使得数据包更加符合我们的要求
/repository/public/*** 的请求,对应的 Host 也是我司的私有库,这说明配置是生效了的,而且都是从私有仓库进行下载。/repository/ivy-release ,果断将对应的仓库配置去掉,省去没必要的请求。
Round 2: 从半小时到五分钟
Http Request,发现请求的都是 SNAPSHOT版本的依赖库, 参见下图SNAPSHOT 每次都去请求远程仓库呢?难道是 SNAPSHOT 被区别对待,不会被缓存?
| Attribute | Description | Required |
|---|---|---|
| default | the name of the default cache to use on all resolvers not defining the cache instance to use | No, defaults to a default cache manager instance named 'default-cache' |
| defaultCacheDir | a path to a directory to use as default basedir for both resolution and repository cache(s) |
No, defaults to.ivy2/cache |
| resolutionCacheDir | the path of the directory to use for all resolution cache data | No, defaults to defaultCacheDir |
| repositoryCacheDir | the path of the default directory to use for repository cache data.This should not point to a directory used as a repository! | No, defaults to defaultCacheDir |
注意关键字
defaultCacheDir, 这个就是 Ivy 的缓存目录,对应路径为用户目录下的 .ivy2/cache。~/.ivy/cache , 果不其然,进入该目录查看一下:~/.ivy/cache 下发现了很多依赖库的目录, 下面就需要验证一下有没有缓存SNAPSHOT 的版本了, 以我司的 user-client 4.1.2-SNAPSHOT 为目标进行查找:
SNAPSHOT 的啊,可为什么不走本地缓存呢 ?To set up Cached Resolution include the following setting in your project’s build:updateOptions := updateOptions.value.withCachedResolution(true)
updateOptions:=updateOptions.value.withCachedResolution(true) 的配置,这也太简单了吧?
不管啦,先加上试试。
SNAPSHOT 就不请求远程仓库了。service模块, 该模块维护着几乎所有的依赖。Round 3:从五分钟到一分钟
SNAPSHOT 依赖每次启动都要去远程仓库拉取呢 ?能不能只在依赖的版本有更新的时候再去拉取呢 ?SNAPSHOTR 和缓存做了一些描述:When a minigraph contains either a SNAPSHOT or dynamic dependency, the graph is considered dynamic, and it will be invalidated after a single task execution. Therefore, if you have any SNAPSHOT in your graph, your experience may degrade.
SNAPSHOT 版本,会导致某个子依赖关系缓存失效, 而这个子依赖就是动态的,反正就是不会走缓存的意思。SNAPSHOT, 如果不使用 SNAPSHO 不就没这个问题了嘛。SNAPSHOT 作为版本号,而且各种互相依赖,短时间内是不可能直接过渡的了,所以直接PASS该方案了。updateOptions := updateOptions.value.withLatestSnapshots(false)
SNAPSHOT 依赖, 然后比对它们的发布时间,取最新的那一个。withLatestSnapshots(false) 可以禁用该策略, 这样 SBT 就直接使用从远程仓库拉取到的第一个 SNAPSHOT 依赖。SNAPSHOT
因为快照在不改变版本的情况下是可以重复发布的,区分同版本不同快照就只能按照时间戳来了。
SBT 无法确定本地的快照是最新的,所以每次启动都会去仓库拉取最新快照。使用 withLatestSnapshots(false) 后就不会取最新的,而是直接取第一个。
不取最新的 SNAPSHOT 对我们影响不大, 因为我们内部的服务如果有改动,基本就会升级版本号(就算是 SNAPSHOT),很少有一直重复发同版本的 SNAPSHOT 的情况。
这么一说,似乎我们连用
SNAPSHOT的意义都不大了,然而历史原因......
SNAPSHOT 每次 update 都会走网络请求的问题还是没解决。
When
offline := true, remote SNAPSHOTs will not be updated by a resolution, even an explicitly requested update. This should efectively support working without a connection to remote repositories. Reproducible examples demonstrating otherwise are appreciated. Obviously, update must have successfully run before going offline.
SNAPSHOT 的依赖了,这不正是我们要的东西吗?
offline := true的情况下,升级版本是否会从远程仓库请求?新的问题
写在最后
build.sbt 加了两行配置
updateOptions := updateOptions.value.withCachedResolution(true).withLatestSnapshots(false)
参考
1. sbt Reference Manua
https://www.scala-sbt.org/1.x/docs/
2. sbt 源码
https://www.scala-sbt.org/0.13/sxr/
3. Wireshark User’s Guide
https://www.wireshark.org/docs/wsug_html_chunked/
4. Apache Ivy Documentation (2.0.0)
http://ant.apache.org/ivy/history/2.0.0/index.html
5. Offline mode and Dependency Locking
https://github.com/sbt/sbt/wiki/User-Stories:--Offline-mode-and-Dependency-Locking
全文完
以下文章您可能也会感兴趣:
我们正在招聘 Java 工程师,欢迎有兴趣的同学投递简历到 [email protected] 。