SlideShare une entreprise Scribd logo
1  sur  12
Télécharger pour lire hors ligne
量子数科院                                                                量子统计爱好者的交流学习园地




首页     关于




← “听”得见代码的盲人淘宝店主 —— 一封来自量子工程师的                    Zfor项目的Rebar改造 →
                                                                          新浪微博
内部邮件

                                                                               一淘量子工程师们 北京
Rebar: Erlang构建工具
                                                                                      加关注
发表于 2011 年 04 月 12 日 由 jiuling.ypf


Rebar是一款Erlang的构建工具,使用它可以方便的编译、测试erlang程序、内联驱动和打包Erlang发行
版本。                                                                                         搜索


Rebar是一个独立的erlang脚本,所以使用Rebar发布程序非常简单,甚至可以直接集成在项目文件夹中。               量子新文
                                                                      storm 基础简介
默认的情况下,Rebar会按照Erlang/OTP来组织项目的结构,这样一来,构建时的配置工作量就会大大
                                                                      Twitter storm 性能测试报告与分
减少。Rebar同时提供了依赖库(包)管理机制,方便程序员重用已存在的模块。Rebar的以来管理机制支持                  析
的方式非常多,甚至包括Git, Hg等少见的方式。                                             my sql导入导出数据方法

                                                                     分类目录
这里有一个例子演示怎样将一个已经存在的项目转化为使用rebar来构建。                                   数据之美 (6)
                                                                      未分类 (11)
                                                                      经验分享 (18)
   准备开始
                                                                      量子业务 (4)
   Rebar的命令参数                                                         量子动态 (8)
   构建Rebar                                                            量子技术 (37)

   Rebar和OTP约定
                                                                     友情 链 接
   模板支持                                                               一淘量子工程师微博
   处理发行版本                                                             淘宝搜索技术博客
                                                                      淘宝数据平台与产品官方博客
   扩展Rebar
                                                                     功能
1.准 备 开始                                                              注册
                                                                      登录
                                                                      文章 RSS
第一步:                                                                  评论 RSS
                                                                      WordPress.org
学习使用Rebar的最好的方法是使用一个简单的例子来演示如何用Rebar构建Erlang项目。首先,我们为这
个例子项目创建一个文件夹:


   $ mkdir myapp; cd myapp


然后,下载rebar的二进制文件到这个项目的文件夹。注意:如果在你的PATH中间有已经有rebar了,不会
对我们这个例子有影响。


   $cd .. ;

   $git clone git://github.com/basho/rebar.git;

   $cd rebar;

                                                                           converted by Web2PDFConvert.com
$./bootstrap;

  $cd ../myapp;

  $mv ../rebar/rebar ./



接下来我们使用rebar的模板系统来构建我们程序的“骨架”。


  $ ./rebar create-app appid=myapp


这条指令的结果会产生一个子文件夹“src”,src包含三个文件夹:


  myapp.app.src:OTP应用程序的资源文件
  myapp_app.erl:一个OTP应用程序的Application behaviour
  myapp_sup.erl: 一个OTP应用程序的Supervisor behaviour

现在我们可以使用rebar来编译这个应用程序:


  $ ./rebar compile


执行完成后,会出现一个新的文件夹ebin,在ebin文件夹下,会出现与src文件夹下源文件一一对应的beam
文件。同时,rebar会根据myapp.app.src动态生成一个合适OTP项目资源文件。


编译完成后,如果想清除文件也非常简单:


  $ ./rebar clean


测试 :


Rebar为eunit和common_test两个测试框架都提供了支持,在下面这个例子中,我们使用eunit来为我们
的应用程序写一个测试用例。


打开文件src/myapp_app.erl,在-export()指令之后添加如下代码:


  -ifdef(TEST).

  -include_lib(“eunit/include/eunit.hrl”).

  -endif.



在这个文件的最后添加:


  -ifdef(TEST).

  simple_test() ->

  ok = application:start(myapp),

  ?assertNot(undefined == whereis(myapp_sup)).

  -endif.

                                                             converted by Web2PDFConvert.com
通过使用ifdef保护我们的测试代码,我们可以保证最后的测试代码不会随着编译生成的代码进入ebin文
件夹。下面我们来运行这个单元测试用例:


  $ ./rebar compile eunit


你应该收到类似的输出:


  ==> myapp (compile)

  Compiled src/myapp_app.erl

  Compiled src/myapp_sup.erl

  ==> myapp (eunit)

  Compiled src/myapp_sup.erl

  Compiled src/myapp_app.erl

  Test passed.



注意:rebar这次会编译myapp_app.erl文件两遍,第二遍编译会将输出放到一个特殊的文件夹下(.eunit)下
,生成的文件会包含调试信息和其他有用的测试标记。

如果你想检查我们单元测试的覆盖率,可以非常简单的通过在myapp/rebar.config添加一行:


  {cover_enabled, true}.


然后重新运行我们的测试用例:


  $ ./rebar compile eunit

  ==> myapp (compile)

  ==> myapp (eunit)

  Test passed.

  Cover analysis: /Users/dizzyd/tmp/myapp/.eunit/index.html



详细的覆盖分析会被保存在.eunit/index.html文件里。


2.Rebar的命令参数

Rebar提供了开发中最常用的一些操作,包括:

  编译
  单元测试和覆盖分析
  静态分析(通过Dialyzer和Xref)
  生成文档


                                                               converted by Web2PDFConvert.com
依赖管理


另外,rebar和reltool提供模板机制以方便OTP嵌入式系统利用。


最经常使用的命令:


  命令                描述

  compile           编译项目中所有可用的源文件

  eunit             使用Eunit执行单元测试

  doc               使用Edoc生成文档

  clean             去掉所有生成的文件。包括编译,单元测试等过程生成的文件


较少用的命令(按照字母序):


  命令                      描述

  analyze                 使用Dialyzer执行静态分析

  build_plt               构建Dialyzer PLT; 具体描述请看:Dialyzer documentation

  check_plt               检查Dialyzer PLT 是否是最新,需要的话重新构建

  create                  根据模板创建一个典型的项目

  create-app              根据模板文件priv/templates/simpleapp.template ,创建一个典
                          型的OTP应用

  create-node             Create a prototypical OTP embedded system (described by the
                          priv/templates/simplenode.reltool.config template)

  delete-deps             删除rebar.config 设置的依赖库(包)源文件D

  generate                使用 Reltool 构建一个embedded system

  get-deps                检索rebar.config 文件中配置的依赖的代码

  xref                    使用Xref 分析依赖


支持的源文件的格式:


Rebar可以通过compile指令编译多种格式的源文件。


  源文件                    目 标 文件                         描述

  src/*.erl              ebin/*.beam                    ERlang的源文件

  src/*.app.src          ebin/*.app                     Erlang应用程序的资源文件

  c_src/*.c              priv/<app>.so                  port driver的c语言源代码或
                                                        者NIF共享链接库

  mibs/*.mib             priv/mibs/*.bin                SNMP 的mib 文件

  src/*.xrl              src/*.erl                      Leex 生成的文件

  src/*.yrl              src/*.erl                      Yecc 生成的文件

  asn1/*.asn1            src/*.erl                      ASN-1 文件

  templates/*.dtl        ebin/*_dtl.beam                ErlyDTL模板文件 (需要额外
                                                        安装 ErlyDTL)


                                                                                        converted by Web2PDFConvert.com
src/*.lfe               ebin/*.beam                  LFE 源文件 (需要额外安装LFE)

  src/*.peg               ebin/*.beam                  Neotoma PEG 语法源文件 (需
                                                       要额外安装Neotoma)

  src/*.proto             ebin/*_pb.beam,              Protocol Buffers 参数(需要额
                          include/*_pb.hrl             外安装protobuffs)


选项:


下面列出可以在在rebar.config文件中配置的各种选项。


  命令                      选项 参数                 描述

  compile                  erl_first_files      需要提前编译的erlang源文件(例
                                                如behavior模块)

  compile                  erl_opts             编译器支持的其他选项,详情请见
                                                here

  compile                  mib_first_files      需要提前编译的mib文件列表 (例如,
                                                mib 文件中import部分的引用的RFC
                                                文件

  compile                  src_dirs             列出其他包含erlang源文件的目录

  compile                  erlydtl_opts         更多的支持的选项查阅 ErlyDTL
                                                Templates

  clean                    clean_files          需要在clean步骤删除的文件列表,列
                                                出那些需要clean指令删除的其他模
                                                块的文件

  doc                      edoc_opts            edoc 支持的指令,详见:here

  eunit                    eunit_opts           Eunit支持的指令,详见 here

  eunit                    cover_enabled        开启erlang的覆盖率分析

  eunit                    eunit_compile_opts   Eunit编译时用到的其他的选项

  analyze                  dialyzer_opts        指定Dialyzer PLT 文件

  build_plt                dialyzer_opts        指定Dialyzer PLT 文件

  check_plt                dialyzer_opts        指定 Dialyzer PLT 文件

  get-deps, delete-deps    base_dir             为deps_dir 指定一个候选的目录

  get-deps, delete-deps    deps_dir             制定一个文件夹存储依赖

  get-deps, delete-deps    deps                 依赖的列表

  generate                 target_dir           目标文件夹

  generate                 overlay_vars         Overlay variables file

  xref                     xref_warnings        打开xref的警告

  xref                     xref_checks          Xref模块中analyze/3支持的选项,
                                                具体可以参考: here


3.构建 Rebar

构建rebar



                                                                                 converted by Web2PDFConvert.com
$git clone git://github.com/basho/rebar.git;

   $cd rebar;

   $./bootstrap;

   Recompile: src/rebar_core

   ==> 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的脚本,现在拷贝这个脚本到项目目录,开始rebar之旅。


4.Rebar和 OTP约 定

Rebar按照OTP的约定组织项目,OTP约定可以参考OTP设计原则。应用程序的目录需要下列子文件夹:

  src
  ebin
  priv
  include

应用程序的资源文件(*.app)应该放到ebin文件夹下。除了上面文件夹,rebar还还支持下列的原则:


  test:包含EUnit测试脚本的文件夹
  c_src:包含c语言写的port drivers

5.模板支持

我可以使用我的模板吗?—-是的,只要吧把模板文件(mytemplate.template)放到.rebar/templates下,然
后通过下列命令使用:


   $ rebar create template=mytemplate


6.管理 发 行版本

reltool.config简介:

erlang通过配置文件reltool.config来帮助创建节点,配置文件中包含rebar和reltool(Erlang R13B引入的
发布管理工具)创建节点需要的信息。


创建应用如下:


   $ ./rebar create-app appid=exemplar


注意:create-app和create-node选项能够在rebar_templater.erl中找到,其他的支持的参数都能
在simpleapp.template和simplenode.template中找到。


要创建一个节点,需要手动创建一个rel文件:




                                                                                                  converted by Web2PDFConvert.com
$ mkdir rel

  $ cd rel



创建节点:


  $ ../rebar create-node nodeid=exemplar

  $ ls -lR

  .:

  total 8

  drwxr-xr-x 2 haoze.zpx users 4096 Apr 11 11:08 files

  -rw-r–r– 1 haoze.zpx users 806 Apr 11 11:08 reltool.config

  ./files:

  total 28

  -rw-r–r– 1 haoze.zpx users 334 Apr 11 11:08 app.config

  -rwxr–r– 1 haoze.zpx users 1120 Apr 11 11:08 erl

  -rwxr–r– 1 haoze.zpx users 4370 Apr 11 11:08 exemplar

  -rwxr–r– 1 haoze.zpx users 4819 Apr 11 11:08 nodetool

  -rw-r–r– 1 haoze.zpx users 423 Apr 11 11:08 vm.args



需要在rebar.config中添加:


  {sub_dirs, ["rel"]}.


执行:


  $ ./rebar generate

  ==> rel (generate)



在rel目录中就会生成examplar系统:


  $ ls -l rel/exemplar/

  total 24

  drwxr-xr-x 2 haoze.zpx users 4096 Apr 11 11:09 bin

  drwxr-xr-x 8 haoze.zpx users 4096 Apr 11 11:09 erts-5.8


                                                               converted by Web2PDFConvert.com
drwxr-xr-x 2 haoze.zpx users 4096 Apr 11 11:09 etc

   drwxr-xr-x 18 haoze.zpx users 4096 Apr 11 11:09 lib

   drwxr-xr-x 3 haoze.zpx users 4096 Apr 11 11:09 log

   drwxr-xr-x 3 haoze.zpx users 4096 Apr 11 11:09 releases



7.扩 展 Rebar

(Tips:这章我没有实验过)


在标准发布版本中,rebar能够支持大部分erlang开发者的需要。当你碰到需要扩展rebar的需求时,rebar
提供一种简单的方法帮助加入新功能。


Contexts:

想要知道如何扩展rebar,一个关键的因素就是理解rebar的核心机制—–contexts。Contexts决定在项目目
录结构中选择那些命令选项执行。


对于哪个目录能够执行哪个选项,contexts没有限制,通过配置资源文件rebar.app,例如想要在项目的任
何一个目录都执行any_dir_modules,需要在rebar.app中添加:


   {application, rebar,

   [{description, "Rebar: Erlang Build Tool"},

   %% ...

   {env, [

   %% ...

   %% any_dir processing modules

   {any_dir_modules, [

   %% ...

   rebar_exemplar

   ]},

   %% …

   ]}

   ]}.



Module context:

Module context允许rebar的选项与项目中的文件夹和发布文件夹都关联。



                                                               converted by Web2PDFConvert.com
你可以通过查看rebar.app了解这些,从中学习如何用modules配置参数来使用app_dir和rel_dir。


Example: EDoc command修改 rebar.app:


   {application, rebar,

   [{description, "Rebar: Erlang Build Tool"},

   {vsn, "2"},

   {modules, [ rebar,

   %% ...

   rebar_edoc,

   %% ...

   mustache ]},

   {registered, []},

   {applications, [kernel,

   stdlib,

   sasl]},

   {env, [

   %% ...

   %% Dir specific processing modules

   {modules, [

   {app_dir, [

   %% ...

   rebar_edoc,

   %% ...

   ]},

   {rel_dir, [

   rebar_reltool

   ]}

   ]}

   ]}



                                                             converted by Web2PDFConvert.com
]}.




引入rebar_edoc模块:




   -module(rebar_edoc).

   -export([doc/2, clean/2]).

   -include(“rebar.hrl”).

   %% @doc Generate Erlang program documentation.

   %% @spec doc(#config{}, string()) -> ok

   -spec(doc(Config::#config{}, File::string()) -> ok).

   doc(Config, File) ->

   {ok, AppName, _AppData} = rebar_app_utils:load_app_file(File),

   EDocOpts = rebar_config:get(Config, edoc_opts, []),

   ok = edoc:application(AppName, “.”, EDocOpts),

   ok.

   %% @doc Remove the generated Erlang program documentation.

   %% @spec clean(#config{}, string()) -> ok

   -spec(clean(Config::#config{}, File::string()) -> ok).

   clean(Config, _File) ->

   EDocOpts = rebar_config:get(Config, edoc_opts, []),

   DocDir = proplists:get_value(dir, EDocOpts, “doc”),

   rebar_file_utils:rm_rf(DocDir).




转载文章请注明,转载自:量子数科院[http://www.linezing.com/blog]。文章均为原创,版权归量子统计所有



                   关于  jiuling.ypf
                   Yuan Panfeng @ TAOBAO
                   查看由 jiuling.ypf 发表的所有文章 →




                                                                    converted by Web2PDFConvert.com
此条目发表在 量子技术 分类目录,贴了 Erlang, Rebar 标签。将固定链接加入收藏夹。

 ← “听”得见代码的盲人淘宝店主 —— 一封来自量子工程师的                                                        Zfor项目的Rebar改造 →
 内部邮件



 《 Rebar: Erlang构建工具 》有                           4 条 评论
 Pingback 引用通告: ECAE — Shopex电子商务云的梦想空间 » [Denny] Erlang


 Pingback 引用通告: Mochiweb & ErlyDTL & Rebar | 红树林



           sunjie_s.pt 说:
           2011 年 07 月 29 日下午 3:14


           写的很好,赞一个,收藏了~~~

           回复




           爱 你一杯子 说:
           2011 年 06 月 14 日下午 12:35


           博主的文章写的真好,很受用,先收藏了。

           回复




 发 表 评论

 电子邮件地址不会被公开。 必填项已被标记为 *

 名称 *




 电子邮件 *




 网站




 评论




 您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite>
 <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

  发表评论




量子数科院                                                                                                          自豪地采用
                                                                                                               WordPress。




                                                                                                                  converted by Web2PDFConvert.com
converted by Web2PDFConvert.com

Contenu connexe

En vedette

Nada de Carmen Laforet
Nada de Carmen LaforetNada de Carmen Laforet
Nada de Carmen Laforetoliviaradop
 
Alvido ppt fikrbank_com3
Alvido ppt fikrbank_com3Alvido ppt fikrbank_com3
Alvido ppt fikrbank_com3fikrbank
 
Alvido Zerikarli PowerPoint! (Fikrbank)
Alvido Zerikarli PowerPoint! (Fikrbank)Alvido Zerikarli PowerPoint! (Fikrbank)
Alvido Zerikarli PowerPoint! (Fikrbank)fikrbank
 

En vedette (7)

Nada de Carmen Laforet
Nada de Carmen LaforetNada de Carmen Laforet
Nada de Carmen Laforet
 
Alvido ppt fikrbank_com3
Alvido ppt fikrbank_com3Alvido ppt fikrbank_com3
Alvido ppt fikrbank_com3
 
Test
TestTest
Test
 
Www
WwwWww
Www
 
Alvido Zerikarli PowerPoint! (Fikrbank)
Alvido Zerikarli PowerPoint! (Fikrbank)Alvido Zerikarli PowerPoint! (Fikrbank)
Alvido Zerikarli PowerPoint! (Fikrbank)
 
Fertilization
FertilizationFertilization
Fertilization
 
Special ed handbook
Special ed handbookSpecial ed handbook
Special ed handbook
 

Similaire à rebar erlang

Ruby rails分享
Ruby rails分享Ruby rails分享
Ruby rails分享Cam Song
 
开源应用日志收集系统
开源应用日志收集系统开源应用日志收集系统
开源应用日志收集系统klandor
 
Hello Abap
Hello AbapHello Abap
Hello Abaperick du
 
互联网创业服务器运维工具集
互联网创业服务器运维工具集互联网创业服务器运维工具集
互联网创业服务器运维工具集zhen chen
 
Google protocol buffers简析
Google protocol buffers简析Google protocol buffers简析
Google protocol buffers简析wavefly
 
4. Go 工程化实践-0124-v2.pdf
4. Go 工程化实践-0124-v2.pdf4. Go 工程化实践-0124-v2.pdf
4. Go 工程化实践-0124-v2.pdfssuserd6c7621
 
Subversion
SubversionSubversion
Subversioni7Xh
 
Web testing automation
Web testing automationWeb testing automation
Web testing automationkuozui
 
Erlang Practice
Erlang PracticeErlang Practice
Erlang Practicelitaocheng
 
从无阻塞并行脚本加载(Lab.js)到浏览器消息模型
从无阻塞并行脚本加载(Lab.js)到浏览器消息模型从无阻塞并行脚本加载(Lab.js)到浏览器消息模型
从无阻塞并行脚本加载(Lab.js)到浏览器消息模型Jackson Tian
 
轻量级Flash服务器开发框架(刘恒)
轻量级Flash服务器开发框架(刘恒)轻量级Flash服务器开发框架(刘恒)
轻量级Flash服务器开发框架(刘恒)FLASH开发者交流会
 
基于Lucene的站内搜索 Beta
基于Lucene的站内搜索 Beta基于Lucene的站内搜索 Beta
基于Lucene的站内搜索 Betazhu02
 
基于Lucene的站内搜索 Beta
基于Lucene的站内搜索 Beta基于Lucene的站内搜索 Beta
基于Lucene的站内搜索 Betafulin tang
 
An introduce to n hibernate (part 1) pub
An introduce to n hibernate (part 1) pubAn introduce to n hibernate (part 1) pub
An introduce to n hibernate (part 1) pubjiangxu
 
CodeIgniter 2.0.X
CodeIgniter 2.0.XCodeIgniter 2.0.X
CodeIgniter 2.0.XBo-Yi Wu
 
基于Ivy ant的java构建初探
基于Ivy ant的java构建初探基于Ivy ant的java构建初探
基于Ivy ant的java构建初探Anson Yang
 
GNU Autoconf / Automake #4
GNU Autoconf / Automake #4GNU Autoconf / Automake #4
GNU Autoconf / Automake #4imacat .
 
Ceph Day Shanghai - Ceph in Chinau Unicom Labs
Ceph Day Shanghai - Ceph in Chinau Unicom LabsCeph Day Shanghai - Ceph in Chinau Unicom Labs
Ceph Day Shanghai - Ceph in Chinau Unicom LabsCeph Community
 

Similaire à rebar erlang (20)

Ruby rails分享
Ruby rails分享Ruby rails分享
Ruby rails分享
 
开源应用日志收集系统
开源应用日志收集系统开源应用日志收集系统
开源应用日志收集系统
 
Hello Abap
Hello AbapHello Abap
Hello Abap
 
1~60
1~601~60
1~60
 
互联网创业服务器运维工具集
互联网创业服务器运维工具集互联网创业服务器运维工具集
互联网创业服务器运维工具集
 
Google protocol buffers简析
Google protocol buffers简析Google protocol buffers简析
Google protocol buffers简析
 
4. Go 工程化实践-0124-v2.pdf
4. Go 工程化实践-0124-v2.pdf4. Go 工程化实践-0124-v2.pdf
4. Go 工程化实践-0124-v2.pdf
 
Subversion
SubversionSubversion
Subversion
 
Web testing automation
Web testing automationWeb testing automation
Web testing automation
 
Erlang Practice
Erlang PracticeErlang Practice
Erlang Practice
 
从无阻塞并行脚本加载(Lab.js)到浏览器消息模型
从无阻塞并行脚本加载(Lab.js)到浏览器消息模型从无阻塞并行脚本加载(Lab.js)到浏览器消息模型
从无阻塞并行脚本加载(Lab.js)到浏览器消息模型
 
轻量级Flash服务器开发框架(刘恒)
轻量级Flash服务器开发框架(刘恒)轻量级Flash服务器开发框架(刘恒)
轻量级Flash服务器开发框架(刘恒)
 
基于Lucene的站内搜索 Beta
基于Lucene的站内搜索 Beta基于Lucene的站内搜索 Beta
基于Lucene的站内搜索 Beta
 
基于Lucene的站内搜索 Beta
基于Lucene的站内搜索 Beta基于Lucene的站内搜索 Beta
基于Lucene的站内搜索 Beta
 
An introduce to n hibernate (part 1) pub
An introduce to n hibernate (part 1) pubAn introduce to n hibernate (part 1) pub
An introduce to n hibernate (part 1) pub
 
CodeIgniter 2.0.X
CodeIgniter 2.0.XCodeIgniter 2.0.X
CodeIgniter 2.0.X
 
基于Ivy ant的java构建初探
基于Ivy ant的java构建初探基于Ivy ant的java构建初探
基于Ivy ant的java构建初探
 
GNU Autoconf / Automake #4
GNU Autoconf / Automake #4GNU Autoconf / Automake #4
GNU Autoconf / Automake #4
 
Ceph Day Shanghai - Ceph in Chinau Unicom Labs
Ceph Day Shanghai - Ceph in Chinau Unicom LabsCeph Day Shanghai - Ceph in Chinau Unicom Labs
Ceph Day Shanghai - Ceph in Chinau Unicom Labs
 
Demo review
Demo reviewDemo review
Demo review
 

rebar erlang

  • 1. 量子数科院 量子统计爱好者的交流学习园地 首页 关于 ← “听”得见代码的盲人淘宝店主 —— 一封来自量子工程师的 Zfor项目的Rebar改造 → 新浪微博 内部邮件 一淘量子工程师们 北京 Rebar: Erlang构建工具 加关注 发表于 2011 年 04 月 12 日 由 jiuling.ypf Rebar是一款Erlang的构建工具,使用它可以方便的编译、测试erlang程序、内联驱动和打包Erlang发行 版本。 搜索 Rebar是一个独立的erlang脚本,所以使用Rebar发布程序非常简单,甚至可以直接集成在项目文件夹中。 量子新文 storm 基础简介 默认的情况下,Rebar会按照Erlang/OTP来组织项目的结构,这样一来,构建时的配置工作量就会大大 Twitter storm 性能测试报告与分 减少。Rebar同时提供了依赖库(包)管理机制,方便程序员重用已存在的模块。Rebar的以来管理机制支持 析 的方式非常多,甚至包括Git, Hg等少见的方式。 my sql导入导出数据方法 分类目录 这里有一个例子演示怎样将一个已经存在的项目转化为使用rebar来构建。 数据之美 (6) 未分类 (11) 经验分享 (18) 准备开始 量子业务 (4) Rebar的命令参数 量子动态 (8) 构建Rebar 量子技术 (37) Rebar和OTP约定 友情 链 接 模板支持 一淘量子工程师微博 处理发行版本 淘宝搜索技术博客 淘宝数据平台与产品官方博客 扩展Rebar 功能 1.准 备 开始 注册 登录 文章 RSS 第一步: 评论 RSS WordPress.org 学习使用Rebar的最好的方法是使用一个简单的例子来演示如何用Rebar构建Erlang项目。首先,我们为这 个例子项目创建一个文件夹: $ mkdir myapp; cd myapp 然后,下载rebar的二进制文件到这个项目的文件夹。注意:如果在你的PATH中间有已经有rebar了,不会 对我们这个例子有影响。 $cd .. ; $git clone git://github.com/basho/rebar.git; $cd rebar; converted by Web2PDFConvert.com
  • 2. $./bootstrap; $cd ../myapp; $mv ../rebar/rebar ./ 接下来我们使用rebar的模板系统来构建我们程序的“骨架”。 $ ./rebar create-app appid=myapp 这条指令的结果会产生一个子文件夹“src”,src包含三个文件夹: myapp.app.src:OTP应用程序的资源文件 myapp_app.erl:一个OTP应用程序的Application behaviour myapp_sup.erl: 一个OTP应用程序的Supervisor behaviour 现在我们可以使用rebar来编译这个应用程序: $ ./rebar compile 执行完成后,会出现一个新的文件夹ebin,在ebin文件夹下,会出现与src文件夹下源文件一一对应的beam 文件。同时,rebar会根据myapp.app.src动态生成一个合适OTP项目资源文件。 编译完成后,如果想清除文件也非常简单: $ ./rebar clean 测试 : Rebar为eunit和common_test两个测试框架都提供了支持,在下面这个例子中,我们使用eunit来为我们 的应用程序写一个测试用例。 打开文件src/myapp_app.erl,在-export()指令之后添加如下代码: -ifdef(TEST). -include_lib(“eunit/include/eunit.hrl”). -endif. 在这个文件的最后添加: -ifdef(TEST). simple_test() -> ok = application:start(myapp), ?assertNot(undefined == whereis(myapp_sup)). -endif. converted by Web2PDFConvert.com
  • 3. 通过使用ifdef保护我们的测试代码,我们可以保证最后的测试代码不会随着编译生成的代码进入ebin文 件夹。下面我们来运行这个单元测试用例: $ ./rebar compile eunit 你应该收到类似的输出: ==> myapp (compile) Compiled src/myapp_app.erl Compiled src/myapp_sup.erl ==> myapp (eunit) Compiled src/myapp_sup.erl Compiled src/myapp_app.erl Test passed. 注意:rebar这次会编译myapp_app.erl文件两遍,第二遍编译会将输出放到一个特殊的文件夹下(.eunit)下 ,生成的文件会包含调试信息和其他有用的测试标记。 如果你想检查我们单元测试的覆盖率,可以非常简单的通过在myapp/rebar.config添加一行: {cover_enabled, true}. 然后重新运行我们的测试用例: $ ./rebar compile eunit ==> myapp (compile) ==> myapp (eunit) Test passed. Cover analysis: /Users/dizzyd/tmp/myapp/.eunit/index.html 详细的覆盖分析会被保存在.eunit/index.html文件里。 2.Rebar的命令参数 Rebar提供了开发中最常用的一些操作,包括: 编译 单元测试和覆盖分析 静态分析(通过Dialyzer和Xref) 生成文档 converted by Web2PDFConvert.com
  • 4. 依赖管理 另外,rebar和reltool提供模板机制以方便OTP嵌入式系统利用。 最经常使用的命令: 命令 描述 compile 编译项目中所有可用的源文件 eunit 使用Eunit执行单元测试 doc 使用Edoc生成文档 clean 去掉所有生成的文件。包括编译,单元测试等过程生成的文件 较少用的命令(按照字母序): 命令 描述 analyze 使用Dialyzer执行静态分析 build_plt 构建Dialyzer PLT; 具体描述请看:Dialyzer documentation check_plt 检查Dialyzer PLT 是否是最新,需要的话重新构建 create 根据模板创建一个典型的项目 create-app 根据模板文件priv/templates/simpleapp.template ,创建一个典 型的OTP应用 create-node Create a prototypical OTP embedded system (described by the priv/templates/simplenode.reltool.config template) delete-deps 删除rebar.config 设置的依赖库(包)源文件D generate 使用 Reltool 构建一个embedded system get-deps 检索rebar.config 文件中配置的依赖的代码 xref 使用Xref 分析依赖 支持的源文件的格式: Rebar可以通过compile指令编译多种格式的源文件。 源文件 目 标 文件 描述 src/*.erl ebin/*.beam ERlang的源文件 src/*.app.src ebin/*.app Erlang应用程序的资源文件 c_src/*.c priv/<app>.so port driver的c语言源代码或 者NIF共享链接库 mibs/*.mib priv/mibs/*.bin SNMP 的mib 文件 src/*.xrl src/*.erl Leex 生成的文件 src/*.yrl src/*.erl Yecc 生成的文件 asn1/*.asn1 src/*.erl ASN-1 文件 templates/*.dtl ebin/*_dtl.beam ErlyDTL模板文件 (需要额外 安装 ErlyDTL) converted by Web2PDFConvert.com
  • 5. src/*.lfe ebin/*.beam LFE 源文件 (需要额外安装LFE) src/*.peg ebin/*.beam Neotoma PEG 语法源文件 (需 要额外安装Neotoma) src/*.proto ebin/*_pb.beam, Protocol Buffers 参数(需要额 include/*_pb.hrl 外安装protobuffs) 选项: 下面列出可以在在rebar.config文件中配置的各种选项。 命令 选项 参数 描述 compile erl_first_files 需要提前编译的erlang源文件(例 如behavior模块) compile erl_opts 编译器支持的其他选项,详情请见 here compile mib_first_files 需要提前编译的mib文件列表 (例如, mib 文件中import部分的引用的RFC 文件 compile src_dirs 列出其他包含erlang源文件的目录 compile erlydtl_opts 更多的支持的选项查阅 ErlyDTL Templates clean clean_files 需要在clean步骤删除的文件列表,列 出那些需要clean指令删除的其他模 块的文件 doc edoc_opts edoc 支持的指令,详见:here eunit eunit_opts Eunit支持的指令,详见 here eunit cover_enabled 开启erlang的覆盖率分析 eunit eunit_compile_opts Eunit编译时用到的其他的选项 analyze dialyzer_opts 指定Dialyzer PLT 文件 build_plt dialyzer_opts 指定Dialyzer PLT 文件 check_plt dialyzer_opts 指定 Dialyzer PLT 文件 get-deps, delete-deps base_dir 为deps_dir 指定一个候选的目录 get-deps, delete-deps deps_dir 制定一个文件夹存储依赖 get-deps, delete-deps deps 依赖的列表 generate target_dir 目标文件夹 generate overlay_vars Overlay variables file xref xref_warnings 打开xref的警告 xref xref_checks Xref模块中analyze/3支持的选项, 具体可以参考: here 3.构建 Rebar 构建rebar converted by Web2PDFConvert.com
  • 6. $git clone git://github.com/basho/rebar.git; $cd rebar; $./bootstrap; Recompile: src/rebar_core ==> 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的脚本,现在拷贝这个脚本到项目目录,开始rebar之旅。 4.Rebar和 OTP约 定 Rebar按照OTP的约定组织项目,OTP约定可以参考OTP设计原则。应用程序的目录需要下列子文件夹: src ebin priv include 应用程序的资源文件(*.app)应该放到ebin文件夹下。除了上面文件夹,rebar还还支持下列的原则: test:包含EUnit测试脚本的文件夹 c_src:包含c语言写的port drivers 5.模板支持 我可以使用我的模板吗?—-是的,只要吧把模板文件(mytemplate.template)放到.rebar/templates下,然 后通过下列命令使用: $ rebar create template=mytemplate 6.管理 发 行版本 reltool.config简介: erlang通过配置文件reltool.config来帮助创建节点,配置文件中包含rebar和reltool(Erlang R13B引入的 发布管理工具)创建节点需要的信息。 创建应用如下: $ ./rebar create-app appid=exemplar 注意:create-app和create-node选项能够在rebar_templater.erl中找到,其他的支持的参数都能 在simpleapp.template和simplenode.template中找到。 要创建一个节点,需要手动创建一个rel文件: converted by Web2PDFConvert.com
  • 7. $ mkdir rel $ cd rel 创建节点: $ ../rebar create-node nodeid=exemplar $ ls -lR .: total 8 drwxr-xr-x 2 haoze.zpx users 4096 Apr 11 11:08 files -rw-r–r– 1 haoze.zpx users 806 Apr 11 11:08 reltool.config ./files: total 28 -rw-r–r– 1 haoze.zpx users 334 Apr 11 11:08 app.config -rwxr–r– 1 haoze.zpx users 1120 Apr 11 11:08 erl -rwxr–r– 1 haoze.zpx users 4370 Apr 11 11:08 exemplar -rwxr–r– 1 haoze.zpx users 4819 Apr 11 11:08 nodetool -rw-r–r– 1 haoze.zpx users 423 Apr 11 11:08 vm.args 需要在rebar.config中添加: {sub_dirs, ["rel"]}. 执行: $ ./rebar generate ==> rel (generate) 在rel目录中就会生成examplar系统: $ ls -l rel/exemplar/ total 24 drwxr-xr-x 2 haoze.zpx users 4096 Apr 11 11:09 bin drwxr-xr-x 8 haoze.zpx users 4096 Apr 11 11:09 erts-5.8 converted by Web2PDFConvert.com
  • 8. drwxr-xr-x 2 haoze.zpx users 4096 Apr 11 11:09 etc drwxr-xr-x 18 haoze.zpx users 4096 Apr 11 11:09 lib drwxr-xr-x 3 haoze.zpx users 4096 Apr 11 11:09 log drwxr-xr-x 3 haoze.zpx users 4096 Apr 11 11:09 releases 7.扩 展 Rebar (Tips:这章我没有实验过) 在标准发布版本中,rebar能够支持大部分erlang开发者的需要。当你碰到需要扩展rebar的需求时,rebar 提供一种简单的方法帮助加入新功能。 Contexts: 想要知道如何扩展rebar,一个关键的因素就是理解rebar的核心机制—–contexts。Contexts决定在项目目 录结构中选择那些命令选项执行。 对于哪个目录能够执行哪个选项,contexts没有限制,通过配置资源文件rebar.app,例如想要在项目的任 何一个目录都执行any_dir_modules,需要在rebar.app中添加: {application, rebar, [{description, "Rebar: Erlang Build Tool"}, %% ... {env, [ %% ... %% any_dir processing modules {any_dir_modules, [ %% ... rebar_exemplar ]}, %% … ]} ]}. Module context: Module context允许rebar的选项与项目中的文件夹和发布文件夹都关联。 converted by Web2PDFConvert.com
  • 9. 你可以通过查看rebar.app了解这些,从中学习如何用modules配置参数来使用app_dir和rel_dir。 Example: EDoc command修改 rebar.app: {application, rebar, [{description, "Rebar: Erlang Build Tool"}, {vsn, "2"}, {modules, [ rebar, %% ... rebar_edoc, %% ... mustache ]}, {registered, []}, {applications, [kernel, stdlib, sasl]}, {env, [ %% ... %% Dir specific processing modules {modules, [ {app_dir, [ %% ... rebar_edoc, %% ... ]}, {rel_dir, [ rebar_reltool ]} ]} ]} converted by Web2PDFConvert.com
  • 10. ]}. 引入rebar_edoc模块: -module(rebar_edoc). -export([doc/2, clean/2]). -include(“rebar.hrl”). %% @doc Generate Erlang program documentation. %% @spec doc(#config{}, string()) -> ok -spec(doc(Config::#config{}, File::string()) -> ok). doc(Config, File) -> {ok, AppName, _AppData} = rebar_app_utils:load_app_file(File), EDocOpts = rebar_config:get(Config, edoc_opts, []), ok = edoc:application(AppName, “.”, EDocOpts), ok. %% @doc Remove the generated Erlang program documentation. %% @spec clean(#config{}, string()) -> ok -spec(clean(Config::#config{}, File::string()) -> ok). clean(Config, _File) -> EDocOpts = rebar_config:get(Config, edoc_opts, []), DocDir = proplists:get_value(dir, EDocOpts, “doc”), rebar_file_utils:rm_rf(DocDir). 转载文章请注明,转载自:量子数科院[http://www.linezing.com/blog]。文章均为原创,版权归量子统计所有 关于 jiuling.ypf Yuan Panfeng @ TAOBAO 查看由 jiuling.ypf 发表的所有文章 → converted by Web2PDFConvert.com
  • 11. 此条目发表在 量子技术 分类目录,贴了 Erlang, Rebar 标签。将固定链接加入收藏夹。 ← “听”得见代码的盲人淘宝店主 —— 一封来自量子工程师的 Zfor项目的Rebar改造 → 内部邮件 《 Rebar: Erlang构建工具 》有 4 条 评论 Pingback 引用通告: ECAE — Shopex电子商务云的梦想空间 » [Denny] Erlang Pingback 引用通告: Mochiweb & ErlyDTL & Rebar | 红树林 sunjie_s.pt 说: 2011 年 07 月 29 日下午 3:14 写的很好,赞一个,收藏了~~~ 回复 爱 你一杯子 说: 2011 年 06 月 14 日下午 12:35 博主的文章写的真好,很受用,先收藏了。 回复 发 表 评论 电子邮件地址不会被公开。 必填项已被标记为 * 名称 * 电子邮件 * 网站 评论 您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> 发表评论 量子数科院 自豪地采用 WordPress。 converted by Web2PDFConvert.com