Rebar3 builds

New project

Rebar3 is the standard build tool. The new command scaffolds a project from a template: app for an OTP application with a supervisor, lib for a plain library, escript for a command line tool.

The name you pass becomes the folder and the application name, and the generator writes the skeleton without asking a single question.

rebar3 new app myapp

===> Writing myapp/src/myapp_app.erl
===> Writing myapp/src/myapp_sup.erl
===> Writing myapp/src/myapp.app.src
===> Writing myapp/rebar.config
===> Writing myapp/.gitignore
===> Writing myapp/LICENSE.md
===> Writing myapp/README.md

Project layout

Source lives in src, build products in _build, and the rebar.config file holds the project settings and dependencies.

The scaffold also writes an OTP application skeleton with an application callback and a supervisor, which lessons 34 to 36 explain. The first build adds rebar.lock, the file that pins dependency versions.

myapp/ ├── rebar.config ├── src/ │ ├── myapp.app.src │ ├── myapp_app.erl │ └── myapp_sup.erl ├── .gitignore ├── LICENSE.md └── README.md

Compile

The compile task builds every module in src and leaves beam files under _build. Rebar3 tracks dependencies and only rebuilds what changed.

Warnings from the compiler appear in the output; treat them as errors until the project is green.

rebar3 compile

===> Verifying dependencies...
===> Analyzing applications...
===> Compiling myapp

Run it

The shell task starts an erl with the project on the code path and boots the application. Your modules are already loaded and ready to call.

For a one-off run without the shell, use the escript template or erl with the -pa flag pointing at the ebin folder.

rebar3 shell

===> Verifying dependencies...
===> Analyzing applications...
===> Compiling myapp
Erlang/OTP 27 [erts-15.2.7.14] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] [jit:ns]

===> Booted myapp
Eshell V15.2.7.14 (press Ctrl+G to abort, type help(). for help)

Next: Testing with EUnit

Questions

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

  1. Which template builds an OTP application with a supervisor?

    Answer

    app

    The app template writes an application callback and a supervisor; the lib template writes a plain library.

  2. What does rebar3 new app myapp create?

    Answer

    A project folder named myapp with a skeleton

    The generator writes the folder, the source files, the app resource file and the config.

  3. Which folder holds the source of a rebar3 project?

    Answer

    src

    Erlang sources live in src; tests go to test when the project has them.

  4. Where do build products go?

    Answer

    _build

    Rebar3 writes beams and generated files under _build/default/lib/<app>/.

  5. What is rebar.config for?

    Answer

    Project settings and dependencies

    The config holds compiler options, plugins and dependency declarations.

  6. What describes the application name, version and its dependencies?

    Answer

    src/myapp.app.src

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

  7. What does rebar3 compile do?

    Answer

    Builds every module in src under _build

    The compile task verifies dependencies, analyzes applications and compiles the sources to beams.

  8. What does rebar3 shell do?

    Answer

    Starts erl with the project on the code path and boots the app

    The shell task builds the project, adds its ebin folders to the code path and boots the application.

  9. Which file pins dependency versions after the first build?

    Answer

    rebar.lock

    rebar.lock records the exact versions the build resolved, so the next build gets the same ones.

  10. Which command starts a new project from a template?

    Answer

    rebar3 new

    rebar3 new <template> <name> writes a fresh project skeleton.

The examples on this page were run with Erlang/OTP 27 (erts-15.2.7.14) and rebar3 3.27.3 on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev

Banner image