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 就成功了