Rebar3
Rebar3 有二进制文件安装,和源码安装两种方式,
因为源码安装的不是最新版,安装完成还要更新一次。
网络不好很可能就卡住了,推荐二进制文件安装方式。
1. 二进制文件方式安装
下载 rebar3 二进制文件。下载完成后执行以下命令:
#拷贝到 /usr/local/Cellar 目录下
cp ./rebar3 /usr/local/Cellar/
#改变权限
chmod 755 /usr/local/Cellar/rebar3
#创建快捷方式
ln -s /usr/local/Cellar/rebar3 /usr/local/bin/rebar3
#验证
$ rebar3 version
#显示版本号,成功
rebar 3.13.1 on Erlang/OTP 22 Erts 10.7
2. 源代码方式安装
#下载源码
git clone https://github.com/erlang/rebar3.git && cd rebar3
#编译
./bootstrap
#安装并设置环境变量
./rebar3 local install
#验证安装是否成功
rebar3 version
#如果提示 -bash: rebar3: command not found
#则手动添加环境变量
vi ~/.bash_profile
#添加如下内容
export PATH=/Users/lixu/.cache/rebar3/bin:$PATH
#更新到最新版本
rebar3 local upgrade
3. 使用 rebar3
-
新建项目
rebar3 new <模版> <项目名>
模版 说明 app 创建带有监控树的有状态的单OTP应用项目 lib 创建一个OTP应用库(无监控树),对于将多个模块组合在一起作为单应用的项目很有用 release 创建一个准备发布的分散结构的项目 escript 单应用项目的特殊形式,可以将其构建为可运行脚本 plugin rebar3插件 示例:
$ rebar3 new app myapp ===> Writing myapp/src/myapp_app.erl ===> Writing myapp/src/myapp_sup.erl ===> Writing myapp/src/myapp.app.src ===> Writing myapp/rebar.config ===> Writing myapp/.gitignore ===> Writing myapp/LICENSE ===> Writing myapp/README.md
-
为项目添加依赖包
依赖包在 rebar.config 文件中的 deps 键下列出{deps, [ # 添加依赖包 package {cowboy, "2.7.0"}, # 或者向下面这样,为了兼容之前的版本 alternatively, source {cowboy, {git, "git://github.com/ninenines/cowboy.git", {tag, "1.0.1"}}} ]}.
再在 /src/*.app.src 文件中添加所需要的依赖
{application, <APPNAME>, [{description, ""}, {vsn, "<APPVSN>"}, {registered, []}, {modules, []}, {applications, [ kernel, stdlib, + cowboy #新添加的依赖包 ]}, {mod, {<APPNAME>_app, []}}, {env, []} ]}.
-
编辑 src/hello_erlang_app.erl 文件,将代码添加到start/2函数中
-module(erl_rebar3_app). -behaviour(application). -export([start/2, stop/1]). start(_StartType, _StartArgs) -> Dispatch = cowboy_router:compile([ {'_', [{"/", hello_handler, []}]} ]), {ok, _} = cowboy:start_clear( my_http_listener, [{port, 8080}], #{env => #{dispatch => Dispatch}} ), erl_rebar3_sup:start_link(). stop(_State) -> ok.
-
添加 src/hello_handle.erl 文件
-module(hello_handler). -export([init/2]). init(Req, State) -> Req = cowboy_req:reply(200, #{<<"content-type">> => <<"text/plain">>}, <<"Hello Erlang!">>, Req), {ok, Req, State}.
-
构建项目
rebar3 shell #构建并运行项目 rebar3 compile #构建项目
Erlang.mk
-
下载 erlang.mk 文件到项目目录
或用命令下载wget https://erlang.mk/erlang.mk #或者 curl -O https://erlang.mk/erlang.mk
-
执行以下命令,构建引导程序
make -f erlang.mk bootstrap bootstrap-rel SP=2
其它构建几种方式:
#Getting started with OTP applications make -f erlang.mk bootstrap #Getting started with OTP libraries make -f erlang.mk bootstrap-lib #Getting started with OTP releases and libraries make -f erlang.mk bootstrap-lib bootstrap-rel #Getting started with OTP releases and applications make -f erlang.mk bootstrap bootstrap-rel
-
编辑 makefile,来获取和编译Cowboy
PROJECT = hello_cowboy DEPS = cowboy dep_cowboy_commit = 2.7.0 DEP_PLUGINS = cowboy #加载Cowboy提供的插件 include erlang.mk
-
编辑 src/hello_erlang_app.erl 文件,将代码添加到start/2函数中
start(_Type, _Args) -> Dispatch = cowboy_router:compile([ {'_', [{"/", hello_handler, []}]} ]), {ok, _} = cowboy:start_clear(my_http_listener, [{port, 8080}], #{env => #{dispatch => Dispatch}} ), hello_erlang_sup:start_link().
-
执行以下命令,从模板生成处理程序
make new t=cowboy.http n=hello_handler
查看模版列表
make list-templates
-
编辑 src/hello_handler.erl 文件,并按以下代码修改 init/2 函数
init(Req, State) -> Req = cowboy_req:reply(200, #{<<"content-type">> => <<"text/plain">>}, <<"Hello Erlang!">>, Req), {ok, Req, State}.
-
执行命令,用以构建和启动程序
make run
如果提示需要 make 4以上版本
#安装 GNU make brew install make #然后执行 gmake run
-
在浏览器访问 http://localhost:8080 看到 Hello Erlang 就成功了