用 rebar 来构建、编译、测试、发布 Erlang 应用程序
转自:http://dhq.me/build-compile-eunit-release-erlang-application-with-rebar
rebar 是一个遵循 Erlang/OTP 原则的 Erlang 项目构建工具,使用它可以减少构建标准 Erlang/OTP 项目架构配置的工作量,并且可以很容易的编译、测试、发布 Erlang 应用程序。更强大的是,rebar 提供一种依赖管理机制,它可以使开发者很方便地通过 Git、Hg 等方式重用常见的第三方 Erlang 模块或库。
安装 rebar
你可以从 https://github.com/rebar/rebar/wiki/rebar 下载编译好的版本,也可以自己下载 rebar 的源代码,自己编译一个:
1
2
3
|
git clone git: //github .com /rebar/rebar .git cd rebar . /bootstrap |
上面编译好之后,在当前目录下就会生成一个名为 "rebar" 独立的 erlang 脚本(escript),把它放在你想创建标准 Erlang/OTP 项目的目录路径下即可使用,或者把 rebar 放在系统目录的 Path 下,方便在终端使用:
1
|
sudo mv rebar /usr/local/bin |
在终端输入 "rebar -c" 将列出所有可执行的 rebar 命令。或者输入 "rebar -h" 查看更多的 rebar 参数信息。
用 rebar 构建项目
创建一个名为 rebarapp 的文件夹
1
2
|
mkdir rebarapp cd rebarapp |
创建名为 rebarapp 项目:
1
|
rebar create-app appid=rebarapp |
rebar 会根据默认模板(template)在当前目录下生成一个 src 文件夹,里面包含下面3个文件:
- rebarapp.app.src 应用的资源描述文件,影响后面编译生成的 rebarapp.app 里的内容
- rebarapp_app.erl 应用的 Application Behaviour 代码文件
- rebarapp_sup.erl 应用的 Supervisor Behaviour 代码文件
rebar 还内置了 gen_server、gen_fsm、application 等 Erlang/OTP 行为模式的模板,可以自动生成这些行为模式的框架代码。这里以 gen_server 为例,给应用添加一个名为 rebarapp_server 的 gen_server 行为模式。在应用根目录执行以下命令:
1
|
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 文件内容如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
- 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 的工作进程启动,修改如下:
1
2
3
|
init([]) -> RebarappServer = ?CHILD (rebarapp_server, worker), {ok, { {one_for_one, 5, 10}, [ RebarappServer ]} }. |
编译应用
1
|
rebar compile |
编译完后,会在根目录下生成一个 ebin 的文件夹,里面存放的是该应用的资源文件 rebarapp.app 和应用的 beam 文件,也可以执行以下命令对编译生成的应用文件进行清理:
1
|
rebar clean |
使用 Edoc 生成应用文档
1
|
rebar doc |
命令执行完后,会在根目录生成一个 doc 的文件夹,打开里面的 index.html 就可以很直观地看到该应用的模块 API 概览。
eunit 测试
rebar 会根据一个名为 rebar.config 的文件里的 eunit 配置选项来对应用进行测试,rebar.config 详细地配置选项信息可以查看官方上的 rebar.config.sample。在应用的根目录下创建一个 rebar.config,填入以下内容:
1
2
3
4
5
6
7
8
9
10
|
%%-*- mode: erlang -*- %% Erlang compiler options {erl_opts, [debug_info, {i, "test"}, {src_dirs, ["src"]}]}. {eunit_opts, [verbose, {report, {eunit_surefire, [{dir, "."}]}}]}. {cover_enabled, true}. |
上面的配置将会加载根目录下的 test 文件夹里的文件,所以需要在根目录下创建一个 test 文件夹:
1
|
mkdir -p test |
这里 test 文件夹将存放 eunit 的测试用例,在 test 文件夹里新建一个名为 rebarapp_test.hrl 的测试用例文件,内容如下:
1
2
3
4
5
6
7
8
9
|
-include_lib( "eunit/include/eunit.hrl" ). my_test() -> ?assert (1 + 2 =:= 3). simple_test() -> ok = application:start (rebarapp), ?assertNot (undefined =:= whereis(rebarapp_sup)). |
然后在 rebarapp_server.erl 的文件末尾加上以下测试代码:
1
2
3
|
-ifdef( TEST ). -include( "rebarapp_test.hrl" ). -endif. |
当然,如果有必要的话也可以在每个模块文件上加上面测试代码。执行以下命令进行 eunit 测试:
1
|
rebar compile eunit |
如果应用文件没什么变化修改,也可以直接运行 "rebar eunit"。这时终端出现以下类似显示,则 eunit 测试完成:
1
2
3
4
5
6
7
8
9
10
11
|
==> rebarapp (eunit) ======================== EUnit ======================== module 'rebarapp_app' module 'rebarapp_server' rebarapp_server: my_test...ok rebarapp_server: simple_test...[0.014 s] ok [done in 0.019 s] module 'rebarapp_sup' ======================================================= All 2 tests passed. Cover analysis: /Users/dengjoe/erlang/rebarapp/.eunit/index.html |
可以打开根目录下的.eunit/index.html 查看测试报告。
发布应用
在应用根目录下创建一个名为 rel 的文件夹,用来作为应用发布的文件夹:
1
2
|
mkdir -p rel cd rel |
在当前 rel 文件夹里创建一个名为 rebarapp 的独立的 Erlang VM 节点:
1
|
rebar create-node nodeid=rebarapp |
修 改 rel/reltool.config 里的 lib_dirs 的值,默认是一个空列表 "[]",改为应用所在的目录路径 '["../../"]',不然到后面编译发布时会报 "Missing application directory" 的错误出来,修改后的 reltool.config 配置内容如下所示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
{sys, [ {lib_dirs, ["../../"]}, {erts, [{mod_cond, derived}, {app_file, strip}]}, {app_file, strip}, {rel, "rebarapp", "1", [ kernel, stdlib, sasl, rebarapp ]}, {rel, "start_clean", "", [ kernel, stdlib ]}, {boot_rel, "rebarapp"}, {profile, embedded}, {incl_cond, derived}, {mod_cond, derived}, {excl_archive_filters, [".*"]}, %% Do not archive built libs {excl_sys_filters, ["^bin/.*", "^erts.*/bin/(dialyzer|typer)", "^erts.*/(doc|info|include|lib|man|src)"]}, {excl_app_filters, ["\.gitignore"]}, {app, rebarapp, [{mod_cond, app}, {incl_cond, include}]} ]}. {target_dir, "rebarapp"}. {overlay, [ {mkdir, "log/sasl"}, {copy, "files/erl", "\{\{erts_vsn\}\}/bin/erl"}, {copy, "files/nodetool", "\{\{erts_vsn\}\}/bin/nodetool"}, {copy, "files/rebarapp", "bin/rebarapp"}, {copy, "files/rebarapp.cmd", "bin/rebarapp.cmd"}, {copy, "files/start_erl.cmd", "bin/start_erl.cmd"}, {copy, "files/install_upgrade.escript", "bin/install_upgrade.escript"}, {copy, "files/sys.config", "releases/\{\{rel_vsn\}\}/sys.config"}, {copy, "files/vm.args", "releases/\{\{rel_vsn\}\}/vm.args"} ]}. |
返回应用的根目录,在 rebar.config 加上以下一行,把新建的 rel 文件夹放入到 rebar 可访问的子文件夹里,作为应用内容发布文件夹:
1
|
{sub_dirs, ["rel"]}. |
再重新编译下应用 rebarapp
1
|
rebar compile |
如果报什么错,应用 rebarapp 就可以发布了:
1
|
rebar generate |
在终端上看到 "==> rel (generate)" 且没报什么错,应用 rebarapp 发布成功,并在 rel/rebarapp/bin 目录下生成一个用来启动应用或停止应用等操控动作的 shell 文件 rebarapp。
操控文件 rel/rebarapp/bin/rebarapp 用法:
1
|
rebarapp {start|start_boot |foreground|stop|restart|reboot|ping|console|console_clean|console_boot |attach|remote_console|upgrade} |
例如:
启动应用 rebarapp
1
|
rel /rebarapp/bin/rebarapp start |
停止应用 rebarapp
1
|
rel /rebarapp/bin/rebarapp stop |
或者启动应用 rebarapp 后返回一个 erlang shell 的控制台
1
|
rel /rebarapp/bin/rebarapp console |
OK,在 erlang shell 的控制台上调用 rebarapp_server:hello() 输出一个 "Hello World!" 吧。
用 rebar 来构建、编译、测试、发布 Erlang 应用程序的更多相关文章
- [原]Jenkins(七)---jenkins项目编译测试发布由maven构建的web项目
/** * lihaibo * 文章内容都是根据自己工作情况实践得出. * 版权声明:本博客欢迎转发,但请保留原作者信息! http://www.cnblogs.com/horizonli/p/533 ...
- 在Windows用Rebar来构建,编译,测试,发布Erlang项目
rebar是一个遵循 Erlang/OTP 原则的 Erlang 项目构建工具,使用它可以减少构建标准 Erlang/OTP 项目架构配置的工作量,并且可以很容易的编译.测试.发布 Erlang 应用 ...
- Ubuntu18.04安装Docker并部署(编译、发布、构建镜像)Asp.NetCore项目全过程笔记
环境准备:阿里云Ubuntu18.04 全新安装 一.安装Docker 1.删除旧版本并更新包索引: sudo apt-get remove docker docker-engine dock ...
- Jenkins搭建.NET自动编译测试与发布环境
本文地址: http://blog.csdn.net/wangjia184/article/details/18365553 操作系统Windows, 确保需要的.NET Framework已经安装 ...
- ionic实战系列(一):ionic的开发环境配置和编译、发布
我的ionic实战系列是基于<<Ionic实战>>[美]Jeremy Wilken著-这本书的读书笔记,有诸多借鉴,不详细的地方请参考书籍本身的内容. 1.1技术栈模型 Ion ...
- 使用Bitbucket Pipeline进行.Net Core项目的自动构建、测试和部署
1. 引言 首先,Bitbucket提供支持Mercurial和Git版本控制系统的网络托管服务.简单来说,它类似于GitHub,不同之处在于它支持个人免费创建私有项目仓库.除此之外,Bitbucke ...
- 【.NetCore】基于jenkins以及gitlab的持续编译及发布
前沿 其实本来是想把标题叫做持续集成的,只是后来看看研究出的内容,就只有发布这一个动作,自动化测试等内容也未涉及到,所以改名叫持续编译及发布应该更加贴切吧? 问题背景 其实目前我们传统方式上的发布方式 ...
- 如何利用Azure DevOps快速实现自动化构建、测试、打包及部署
前两天有朋友问我,微软的Azure好用吗,适不适合国人的使用习惯,我就跟他讲了下,Azue很好用,这也是为什么微软云营收一直涨涨涨的原因,基本可以再1个小时内实现自动化构建.打包以及部署到Azure服 ...
- selenium结合docker构建分布式测试环境
selenium是目前web和app自动化测试的主要框架.对于web自动化测试而言,由于selenium2.0以后socker服务器由本地浏览器自己启动且直接通过浏览器原生API操作页面,故越来越多的 ...
随机推荐
- json字符串和对象的相互转化
json在代码中是经常用到的,在此总结一下json字符串和对象及数组之间的相互转化: 1.javascript函数方式: <1> JSON.stringify :把一个对象转换成json字 ...
- String与StringBuild、StringBuffer的区别
String与StringBuild.StringBuffer的区别相信困扰了好多新入门的JAVA程序员,而这也是笔试和面试的一道常见题型,如何全面的回答该问题,变得尤为重要. 首先我们需要清楚一点, ...
- CodeForces 671A Recycling Bottles
暴力. 每个人找到一个入口,也就是从回收站到这个入口走的路程由人的位置到入口的路程来替代. 因此,只要找两个人分别从哪里入口就可以了.注意:有可能只要一个人走,另一人不走. #pragma comme ...
- 5.MyBaits调用存储过程
1.创建一个javaweb项目MyBatis_Part4_Procedure 2.在src下创建procedure.sql文件 --创建表 create table p_user( id number ...
- 建立tcl文件
- ajax实现下拉列表联动
下拉框代码 <fieldset style="margin-bottom:5px;"> <div class="form-group"> ...
- from表单实现无跳转上传文件,接收页面后台数据。
本文基于我刚写的http://www.cnblogs.com/iwang5566/p/6287529.html进行了简单的扩展,实现页面无跳转表单数据提交,并接收后台返回的数据. 下载好,上一篇文章d ...
- 快乐编程学ruby
人们常说:不忘初心,方得始终.所以,code除了完成工作任务,在最初还应该是富于乐趣的,正所谓,宅男配女仆,我们来了解了解我们的ruby 萌妹子吧:-). ...
- Python subprocess + timeout的命令执行
Popen对象 poll() 判断是否执行完毕,执行完毕返回0,未执行完毕返回None terminate() 终止进程发送SIGTERM信号 raise 自定义返回错误 import time im ...
- NIC Bonding: 2 nic port as 1 interface
The following is concluded from here. Consider we have 2 interfaces: eth0 & eth1 bond the two in ...