rebar是一個遵循Erlang/OTP原則的Erlang項目構建工具,使用它能夠減小構建標準Erlang/OTP項目架構配置的工做量,而且能夠很容易的編譯,測試,發佈Erlang應用程序。更強大的是,rebar提供了一種依賴管理機制,它可使開發者很方便的經過Git,Hg等方式重用常見的第三方Erlang模塊或庫。html
安裝node
你能夠從https://github.com/rebar/rebar/wiki/rebar 下載並編譯好的版本,也能夠本身下載rebar的源代碼,本身編譯一個:git
git clone git://github.com/rebar/rebar.git cd rebar ./bootstrap
上面編譯好以後,在當前目錄下就會生成一個名爲 "rebar" 獨立的 erlang 腳本(escript),把它放在你想建立標準 Erlang/OTP 項目的目錄路徑下便可使用。github
在終端輸入 "rebar -c" 將列出全部可執行的 rebar 命令。或者輸入 "rebar -h" 查看更多的 rebar 參數信息。shell
用rebar 建立項目bootstrap
建立一個名爲rebarapp的文件夾架構
mkdir rebarapp cd rebarapp
建立名爲rebarapp項目:app
./rebar create-app appid=rebarapp
rebar 會根據默認模板(template)在當前目錄下生成一個 src 文件夾,裏面包含下面3個文件:框架
rebarapp.app.src 應用的資源描述文件,影響後面編譯生成的 rebarapp.app 裏的內容函數
rebarapp_app.erl 應用的 Application Behaviour 代碼文件
rebarapp_sup.erl 應用的 Supervisor Behaviour 代碼文件
rabar還內置了gen_server,gen_fsm,application等 Erlang/OTP 行爲模式的模板,能夠自動生成這些行爲模式的框架代碼。這裏以 gen_server 爲例,給應用添加一個名爲 rebarapp_server 的 gen_server 行爲模式。在應用根目錄執行如下命令
./rebar create template=simplesrv srvid=rebarapp_server
執行完後自動會在 src 文件夾裏生成一個 rebarapp_server.erl 的 gen_server 框架格式的文件,simplesrv 是 gen_server 模板的名稱(gen_fsm、application對應的是simplefsm、simpleapp),srvid 則是該 gen_server 模板的ID(gen_fsm、application對應的是fsmid、appid)。
爲了測試,這裏對 rebarapp_server.erl 進行修改,export 一個 hello 方法,並添加一個 cast 的消息輸出,修改後的 rebarapp_server.erl 文件內容以下:
-module(rebarapp_server). -behaviour(gen_server). -define(SERVER, ?MODULE). %% ------------------------------------------------------------------ %% API Function Exports %% ------------------------------------------------------------------ -export([start_link/0, hello/0]). %% ------------------------------------------------------------------ %% gen_server Function Exports %% ------------------------------------------------------------------ -export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]). %% ------------------------------------------------------------------ %% API Function Definitions %% ------------------------------------------------------------------ start_link() -> gen_server:start_link({local, ?SERVER}, ?MODULE, [], []). %% @doc just a test hello() -> gen_server:cast(?SERVER, 'HELLO'). %% ------------------------------------------------------------------ %% gen_server Function Definitions %% ------------------------------------------------------------------ init(Args) -> {ok, Args}. handle_call(_Request, _From, State) -> {reply, ok, State}. handle_cast('HELLO', State) -> io:format("Hello World!~n"), {noreply, State}; handle_cast(_Msg, State) -> {noreply, State}. handle_info(_Info, State) -> {noreply, State}. terminate(_Reason, _State) -> ok. code_change(_OldVsn, State, _Extra) -> {ok, State}. %% ------------------------------------------------------------------ %% Internal Function Definitions %% ------------------------------------------------------------------
修改 rebarapp_sup.erl 的 init 函數,把 rebarapp_server 做爲應用管理者 rebarapp_sup 的工做進程啓動,修改以下:
init([]) -> RebarappServer = ?CHILD(rebarapp_server, worker), {ok, { {one_for_one, 5, 10}, [RebarappServer]} }.
編譯應用
./rebar compile
編譯完後,會在根目錄下生成一個 ebin 的文件夾,裏面存放的是該應用的資源文件 rebarapp.app 和應用的 beam 文件,也能夠執行如下命令對編譯生成的應用文件進行清理:
./rebar clean
使用 Edoc 生成應用文檔
./rebar doc
命令執行完後,會在根目錄生成一個 doc 的文件夾,打開裏面的 index.html 就能夠很直觀地看到該應用的模塊 API 概覽。
發佈應用
在應用根目錄下建立一個名爲 rel 的文件夾,用來做爲應用發佈的文件夾:
mkdir -p rel cd rel
在當前 rel 文件夾裏建立一個名爲 rebarapp 的獨立的 Erlang VM 節點:
rebar create-node nodeid=rebarapp
修改 rel/reltool.config 裏的 lib_dirs 的值,默認是一個空列表 "[]",改成應用所在的目錄路徑 '["../../"]',否則到後面編譯發佈時會報 "Missing application directory" 的錯誤出來,修改後的 reltool.config 配置內容以下所示:
返回應用的根目錄,建立rebar.config文件,並在 rebar.config 加上如下一行,把新建的 rel 文件夾放入到 rebar 可訪問的子文件夾裏,做爲應用內容發佈文件夾:
{sub_dirs, ["rel"]}.
再從新編譯下應用 rebarapp:
./rebar compile
若是沒報什麼錯,應用 rebarapp 就能夠發佈了,在rel目錄下:
../rebar generate
在終端上看到 "==> rel (generate)" 且沒報什麼錯,應用 rebarapp 發佈成功,並在 rel/rebarapp/bin 目錄下生成一個用來啓動應用或中止應用等操控動做的 shell 文件 rebarapp。
操控文件 rel/rebarapp/bin/rebarapp 用法:
rebarapp {start|start_boot |foreground|stop|restart|reboot|ping|console|console_clean|console_boot |attach|remote_console|upgrade}
啓動應用 rebarapp
./rel/rebarapp/bin/rebarapp start
中止應用 rebarapp
rel/rebarapp/bin/rebarapp stop
或者啓動應用 rebarapp 後返回一個 erlang shell 的控制檯
rel/rebarapp/bin/rebarapp console
OK,在 erlang shell 的控制檯上調用 rebarapp_server:hello() 輸出一個 "Hello World!" 吧。
參考文章:
http://www.cnblogs.com/panfeng412/archive/2011/08/14/2137990.html
http://dhq.me/build-compile-eunit-release-erlang-application-with-rebar