Erlang Rebar3 #

一、Rebar3概述 #

Rebar3是Erlang的标准构建工具,提供项目管理、依赖管理、编译、测试等功能。

1.1 安装 #

bash
brew install rebar3

或下载:

bash
wget https://s3.amazonaws.com/rebar3/rebar3
chmod +x rebar3

二、项目创建 #

2.1 创建应用 #

bash
rebar3 new app my_app

2.2 创建库 #

bash
rebar3 new lib my_lib

2.3 创建发布 #

bash
rebar3 new release my_release

2.4 项目结构 #

text
my_app/
├── src/
│   ├── my_app.app.src
│   ├── my_app_app.erl
│   └── my_app_sup.erl
├── test/
├── rebar.config
└── README.md

三、rebar.config #

3.1 基本配置 #

erlang
{erl_opts, [debug_info]}.
{deps, []}.
{shell, [
    {apps, [my_app]}
]}.

3.2 依赖配置 #

erlang
{deps, [
    {cowboy, "2.9.0"},
    {jiffy, "1.0.8"},
    {lager, "3.9.2"}
]}.

3.3 配置文件 #

erlang
{erl_opts, [debug_info, warnings_as_errors]}.

{deps, [
    {cowboy, "2.9.0"}
]}.

{shell, [
    {apps, [my_app]},
    {config, "config/sys.config"}
]}.

{profiles, [
    {test, [
        {deps, [{meck, "0.9.2"}]},
        {erl_opts, [debug_info]}
    ]}
]}.

{plugins, [
    {rebar3_proper, "0.12.1"}
]}.

四、常用命令 #

4.1 编译 #

bash
rebar3 compile

4.2 获取依赖 #

bash
rebar3 get-deps
rebar3 update-deps

4.3 运行Shell #

bash
rebar3 shell

4.4 测试 #

bash
rebar3 eunit
rebar3 ct
rebar3 proper

4.5 清理 #

bash
rebar3 clean

4.6 发布 #

bash
rebar3 release
rebar3 tar

五、依赖管理 #

5.1 添加依赖 #

在rebar.config中添加:

erlang
{deps, [
    {cowboy, "2.9.0"}
]}.

5.2 Git依赖 #

erlang
{deps, [
    {my_dep, {git, "https://github.com/user/my_dep.git", {tag, "1.0.0"}}}
]}.

5.3 本地依赖 #

erlang
{deps, [
    {my_local_dep, {path, "../my_local_dep"}}
]}.

六、发布配置 #

6.1 relx配置 #

erlang
{relx, [
    {release, {my_app, "1.0.0"}, [my_app, sasl]},
    {mode, dev},
    {extended_start_script, true}
]}.

6.2 构建发布 #

bash
rebar3 release

6.3 打包 #

bash
rebar3 tar

七、插件 #

7.1 常用插件 #

erlang
{plugins, [
    {rebar3_proper, "0.12.1"},
    {rebar3_eqc, "1.0.0"},
    {rebar3_lint, "0.4.0"}
]}.

7.2 使用插件 #

bash
rebar3 proper
rebar3 lint

八、总结 #

本章学习了:

  • Rebar3概述
  • 项目创建
  • 配置文件
  • 常用命令
  • 依赖管理
  • 发布配置
  • 插件使用
最后更新:2026-03-27