在Erlang In Anger第二章中讲到使用rebar来创建一个Erlang项目(Application或Project)
但美中不足的只是给出了指引,但没有给出详细的步骤。
下面我们就使用rebar一步步来创建一个典型的Application。
但在此之前,最好先理解下Erlang In Anger中第二章节所讲关于application结构的部分:http://zhongwencool.gitbooks.io/erlang_in_anger/
$ git clone git://github.com/rebar/rebar.git $ cd rebar $ ./bootstrap Recompile: src/getopt ... Recompile: src/rebar_utils ==> rebar (compile) Congratulations! You now have a self-contained script called "rebar" in your current working directory. Place this script anywhere in your path and you can use rebar to build OTP-compliant apps.
为了全局使用rebar,把你的路径加到PATH下:在~/.profile
里面加入:
export PATH=$PATH:Your_Rebar_Path
然后重启你的shell或source ~/.profile使它生效
2.创建application:
./rebar create-app appid=autoload ==> rebar (create-app) Writing src/autoload.app.src Writing src/autoload_app.erl Writing src/autoload_sup.erl
生成上面三个模板文件(也可以修改rebar配置自定义模板),
myapp.app.src
- The OTP application specification用于rebar自动生成.app文件放到ebin/下。myapp_app.erl
- An implementation of the OTP Application behaviour:实现你代码逻辑的地方。myapp_sup.erl
- An implementation of the OTP Supervisor behaviour (top level supervisor of the application)此application最高等级的监控树。
{deps, [{inotify, ".*", {git, "https://github.com/sheyll/inotify.git"}}]}.
{env, [{autoload_path,"./dev_patch/"}, {node_cookie,[{test1@localhost,best},{test2@localhost,best}]}, {log,"./log/console.log"}]}.
这样设置后就可以在代码里使用:
{ok,NodeCookies} = application:get_env(autoload,node_cookie).
这样的函数在全局中得到对应的值。
init([]) ->
{ok, { {one_for_one, 5, 10}, [?CHILD(autoload,worker)]} }.
4.3 在autoload.erl gen_server里面写逻辑:
$rebar get-deps
$ rebar compile
erl -pa "./ebin" -pz "./dev_patch" -sname autoload -s autoload -detached
使用下面命令查看日志:
tail ./log/console.log -f