Stuff Goes Bad:Erlang In Anger

OTP Applications

For OTP applications, the proper structure is pretty much the same as what was explained in 1.2:

 对于OTP applicaitons,合适的结构基本同1.2中描述的那样:
----------------------------------------------------------------------------------

1 doc/
2 deps/
3 ebin/
4 src/
5 test/
6 LICENSE.txt
7 README.md
8 rebar.config

----------------------------------------------------------------------------------
What’s new in this one is the deps/ directory, which is fairly useful to have, but that will be generated automatically by rebar 2 if necessary.
That’s because there is no canonical package management in Erlang. People instead adopted rebar, which fetches dependencies locally, on a per-project basis.
This is fine and removes a truckload of conflicts, but means that each project you have may have to download its own set of dependencies.
This is accomplished with rebar by adding a few config lines to rebar.config:

 新加的文件夹:deps/ 非常有用的一个文件夹。这个文件夹如果有必要也可以直接通过rebar自动生成2
这是因为在Erlang没有标准的包管理器(canonical package management)。现在的做法是用rebar来为每一个项目在本地获取依赖项。
 这可以解决一大堆的冲突,但也意味着每个项目都要下载自己的依赖项目。
 下面是通过在rebar.config里面增加一些配置项来配置rebar:
----------------------------------------------------------------------------------
1 {deps,
2 [{application_name, "1.0.*",
3 {git, "git://github.com/user/myapp.git", {branch,"master"}}},
4 {application_name, "2.0.1",
5 {git, "git://github.com/user/hisapp.git", {tag,"2.0.1"}}},
6 {application_name, "",
7 {git, "https://bitbucket.org/user/herapp.git", "7cd0aef4cd65"}},
8 {application_name, "my regex",
9 {hg, "https://bitbucket.org/user/theirapp.hg" {branch, "stable"}}}]}.
----------------------------------------------------------------------------------

Feel free to install rebar globally on your system, or keep a local copy if you require a specific version to build your system. Applications are fetched directly from a git (or hg, or svn) source, recursively. They can then be compiled, and specific compile options can be added with the {erl_opts, List}.option in the config file 3 .
Within these directories, you can do your regular development of an OTP application.
To compile them, call rebar get-deps compile, which will download all dependencies,and then build them and your app at once.
When making your application public to the world, distribute it without the dependencies. It’s quite possible that other developers’ applications depend on the same applications yours do, and it’s no use shipping them all multiple times.
The build system in place (in this case, rebar) should be able to figure out duplicated entries and fetch everything necessary only once.

 rebar会递归地从git(或hg,或svn)中直接拿到application的源代码。他们可以被编译,也可使用特定的选项{erl_opts,List}编译. 此选项也在config文件中定义3
你可以在这些文件夹中,开发自己的OTP application功能。
 为了编译他们,你可以使用rebar get-deps把依赖项也下载下来编译,并马上构建依赖项和你自己的app。
 当你把自己的application开源出来时,要把它的依赖项给去掉。因为其他开发者的application很有可能和你一样依赖同样的application,没有必要重复装载他们多次。
 构建系统(在这里,指rebar)应当能判断出重复的依赖项并让所有的项只会被加载一次。

[2] A lot of people package rebar directly in their application. This was initially done to help people who had never used rebar before use libraries and projects in a boostrapped manner.
[3] More details by calling rebar help compile.

[注2]:很多人都是直接把rebar集成在自己的applicaiton中。这最初是为了帮助那些从来不使用rebar的人快速掌握这个工具,你可以在系统里全局自由安装rebar,也可以在局部保存一个特定的版本来构建你的系统。
[注3]:你可以输入rebar help来查看更多的帮助信息。