The rebar3 project layout

rebar3 new app shop writes more than a folder of modules: each file has a job in the build. rebar.config holds the compiler options and the dependencies, src/shop.app.src becomes the application resource, and the two generated modules are the callbacks OTP calls to start and stop the application. This lesson reads those four files, which are the same in every Erlang project, whatever the name.

The folders

src holds the Erlang sources and the .app.src resource. test holds the EUnit and Common Test suites; only the test profile reads it, so test code never reaches a release. priv holds data a program reads at run time, such as a template or a certificate; code:priv_dir/1 finds it after the build. include holds the .hrl headers a module shares, and _build is written by the tool, never edited by hand.

The build folder has a profile for every purpose: _build/default for the normal build, _build/test for the test profile and _build/prod for a release. Each holds a copy of the application under lib, with an ebin folder of beams and the generated .app file.

rebar.config

The config file is a list of Erlang terms. The scaffold writes only the compiler options and an empty dependency list; the shell section names the applications the shell task boots. debug_info is not optional in practice: without it a beam has no debug information, and Dialyzer and the debugger lose the line numbers they need:

shop/rebar.config

{erl_opts, [debug_info]}. {deps, []}. {shell, [ %% {config, "config/sys.config"}, {apps, [shop]} ]}.

A real project adds entries to this file, not to a makefile: {deps, [{cowboy, "2.12.0"}]} names a dependency, {profiles, [{test, [{erl_opts, [nowarn_export_all]}]}]} changes the options for one profile, and a plugin is one tuple of its own. The rebar3 lesson shows the tasks that read it.

The .app.src resource

The .app.src file describes the application: its name, version, the callback module that starts it, and the other applications it needs before it can start. Rebar fills in the modules list and writes the result as shop.app in the ebin folder of the build:

shop/src/shop.app.src

{application, shop, [ {description, "An OTP application"}, {vsn, "0.1.0"}, {registered, []}, {mod, {shop_app, []}}, {applications, [ kernel, stdlib ]}, {env, []}, {modules, []}, {licenses, ["Apache-2.0"]}, {links, []} ]}.

Two entries matter at start time. applications is the start order: kernel and stdlib are always there, and a project that uses crypto or inets must name it, or the call fails with undef at run time. mod names the application callback, the module OTP calls with start/2 and stop/1. The OTP applications lesson starts and stops one.

The generated modules

The application callback starts the top supervisor and answers its pid. Both arguments of start/2 are unused in the scaffold, so both carry an underscore; the compiler treats an unused variable as an error under -Werror:

shop/src/shop_app.erl

%%%------------------------------------------------------------------- %% @doc shop public API %% @end %%%------------------------------------------------------------------- -module(shop_app). -behaviour(application). -export([start/2, stop/1]). start(_StartType, _StartArgs) -> shop_sup:start_link(). stop(_State) -> ok. %% internal functions

The supervisor callback answers the flags of the tree and the list of children. The scaffold has no children, so the list is empty and the strategy does not matter yet; the supervisors lesson fills both in:

shop/src/shop_sup.erl

%%%------------------------------------------------------------------- %% @doc shop top level supervisor. %% @end %%%------------------------------------------------------------------- -module(shop_sup). -behaviour(supervisor). -export([start_link/0]). -export([init/1]). -define(SERVER, ?MODULE). start_link() -> supervisor:start_link({local, ?SERVER}, ?MODULE, []). init([]) -> SupFlags = #{ strategy => one_for_all, intensity => 0, period => 1 }, ChildSpecs = [], {ok, {SupFlags, ChildSpecs}}. %% internal functions

The module attribute -behaviour is what makes the file a callback module: the compiler checks that the functions the behaviour requires are exported with the right arity. A missing init/1 is a warning, and with -Werror a failed build, long before the node tries to start the tree.

What the build writes

After rebar3 compile the application folder under _build/default/lib holds an ebin with one beam per module and the generated .app file. A manual erlc -o ebin writes only the beams and no resource, which is why a module compiled by hand works in a shell but not as an application:

shop/ ├── rebar.config ├── rebar.lock ├── src/ │ ├── shop.app.src │ ├── shop_app.erl │ └── shop_sup.erl └── _build/ └── default/ └── lib/ └── shop/ └── ebin/ ├── shop.app ├── shop_app.beam └── shop_sup.beam

The tree is a drawing of the finished build, not a command output. A clean build that removed the folder rebuilds it in one task, and the folder is never committed: the scaffold writes it into .gitignore for exactly that reason.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. Which folder holds the Erlang sources of a rebar3 project?

    Answer

    src

    src holds the modules and the .app.src resource; _build holds the build output.

  2. What does the debug_info compiler option add?

    Answer

    Debug information that Dialyzer and the debugger read

    Without debug information a beam has no line numbers, and the analysis tools lose their positions.

  3. Which file describes the application name, version and dependencies?

    Answer

    src/shop.app.src

    The .app.src file becomes the application resource and lists the applications the app needs.

  4. What does the mod entry of the application resource name?

    Answer

    The application callback module

    OTP calls start/2 and stop/1 of the module named by mod when the application starts and stops.

  5. What does this module print? -module(q1). -export([main/0]). main() -> App = {application, shop, [{vsn, "0.1.0"}, {mod, {shop_app, []}}]}, io:format("~p~n", [element(2, App)]).

    Answer

    shop

    The second element of the resource tuple is the application name.

  6. What does this module print? -module(q2). -export([main/0]). main() -> App = {application, shop, [{vsn, "0.1.0"}, {mod, {shop_app, []}}]}, Props = element(3, App), io:format("~p~n", [proplists:get_value(vsn, Props)]).

    Answer

    "0.1.0"

    The third element is the property list, and get_value/2 returns the string that the vsn key holds.

  7. What does this module print? -module(q3). -export([main/0]). init([]) -> {ok, {#{strategy => one_for_all, intensity => 0, period => 1}, []}}. main() -> {ok, {Flags, Children}} = init([]), io:format("~p~n", [{maps:get(strategy, Flags), Children}]).

    Answer

    {one_for_all,[]}

    The supervisor callback returns the flags and the child list together, and the program prints both.

  8. What does this module print? -module(q4). -export([main/0]). main() -> App = {application, shop, [{applications, [kernel, stdlib]}]}, Props = element(3, App), io:format("~p~n", [proplists:get_value(applications, Props)]).

    Answer

    [kernel,stdlib]

    The applications key holds the list of applications the application depends on.

  9. Why does a module compiled with plain erlc fail as an application?

    Answer

    It has no .app resource

    An application start needs the resource file; erlc writes beams only, while rebar3 generates the .app file.

  10. What does -behaviour(supervisor) make the compiler check?

    Answer

    That the required callbacks are exported with the right arity

    A behaviour declares callbacks, and the compiler warns when a module that claims it does not export them.

The files on this page are the ones `rebar3 new app shop` writes with rebar3 3.27.3 on Erlang/OTP 27 (erts-15.2.7.14); the modules were compiled with `erlc +debug_info -Werror` and passed Elvis and Dialyzer in a Docker container.

Article author: Arthur Isaev