一開始自己寫application的時候要手動建立好幾個模組,最近發現了一個好東西basho-rebar,用rebar建立application可以自動產生三個檔案:
lqg_app.erl
lqg_sup.erl
lqg.app.src
然後,再添加個人需要的邏輯業務,一個application很快就出爐了。。。。。。
具體做法: ======這裡用了basho-lager(erlang的日誌應用)來做執行個體
1.建立應用檔案夾(很簡單的一步)
mkdir lapp
2.進入建立好的應用檔案夾,下載 rebar 執行檔案到此處
cd lapp/wget http://cloud.github.com/downloads/basho/rebar/rebar && chmod u+x rebar
3.使用rebar建立application
./rebar create-app appid=lager
這是能看到 在路徑 lapp/src/下產生了三個檔案
lqg@lqg:~/lapp$ ./rebar create-app appid=lager==> lapp (create-app)Writing src/lager.app.srcWriting src/lager_app.erlWriting src/lager_sup.erl
4。由於lager有依賴的應用,所以要把以來解決,這裡可以通過 rebar的設定檔去把依賴自動下載補全,當然前提是寫好了設定檔 rebar.config
在這裡,rebar.config的內容如下:
{erl_opts, [debug_info]}.{deps, [ {lager_amqp_backend, ".*", {git, "https://github.com/jbrisbin/lager_amqp_backend.git", "master"}}, {amqp_client, ".*", {git, "https://github.com/jbrisbin/amqp_client.git", {tag,"rabbitmq_2.7.0"}}}]}.
接著,執行 ./rebar get-deps 把依賴下載回來,路徑是 lapp/deps/*
lqg@lqg:~/lapp$ ./rebar get-deps==> lapp (get-deps)Pulling lager_amqp_backend from {git,"https://github.com/jbrisbin/lager_amqp_backend.git", "master"}Cloning into 'lager_amqp_backend'...Pulling amqp_client from {git,"https://github.com/jbrisbin/amqp_client.git", {tag,"rabbitmq_2.7.0"}}Cloning into 'amqp_client'...==> Entering directory `/home/lqg/lapp/deps/lager_amqp_backend'==> Entering directory `/home/lqg/lapp/deps/amqp_client'==> amqp_client (get-deps)Pulling rabbit_common from {git,"git://github.com/jbrisbin/rabbit_common.git", {tag,"rabbitmq_2.7.0"}}Cloning into 'rabbit_common'...==> Entering directory `/home/lqg/lapp/deps/rabbit_common'==> rabbit_common (get-deps)==> Leaving directory `/home/lqg/lapp/deps/rabbit_common'==> Leaving directory `/home/lqg/lapp/deps/amqp_client'==> lager_amqp_backend (get-deps)Pulling lager from {git,"https://github.com/basho/lager.git",{tag,"0.9.4"}}Cloning into 'lager'...==> Entering directory `/home/lqg/lapp/deps/lager'==> lager (get-deps)==> Leaving directory `/home/lqg/lapp/deps/lager'==> Leaving directory `/home/lqg/lapp/deps/lager_amqp_backend'
5.這時候基本完成了,差個測試代碼,隨便寫個 test.erl
-module(test).-compile([{parse_transform, lager_transform}]). %%lager的編譯要求-export([t/0]).t()-> lager:info("lqg").
6.都準備好之後就要編譯了,這裡編譯不使用不需要makefile了,直接用rebar來執行 ./rebar compile
7.啟動 erl 進行測試
erl -sname lqg -pa ebin/ -pa deps/*/ebin/
添加啟動命令參數,把依賴一併包括進來。。。
lqg@lqg:~/lapp$ erl -sname lqg -pa ebin/ -pa deps/*/ebin/Erlang R15B01 (erts-5.9.1) [source] [64-bit] [smp:2:2] [async-threads:0] [hipe] [kernel-poll:false]Eshell V5.9.1 (abort with ^G) (lqg@lqg)1> application:start(compiler). %%此處是lager的依賴應用,要先啟動,ok(lqg@lqg)2> application:start(syntax_tools).%%同上ok(lqg@lqg)3> application:start(lager). ok16:39:31.020 [info] Application lager started on node lqg@lqg(lqg@lqg)4> test:t().16:39:39.491 [info] lqg %%日誌輸出ok(lqg@lqg)5>
在回頭看看,現在發布一個應用,很容易了=。=
這裡順便說下lager的一些特點
1.內部定義了6個日誌等級 debug<info<notice<warning<error<critcal<alert<emergency
2.支援動態設定日誌輸出等級 lager:set_loglevel/2,set_loglevel/3
3.支援日誌匯總 lager_amqp_backend