erlang應用發佈

http://blog.csdn.net/zhangxinrun/article/details/6993892php

參考「轉載1」和「轉載2」就能夠了,但須要注意如下兩點:

1.若是用rebar - create-app appid=abc 建立 「應用abc」,當前的目錄名必須是abc,舉例:在abc目錄下執行rebar - create-app appid=abc,不然在應用發佈的配置文件reltool.config中,配置lib_dirs, ["../../"]項時,找不到應用abc。
2.用rebar生成的abc應用的erlang源碼文件爲:abc_app.erl abc_sup.erl,若是須要添加新的erlang源代碼,新的源代碼文件的命名必須是"abc_"開頭,例如:abc_server.erl ,不然在執行發佈命令rebar generate時,不能將新添加的erlang源代碼編譯成功的abc_server.beam文件打包進「abc-1.ez」發佈包。


轉載1:http://www.linezing.com/blog/?p=351

Zfor項目的Rebar改造
html

本文檔用於描述如何使用rebar erlang構建工具,將github上的zfor項目(https://github.com/chaoslawful/zfor)進行改造。
關於改造過程,咱們約定:首先創建一個公共的工做目錄(本文檔中假定爲work目錄),做爲zfor項目改造的主目錄,改造過程當中的全部操做均在此目錄下完成。java

1.    準備工做

1.1 下載zfor

1.       首先,在公共目錄(work目錄)下使用git命令下載zfor:node

cd workgit clone https://github.com/chaoslawful/zfor.git

2.       而後,進入zfor目錄,切換到zfor 1.0.10舊版本(目前,github上的master版本是已經rebar改造後的版本,所以咱們須要從新下載1.0.10歷史版本):python

cd zforgit checkout 1.0.10

3.       Zfor項目的原始項目目錄格式以下:git

${PROJECT}├── bin├── cfg├── Changelog

 

├── COPYINGgithub

├── docbootstrap

├── examplesubuntu

├── Makefilevim

├── pkg

├── README

├── src

└── THANKS

└── TODO

1.2 下載rebar

1.       首先,在公共目錄(work目錄)下使用git命令下載rebar:

cd workgit clone https://github.com/basho/rebar.git

2.       而後,進入rebar目錄,編譯rebar:

cd rebar./bootstrap

3.       編譯後在該目錄下生成rebar二進制執行文件:

1.3 項目規範

Rebar按照OTP設計原則組織項目,在此基礎上,zfor項目的rebar改造過程遵循如下項目規範:

${PROJECT}├── c_src├── doc├── ebin

 

├── include

├── priv

├── src

└── test

其中:

  • c_src/ – 保存 Port Driver 相關的 C 代碼
  • doc/ – 保存文檔文件
  • ebin/ – 保存 Erlang 目標文件(即 *.beam),OTP 應用的描述文件 *.app 也放置在此
  • include/ – 保存 Erlang 頭文件(即 *.hrl)
  • priv/ – 保存應用特有的文件,例如獨立的 C 應用代碼、腳本文件、rpm spec 等
  • src/ – 保存 Erlang 源代碼文件(即 *.erl)
  • test/ – 保存項目相關的測試文件及數據,測試自己能夠是 EUnit 風格或 common-test 風格的

2.    改造過程

2.1 建立rebar項目

首先,在公共目錄(work目錄)下,建立zfor的rebar改造項目目錄:zfor_rebar,將1.2節生成的rebar二進制執行文件拷貝到該目錄,並在該目錄下建立rebar項目:

$ mkdir zfor_rebar$ cd zfor_rebar$ cp ../rebar/rebar ./$ ./rebar create-app appid=zfor

 

==> zfor_rebar (create-app)

Writing src/zfor.app.src

Writing src/zfor_app.erl

Writing src/zfor_sup.erl

2.2 建立必要的子目錄

在zfor_rebar目錄下,按照1.3節中的項目規範,創建必要的項目子目錄:

$ cd zfor_rebar$ mkdir c_src$ mkdir doc$ mkdir ebin

 

$ mkdir include

$ mkdir priv

$ mkdir test

備註:src目錄已經在建立rebar項目時自動生成。

2.3 遷移zfor項目文件

按照1.3節中的項目規範,進行zfor項目文件的遷移:

1.       c_src部分:

暫無。

2.       doc部分:

$ cd zfor$ mv ./doc/* ../zfor_rebar/doc/

3.       ebin部分:

暫無。

4.       include部分:

$ cd zfor$ cd src/zfor/$ mv ./include/* ../../../zfor_rebar/include/

5.       priv部分:

$ cd zfor$ cd src$ mv ./erlang_zfor/ ../../zfor_rebar/priv/$ mv ./java_zfor/ ../../zfor_rebar/priv/

 

$ mv ./libzfor/ ../../zfor_rebar/priv/

$ mv ./php_zfor/ ../../zfor_rebar/priv/

$ mv ./python_zfor/ ../../zfor_rebar/priv/

$ cd ..

$ mv ./bin ./scripts

$ mv ./scripts/ ../zfor_rebar/priv/

$ mv ./cfg/ ../zfor_rebar/priv/

$ mv ./examples/ ../zfor_rebar/priv/

$ mv ./pkg/ ../zfor_rebar/priv/

6.       src部分:

$ cd zfor$ cd src/zfor/$ mv ./src/* ../../../zfor_rebar/src/

7.       test部分:

$ cd zfor$ cd src/zfor/$ mv ./test/* ../../../zfor_rebar/test/

8.       其餘部分

$ cd zfor$ mv Changelog ../zfor_rebar/$ mv COPYING ../zfor_rebar/$ mv README ../zfor_rebar/

 

$ mv THANKS ../zfor_rebar/

$ mv TODO ../zfor_rebar/

至此,已經將zfor項目遷移到了zfor_rebar目錄下,最終的目錄結構以下:

2.4 修改zfor.app.in配置文件

1. 進入zfor_rebar目錄下的src目錄,根據項目名,將zfor.app.in重命名爲zfor_rebar.app.src:

$ cd zfor_rebar/src$ mv zfor.app.in zfor.app.src

2. 編輯zfor.app.src,將「{vsn, 「%VSN%」}」改成「{vsn, 「1.0.5″}」,將「{conf_path, 「%CONFPATH%」}」改成「{conf_path, 「/usr/local/etc/zfor」}」。

$ cd zfor_rebar/src$ vim zfor.app.src

2.5 編譯項目

使用rebar compile命令編譯zfor:

$ cd zfor_rebar$ ./rebar compile

 

==> zfor_rebar (compile)
Compiled src/zfor_app.erl
src/zfor_hostlist.erl:80: Warning: http:request/4 is deprecated and will be removed in R15B; use httpc:request/4
Compiled src/zfor_hostlist.erl
Compiled src/zfor_sup.erl
Compiled src/zfor_caretaker.erl
src/zfor_server.erl:50: Warning: NOT OPTIMIZED: called function handle_req/3 does not begin with a suitable binary matching instruction
src/zfor_server.erl:99: Warning: NOT OPTIMIZED: sub binary used by erlang:binary_to_list/1
Compiled src/zfor_server.erl
Compiled src/zfor_httpclient.erl
Compiled src/zfor_util.erl
Compiled src/zfor_main.erl
src/zfor_config.erl:240: Warning: http:request/4 is deprecated and will be removed in R15B; use httpc:request/4
src/zfor_config.erl:461: Warning: http:request/4 is deprecated and will be removed in R15B; use httpc:request/4
Compiled src/zfor_config.erl

2.6 發佈項目

1. 在項目根目錄下,建立rel發佈目錄,而後建立一個node用於發佈:
$ mkdir rel
$ cd rel
$ nodeid=zfor../rebar create-node
==()> relcreate-node
==> rel (create-node)
Writing reltool.config
Writing files/erl
Writing files/nodetool
Writing files/zfor
Writing files/app.config
Writing files/vm.args
$ cd-
2. 修改rel/reltool.config文件(其中**之間是須要修改的地方): 
{sys,[
*{lib_dirs,["../../"]}*,      
{rel,"zfor","1",      
[       
kernel,        
stdlib,        
sasl,        
*zfor*        
]},       
{rel,"start_clean","",      
[       
kernel,        
stdlib        
]},       
{boot_rel,"zfor"},      
{profile,embedded},      
{excl_sys_filters,["^bin/.*",      
"^erts.*/bin/(dialyzer|typer)"]},                          
*{app,zfor,[{incl_cond,include}]}*,      
{app,sasl,[{incl_cond,include}]}      
]}.     
 
{rebar,[
{empty_dirs,[        
"log/sasl"                      
]},                     
 
{overlay,"overlay"}        
]}.        

3.       爲了使rel目錄對rebar可見,須要在項目根目錄下,建立rebar.config配置文件:

{sub_dirs,["rel"]}.

4.       配置完成後,生成發佈版本:

./rebar generate
==> rel (generate)

5.       發佈完成後,能夠運行發佈的服務:

$ chmod u+x rel/zfor/bin/zfor
./zfor start    #開啓服務
./zfor stop    #中止服務



轉載2:http://alancastro.org/2010/05/01/erlang-application-management-with-rebar.html

Erlang App. Management with Rebar

Introduction

Rebar is a build and packaging tool for Erlang applications. It is implemented in Erlang and the only dependency is the Erlang Virtual Machine, so it can work as a stand-alone escript in your project. Rebar was created byDave Smith from Basho, creators of Riak. In this tutorial I’ll be creating a sample application, building it and generating a release for it, all of this with Rebar.

Application Requirements

Rebar expects that your applications follows the OTP Design Principles. The application directory must have the following sub-directories:

  • src – Contains the Erlang source code.
  • ebin – Contains the Erlang object code, the beam files. The .app file is also placed here.
  • priv – Used for application specific files. For example, C executables are placed here. The function code:priv_dir/1 should be used to access this directory.
  • include – Used for include files.

Don’t worry about this, Rebar can create applications with this kind of directory structure automatically.

Installation

To install Rebar is very easy, just download it at: http://hg.basho.com/rebar/downloads/rebar. After getting it just run a chmod u+x rebar. Rebar depends on Erlang R13B03 (or later), so I advise you to get ErlangOTP source code em build it. There is a tutorial here: http://wiki.github.com/erlang/otp/installation.

Rebar’s Features

To see what Rebar offers just execute ./rebar -c and you’ll be able to see all of Rebar’s commands.

$ ./rebar -c analyze Analyze with Dialyzer build_plt Build Dialyzer PLT check_plt Check Dialyzer PLT clean Clean compile Compile sources create template= [var=foo,...] Create skel based on template and vars create-app Create simple app skel create-node Create simple node skel check-deps Display to be fetched dependencies get-deps Fetch dependencies delete-deps Delete fetched dependencies generate [dump_spec=0/1] Build release with reltool install [target=] Install build into target eunit [suite=foo] Run eunit [test/foo_tests.erl] tests int_test [suite=] [case=] Run ct suites in ./int_test perf_test [suite=] [case=] Run ct suites in ./perf_test test [suite=] [case=] Run ct suites in ./test 

I won’t be using all of the commands listed above for the sample application, so if you get more interested in it take a look at thesource code.

Getting Started

To get started off I’ll go step by step on creating a new project with rebar.

$ mkdir mysample $ cd mysample $ wget http://hg.basho.com/rebar/downloads/rebar $ chmod u+x rebar $ ./rebar create-app appid=mysample ==> mysample (create-app) Writing ebin/mysample.app Writing src/mysample_app.erl Writing src/mysample_sup.erl 

This was pretty easy to follow. Rebar creates this directory structure based on a previous created template. If you want to add new templates or change the ones that exists take a look at Rebar’s source code:http://bitbucket.org/basho/rebar/src and navigate topriv/templates/simpleapp.template.

Well, for now we’ve had Rebar create our initial application directories and files. Let’s take a look at the source code we’ve got so far:

ebin/mysample.app – Application descriptor

{application, mysample, [ {description, ""}, {vsn, "1"}, {modules, [ mysample_app, mysample_sup ]}, {registered, []}, {applications, [ kernel, stdlib ]}, {mod, { mysample_app, []}}, {env, []} ]}. 

src/mysample_app.erl – Application callback module

-module(mysample_app). -behaviour(application). -export([start/2, stop/1]). start(_StartType, _StartArgs) -> mysample_sup:start_link(). stop(_State) -> ok. 

src/mysample_sup.erl – Supervisor callback module

-module(mysample_sup). -behaviour(supervisor). -export([start_link/0]). -export([init/1]). -define(CHILD(I, Type), {I, {I, start_link, []}, permanent, 5000, Type, [I]}). start_link() -> supervisor:start_link({local, ?MODULE}, ?MODULE, []). init([]) -> {ok, { {one_for_one, 5, 10}, []} }. 

The generated code is almost a fully working simple application, but there is one thing missing, aGeneric Server! So the next step is to create one. I’ll create a generic server calledmysample_server.erl.

Place this code in src/mysample_server.erl:

-module(mysample_server). -behaviour(gen_server). -export([start_link/0, say_hello/0]). -export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]). start_link() -> gen_server:start_link({local, ?MODULE}, ?MODULE, [], []). init([]) -> {ok, []}. say_hello() -> gen_server:call(?MODULE, hello). %% callbacks handle_call(hello, _From, State) -> io:format("Hello from server!~n", []), {reply, ok, State}; handle_call(_Request, _From, State) -> Reply = ok, {reply, Reply, State}. handle_cast(_Msg, State) -> {noreply, State}. handle_info(_Info, State) -> {noreply, State}. terminate(_Reason, _State) -> ok. code_change(_OldVsn, State, _Extra) -> {ok, State}. 

The next step is make the generic server part of the application, this is done by doing the following steps:

  • Add mysample_server into ebin/mysample.app under the modules tuple.
  • Make mysample_server a child of mysample supervisor.

So by making this changes we have the following new codes:

ebin/mysample.app:

{application, mysample, [ {description, ""}, {vsn, "1"}, {modules, [ mysample_app, mysample_sup, mysample_server ]}, {registered, []}, {applications, [ kernel, stdlib ]}, {mod, { mysample_app, []}}, {env, []} ]}. 

src/mysample_sup.erl:

-module(mysample_sup). -behaviour(supervisor). -export([start_link/0]). -export([init/1]). -define(CHILD(I, Type), {I, {I, start_link, []}, permanent, 5000, Type, [I]}). start_link() -> supervisor:start_link({local, ?MODULE}, ?MODULE, []). init([]) -> MySampleServer = ?CHILD(mysample_server, worker), {ok, { {one_for_one, 5, 10}, [MySampleServer]} }. 

Now that we’ve made every change, let’s compile the project:

$ ./rebar compile ==> mysample (compile) Compiled src/mysample_app.erl Compiled src/mysample_sup.erl Compiled src/mysample_server.erl 

Now take a look at your ebin directory and check if each module has a beam file placed there.

What about now? We’ve got an application module, a supervisor, and a simple generic server, but what do we do with it? Well, wouldn’t be great to make your application work as a service that you can start, stop, and restart whenever you want? You can create this using some Erlang tools, but with Rebar you can do this in avery very easy way. Summarizing what I’ve just said: 「Rebar can help you create anErlang Release」.

To create a release we have to create a new directory in the root of our project calledrel and create a node in it. A node is kind of a standalone Erlang VM for our application to run on.

$ mkdir rel $ cd rel $ ../rebar create-node nodeid=mysample ==> rel (create-node) Writing reltool.config Writing overlay/erts-vsn/bin/erl Writing overlay/erts-vsn/bin/nodetool Writing overlay/bin/mysample Writing overlay/etc/app.config Writing overlay/etc/vm.args $ cd - 

We’ve just created a node for our application to run on, this node is created using a template just like the one used in the application creation. You can see it at Rebar’s source code:http://bitbucket.org/basho/rebar/src/ and then head topriv/templates/simplenode.template.

Now we’re almost done. We only need to make some changes to the rel/reltool.config, file that contains the release configuration, so it can be compatible with our application’s modules.

Here is the changed version of the generated file, I will put * around the lines I changed, notice that they don’t belong to the real code.

{sys, [ *{lib_dirs, ["../../"]}*, {rel, "mysample", "1", [ kernel, stdlib, sasl, *mysample* ]}, {rel, "start_clean", "", [ kernel, stdlib ]}, {boot_rel, "mysample"}, {profile, embedded}, {excl_sys_filters, ["^bin/.*", "^erts.*/bin/(dialyzer|typer)"]}, *{app, mysample, [{incl_cond, include}]}*, {app, sasl, [{incl_cond, include}]} ]}. {rebar, [ {empty_dirs, [ "log/sasl" ]}, {overlay, "overlay"} ]}. 

If you want to know the meaning of each of this tuple’s options, look at Reltool Manual.

Now lets make this directory visible for rebar, for that we create rebar.config file in the root directory of our project.

rebar.config:

{sub_dirs, ["rel"]}. 

Configuration is now over! Lets generate our release:

$ ./rebar generate 

This command creates a release of your application. Take a look at yours rel/mysample folder and you’ll be able to see something like this:

$ ls rel/mysample bin erts-5.7.4 etc lib log releases 

Now let’s run our release service, see the step by step below:

$ chmod u+x rel/mysample/bin/mysample $ ./rel/mysample/bin/mysample Usage: mysample {start|stop|restart|reboot|ping|console|attach} 

Seems that everything is alright. To make sure your application was packed in the release, run your service with console./rel/mysample/bin/mysample console and execute application:which_applications().. Check if you got something like this:

(mysample@127.0.0.1)1> application:which_applications(). [{mysample,[],"1"},  {sasl,"SASL CXC 138 11","2.1.8"},  {stdlib,"ERTS CXC 138 10","1.16.4"},  {kernel,"ERTS CXC 138 10","2.13.4"}] (mysample@127.0.0.1)2> mysample_server:say_hello(). Hello from server! 

If yes, you’ve done it! If not, make sure you’ve followed everything correctly.

Conclusions

The purpose of this tutorial was to give an introduction to a tool that helps we manage our Erlang applications. I think Rebar has a long way to go from here, but it is on the right track. It’s a great tool and if there were more documentation available there would have more people using it. I’ve talked about some of it’s most important features here, but in the beggining I showed that there is a lot more to it than just thancompile and generatefunctions. So get Rebar and start using it on your next project.


轉載3(問題解決):http://lists.basho.com/pipermail/rebar_lists.basho.com/2011-March/000661.html

Hi Ken,I am fairly new to rebar, and I had a similar problem the other day. Theproblem is that your application must reside in a directory whose name isthe same as the application's name. This means that your application'jaus_core' must be in ~/Desktop/work/1_jaus/jaus_coreand not ~/Desktop/work/1_jaus/erljausHope this helps!-- DominiqueDominique Boucher* *| CTO*Nu Echo Inc.***1435, Saint-Alexandre Street, Suite 200, Montreal (Qc), Canada, H3A 2G4Tel: (514) 861-3246 ext 4231 | FAX: (514) 861-1676Dominique.Boucher at nuecho.com | www.nuecho.com | blog.nuecho.com*Because performance matters.***On Fri, Mar 11, 2011 at 12:36 AM, Ken Robinson <kenrobinsonster at gmail.com>wrote:> Hi All,>> I was trying to generate a release using the howtos:> https://github.com/basho/rebar/wiki/Release-handling>> http://alancastro.org/2010/05/01/erlang-application-management-with-rebar.html>> Below is my workflow and my reltool.config. What I'm trying to> understand is whether I need populate all the reltool config values> myself or does rebar do this for me? Any help would be greatly> appreciated.> Ken.>> Workflow> ========>kenrobinsonster at ubuntu-dev-01:~/Desktop/work/1_jaus/erljaus$ mkdir rel>kenrobinsonster at ubuntu-dev-01:~/Desktop/work/1_jaus/erljaus$ cd rel/>kenrobinsonster at ubuntu-dev-01:~/Desktop/work/1_jaus/erljaus/rel$ rebar> create-node nodeid=jaus> ==> rel (create-node)> Writing reltool.config> Writing files/erl> Writing files/nodetool> Writing files/jaus> Writing files/app.config> Writing files/vm.args>kenrobinsonster at ubuntu-dev-01:~/Desktop/work/1_jaus/erljaus/rel$ rebar> generate> ==> rel (generate)> {"init terminating in do_boot","Release jaus uses non existing> application jaus_core"}>> Crash dump was written to: erl_crash.dump> init terminating in do_boot (Release jaus uses non existing> application jaus_core)>>kenrobinsonster at ubuntu-dev-01:~/Desktop/work/1_jaus/erljaus/rel$ ls> ../ebin/> jaus_core.app jaus_core_transport_receiverec.beam> jaus_core_app.beam jaus_core_transport_records.beam> ...>> ReltoolConfig> ===========> {sys, [> {lib_dirs, ["../..", "../ebin"]},> {rel, "jaus", "1",> [> kernel,> stdlib,> sasl,> mnesia,> jaus_core> ]},> {rel, "start_clean", "",> [> kernel,> stdlib> ]},> {boot_rel, "jaus"},> {profile, embedded},> {excl_sys_filters, ["^bin/.*",> "^erts.*/bin/(dialyzer|typer)"]},> {app, sasl, [{incl_cond, include}]}> ]}.>> {target_dir, "jaus"}.>> {overlay, [> {mkdir, "log/sasl"},> {copy, "files/erl", "{{erts_vsn}}/bin/erl"},> {copy, "files/nodetool", "{{erts_vsn}}/bin/nodetool"},> {copy, "files/jaus", "bin/jaus"},> {copy, "files/app.config", "etc/app.config"},> {copy, "files/vm.args", "etc/vm.args"}> ]}.>>> --> regards,> Ken Robinson> Mob +61438681120> Home +61738523767>> _______________________________________________> rebar mailing list>rebar at lists.basho.com>http://lists.basho.com/mailman/listinfo/rebar_lists.basho.com>

相關文章
相關標籤/搜索