Rebar3 builds
A single erlc call builds one module. A project has modules, a test suite, dependencies and a release, and rebar3 is the build tool that knows all of them. It is the tool the rest of this chapter uses, and this lesson runs it from an empty folder to a project that compiles and whose test task reports what it found. The Erlang toolchain in this course ships rebar3 already, so nothing has to be installed.
A new project
First the version, because a project file written for another major version may not build. Then new, which takes a template and a name. The app template writes an OTP application with an application callback and a supervisor, the shape a real service starts from:
rebar3 version
rebar 3.27.3 on Erlang/OTP 27 Erts 15.2.7.14
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
Every line names a file the generator wrote. The name becomes the folder, the application name and the OTP application callback module, so a name that is already an atom of another project is refused before anything is written.
The layout
Source lives in src, build products in _build, and rebar.config holds the compiler options and the dependencies. After the first build the folder also holds rebar.lock, the file that pins the exact dependency versions the build resolved:
myapp/ ├── rebar.config ├── rebar.lock ├── src/ │ ├── myapp.app.src │ ├── myapp_app.erl │ └── myapp_sup.erl ├── _build/ │ └── default/ │ └── lib/ │ └── myapp/ ├── .gitignore ├── LICENSE.md └── README.md
The wspw tree is a drawing, not a command output: the build folder is deep and its leaves depend on the version. The project layout lesson opens each file, including the .app.src resource that names the application and its dependencies.
Compile
compile builds every module in src and writes the beams under _build/default/lib/myapp/ebin. The three task lines are the whole output of a clean project: dependencies verified, applications analyzed, the application compiled. A warning from the compiler arrives on the same stream and a project treats it as an error until it is gone:
rebar3 compile
===> Verifying dependencies... ===> Analyzing applications... ===> Compiling myapp
The task keeps its work between runs: the next compile only rebuilds a module whose source or options changed. That is why a build that looks silent is not broken; it says nothing was out of date.
Run the tests
eunit compiles the project and runs every test module. A fresh scaffold has none, and the task says so instead of failing:
rebar3 eunit
===> Verifying dependencies... ===> Analyzing applications... ===> Compiling myapp ===> Performing EUnit tests... Finished in 0.011 seconds 0 tests
The timing line is the only part that changes between runs, which is why a course page quotes it and a test does not compare it. The EUnit lesson writes a test module, and the Common Test lesson adds the suite that covers a whole application.
The shell
rebar3 shell builds the project, puts its ebin folders on the code path and boots the application, then leaves an ordinary Erlang shell. It does not end by itself, so this page shows the command and not a transcript:
rebar3 shell
Inside it, the modules of the project are already loaded, and myapp_app:start/2 has run. A command that must end, such as a test or a release build, belongs in its own task of the build tool, and a release is built so that the system starts without a shell.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
Which template builds an OTP application with a supervisor?
Answer
appThe app template writes an application callback and a supervisor; the lib template writes a plain library.
-
What does
rebar3 new app myappcreate?Answer
A project folder named
myappwith a skeletonThe generator writes the folder, the source files, the app resource file and the config.
-
Which folder holds the source of a rebar3 project?
Answer
srcErlang sources live in
src; tests go totestwhen the project has them. -
Where do build products go?
Answer
_buildRebar3 writes beams and generated files under
_build/default/lib/<app>/. -
What is
rebar.configfor?Answer
Project settings and dependencies
The config holds compiler options, plugins and dependency declarations.
-
What describes the application name, version and its dependencies?
Answer
src/myapp.app.srcThe
.app.srcfile becomes the application resource, and it lists the applications the app needs. -
What does
rebar3 compileprint for a clean project?Answer
Verifying dependencies,Analyzing applicationsandCompiling myappThe three task lines are the whole output; the beams are written under
_buildwithout being listed. -
What does
rebar3 eunitreport for a project with no tests?Answer
0 testsThe task treats an empty suite as a successful run and reports the count it ran.
-
What does
rebar3 shelldo?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.
-
Which file pins dependency versions after the first build?
Answer
rebar.lockrebar.lockrecords the exact versions the build resolved, so the next build gets the same ones.
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