前言

上篇文章介绍了下 安装sbt环境 启动scala项目

​安装SBT环境运行Scala项目​

为什么要弄这个 因为我本来是想对spark源码编译部署

spark是用scala语言编译的

spark源码

https://gitee.com/pingfanrenbiji/spark

spark提供的编译方式

编译的前提是将所有的依赖包都下载下来

而资源包管理方式有maven、graddle、sbt等

maven方式

将maven资源库修改为阿里云资源库

配置方式

方式1

Spark源码打包编译的过程_maven

方式2
<repositories>
<repository>
<id>aliyun</id>
<url>https://maven.aliyun.com/repository/public</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
  • 如果你只是配置了repositories,那么你会发现在mvn在下载依赖的时候,一部分从阿里云下载,一部分还是从默认的仓库(https://repo.maven.apache.org )下载
  • 只有项目本身的依赖,走了aliyun这个repository,maven命令需要的插件(比如clean、install都是maven的插件),走的还是默认的repository
方式3

在maven setting文件中

 <mirrors>
<mirror>
<id>nexus-aliyun</id>
<mirrorOf>central</mirrorOf>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>
</mirrors>

maven编译打包

mvn -DskipTests clean package

Spark源码打包编译的过程_scala_02

sbt方式

Spark源码打包编译的过程_maven_03

这样下载依赖包的速度超级慢

原因是从​​https://repo1.maven.org/maven2/​​这个国外的资源库网站下载的

那么类似于maven 准备换成国内的资源库

国内资源库配置方法

上面文章介绍的是配置的华为的国内资源库

这里配置下阿里的国内资源库

vim ~/.sbt/repositories

[repositories]
aliyun-maven-repo: https://maven.aliyun.com/repository/public
aliyun-nexus: https://maven.aliyun.com/nexus/content/groups/public/
typesafe: https://repo.typesafe.com/typesafe/ivy-releases/, [organization]/[module]/(scala_[scalaVersion]/)(sbt_[sbtVersion]/)[revision]/[type]s/[artifact](-[classifier]).[ext], bootOnly
maven-central
sonatype-oss-releases
sonatype-oss-snapshots
ivy-sbt-plugin: https://dl.bintray.com/sbt/sbt-plugin-releases/, [organization]/[module]/(scala_[scalaVersion]/)(sbt_[sbtVersion]/)[revision]/[type]s/[artifact](-[classifier]).[ext]

检验配置的国内资源库是否生效

mkdir test
cd test
sbt
show fullResolvers

Spark源码打包编译的过程_资源库_04

可以看到 配置的阿里云资源库 已经生效了

既然sbt资源库下载已经换成了国内的资源库了

那么试试吧

Spark源码打包编译的过程_maven_05

咦!怎么还是​​repo1.maven.org​

接下来说一下 我努力挣扎的过程 反正结果以失败而告终 最后先放弃 😂

去掉sbt默认从repo1.maven.org下载的配置

第一次尝试

Spark源码打包编译的过程_maven_06

我是用的=直接将默认的resolvers直接替换掉

而不是用+= 默认加新增的这个

Spark源码打包编译的过程_scala_07

但实际效果不行

第二次尝试

我直接在spark源码中全局搜索 ​​repo1.maven.org​​ 没有找到

那么我就想:通过sbt 下载资源库 那么应该在sbt源码中配置的

目的:下载sbt源码 把所有的​​repo1.maven.org​​​换成阿里云​​https://maven.aliyun.com/repository/public​​ 然后重新打包

下载sbt源码
https://gitee.com/pingfanrenbiji/sbt.git

替换​​repo1.maven.org​

Spark源码打包编译的过程_maven_08

执行打包
mkdir sbt-modules
cd sbt-modules

for i in sbt io librarymanagement zinc; do \
git clone https://gitee.com/pingfanrenbiji/$i.git && (cd $i; git checkout -b develop origin/develop)
done

cd sbt
./sbt-allsources.sh

这个脚本的内容

Spark源码打包编译的过程_资源库_09

需要sbt命令

我瞬间凌乱了(因为我的无知)

心理活动:我的目的是下载sbt源码 然后打包生成可执行文件 sbt

但现在我打包 却需要sbt这个可执行文件。

我知道 我暂时搞不清楚这个问题的原因了 所以先准备下sbt环境 执行这个脚本 看看结果如何吧

  • sbt环境

a、上篇文章也介绍了一种方式 就是直接下载sbt资源包 然后配置环境变量

b、这里用另外一种方法

 brew install sbt

==> Downloading https://github.com/sbt/sbt/releases/download/v1.3.13/sbt-1.3.13.
==> Downloading from https://github-production-release-asset-2e65be.s3.amazonaws
######################################################################## 100.0%
==> Caveats
You can use $SBT_OPTS to pass additional JVM options to sbt.
Project specific options should be placed in .sbtopts in the root of your project.
Global settings should be placed in /usr/local/etc/sbtopts
==> Summary
🍺 /usr/local/Cellar/sbt/1.3.13: 9 files, 1.4MB, built in 2 seconds

有了sbt环境 再执行sbt-allsources.sh这个脚本

Spark源码打包编译的过程_资源库_10

瞬间吐血~(怎么还是这个资源库 我不是已经通过修改源码的方式去掉了嘛)

暂时先放弃 因为这块工作上没有要求 是我自己想学习scala研究的

后记

  • 进入一个未知的领域 在有老师指导的情况下 肯定会事半功倍 但往往都没有老师来指导 那只能靠自学
  • 自学虽然会磕磕绊绊 遇到很多挫折但学会了之后 你就会破茧成蝶

朋友们 共勉吧!