您的位置:首页 > 其它

rebar 使用测试

2016-05-15 18:18 579 查看
rebar安装及创建项目 rebar作为erlang开发中编译,构建,发布,打包,动态升级的常用工具,下面我记录下rebar工具的安装及使用

从源码安装rebar 

1. 建立文件 install_rebar.sh

2. 拷贝如下shell到 install_rebar.sh

git clone git://github.com/rebar/rebar.git
cd rebar
./bootstrap
sudo cp rebar /usr/local/bin/


3. chmod u+ x install_rebar.sh

4. ./install_rebar.sh 等待安装完成

5. 安装完成启动shell  输入rebar -V 查看是否安装完成。

6 安装正确如下

thinkpad@thinkpad:~$ rebar -V
rebar 2.5.0 17 20140624_142144 git 2.5.0-dirty


7. 使用 rebar -c 查看rebar 常用命令

使用rebar 创建项目

1 .rebar create-app appid=game  创建app sup

==> demo (create-app)
Writing src/game.app.src
Writing src/game_app.erl
Writing src/game_sup.erl


2 . rebar create template=simplesrv srvid=game_server 创建 gen_server 行为模块

==> demo (create)
Writing src/game_server.erl


3. 修改  game_sup 

-module(game_sup).

-behaviour(supervisor).

%% API
-export([start_link/0]).

%% Supervisor callbacks
-export([init/1]).

%% Helper macro for declaring children of supervisor
-define(CHILD(I, Type), {I, {I, start_link, []}, permanent, 5000, Type, [I]}).

%% ===================================================================
%% API functions
%% ===================================================================

start_link() ->
supervisor:start_link({local, ?MODULE}, ?MODULE, []).

%% ===================================================================
%% Supervisor callbacks
%% ===================================================================

init([]) ->
   %% 添加这行 一个完成的 包含监督 监控 重启 退出的项目 就完成
Child = ?CHILD(game_server,worker),
{ok, { {one_for_one, 5, 10}, [Child]} }.


4. 编译 返回到上一级目录也就是src目录,运行:./reabr compile   

==> demo (compile)
Compiled src/game_app.erl
Compiled src/game_server.erl
Compiled src/game_sup.erl
Compiled src/game.erl


 

5.  copy 一个测试用的例子,不一定是从gen_server继承过来的,hello.erl 内容是
///////////

-module(hello).

-export([start/0]).

start() ->

    io:format("Hello world~n").

///////////

修改game.app.src 如下所示:

{application, game,

 [

  {description, ""},

  {vsn, "1"},

  {modules,[game_app,game_sup,hello]}, %这是新增加的一行

  {registered, [hello]},   % hello 也是新增加的

  {applications, [

                  kernel,

                  stdlib

                 ]},

  {mod, { game_app, []}},

  {env, []}

 ]}.

保存。

6  erl -pa ebin 启动erlang shell  在shell 输入 application:start(game).

    运行:appmon:start(). 查看监控树 如下

  



7. 运行这个app,并调用测试我们的程序操作如下:

cd ebin

erl

ls().

%这时进入了ebin 目录的erl 命令行了,再运行:

2> application:start(game).

ok

3> hello:start().

Hello world

ok

好了,这样就可以了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  rebar 使用