使用target_system进行erlang应用的发行
时间:2010-10-10 来源:sw2wolf
一个简单实现了OTP的Server:echoline,一个Helloworld级别的OTP应用,目录结构如下:
- ./src/echoline_app.erl
- ./src/echoline_sup.erl
- ./src/echoline_server.erl
- ./src/tcp_server.erl
- ./ebin/echoline_app.app
- ./priv/
- ./include/
- ./echoline_rel.rel
- ./Emakefile
- {'src/*', [debug_info,{i,'include'},{outdir,'ebin'}]}.
- {application, echoline_app,
- [{description, "Echoline Server"},
- {vsn, "1"},
- {modules, [echoline_app, echoline_sup, echoline_server, tcp_server]},
- {registered, []},
- {applications, [kernel, stdlib, sasl]},
- {mod, {echoline_app,[]}}
- ]}.
- erl -pa ./ebin
- 1> application:load(echoline).
- ok
- {release,
- {"echoline_rel", "1"},
- {erts, "5.5.4"},
- [{kernel, "2.11.4"},
- {stdlib, "1.14.4"},
- {sasl, "2.1.5"},
- {echoline_app, "1"}]
- }.
注意,上面的模块版本以实际为主,我用的是R11B-4。而且{echoline, “1″}这句的 版本号,app名称 要跟echoline.app一致。
接着运行这两步就可以生成能够用于分发的release:
- 2> systools:make_script("echoline_rel", [local]).
- ok
- 3> systools:make_tar("echoline_rel").
- ok
程序会在根目录下面生成
- echoline_rel.boot
- echoline_rel.script
- echoline_rel.tar.gz
三个文件。这个tar.gz可以直接用于部署到其他机器上面。同时,还能够以
- erl -boot echoline_rel
这种方式启动应用。
但是这样还不够彻底,在没有Erlang的机器上面,最起码还得安装上一个CEAN才能启动这个应用,好在文档的《System Principles》中提供了一个target_system,使得生成应用的发行版本成为可能。
按照文档创建一个target_system.erl,windows下需要将:
- copy_file(filename:join([ErtsBinDir, "epmd"]),
- filename:join([TmpBinDir, "epmd"]), [preserve]),
- copy_file(filename:join([ErtsBinDir, "run_erl"]),
- filename:join([TmpBinDir, "run_erl"]), [preserve]),
- copy_file(filename:join([ErtsBinDir, "to_erl"]),
- filename:join([TmpBinDir, "to_erl"]), [preserve]),
改为
- copy_file(filename:join([ErtsBinDir, "epmd.exe"]),
- filename:join([TmpBinDir, "epmd.exe"]), [preserve]),
- copy_file(filename:join([ErtsBinDir, "start_erl.exe"]),
- filename:join([TmpBinDir, "start_erl.exe"]), [preserve]),
- copy_file(filename:join([ErtsBinDir, "werl.exe"]),
- filename:join([TmpBinDir, "werl.exe"]), [preserve]),
因为windows下的启动程序跟linux下有所不同。
然后运行:
- erl -pa ./ebin/
- Eshell V5.5.4 (abort with ^G)
- 1> target_system:create("echoline_rel").
- Reading file: "echoline_rel.rel" ...
- Creating file: "plain.rel" from "echoline_rel.rel" ...
- Making "plain.script" and "plain.boot" files ...
- Making "echoline_rel.script" and "echoline_rel.boot" files ...
- Creating tar file "echoline_rel.tar.gz" ...
- Creating directory "tmp" ...
- Extracting "echoline_rel.tar.gz" into directory "tmp" ...
- Deleting "erl" and "start" in directory "tmp/erts-5.5.4/bin" ...
- Creating temporary directory "tmp/bin" ...
- Copying file "plain.boot" to "tmp/bin/start.boot" ...
- Copying files "epmd", "run_erl" and "to_erl" from
- "tmp/erts-5.5.4/bin" to "tmp/bin" ...
- Creating "tmp/releases/start_erl.data" ...
- Recreating tar file "echoline_rel.tar.gz" from contents in directory "tmp" ...
- Removing directory "tmp" ...
- ok
它在根目录下生成了这些文件:
- echoline_rel.boot
- echoline_rel.script
- echoline_rel.tar.gz
- plain.boot
- plain.rel
- plain.script
将 echoline_rel.tar.gz 解压到某个目录下,比如 C:\echoline,就可以用
- C:\echoline>erts-5.5.4\bin\erl.exe -boot .\releases\1\start
来启动这个应用。
相关阅读 更多 +