Ilie NEACSU
Ilie NEACSU

Reputation: 540

What is the correct way to load the code paths of dependencies in an Erlang application.

So assuming that i have an rebar app structure and in deps folder i have a number of dependencies, some are library applications and some are applications that need to be started. I usually do it like this:

start(_Type, _Args) ->
   code:add_path("../deps/depapp/ebin"),
   {ok, _} = application:ensure_all_started(depapp),

Is this the correct way to do it in an development environment ? How about in production ?

Upvotes: 3

Views: 505

Answers (1)

Hamidreza Soleimani
Hamidreza Soleimani

Reputation: 2544

What you used is not necessarily a wrong way but could exposes some problems. For instance this way you have no choices for starting dependent applications which must be started before your application.

So there are other alternatives for loading or starting dependent OTP applications or libraries.

1) Using erl command line flags:

erl -pa ebin deps/*/ebing -s your_dep_app start -s your_app start

2) Using package manger for handling it:

As an example Rebar as a package manager can handle it for you. What you need is specifying your application dependencies in rebar.config and then issue rebar get-deps for Rebar2 or rebar3 compile for Rebar3. Following is a snippet of a sample config file for Rebar3:

{deps,[
  %% Packages
  rebar,
  {rebar,"1.0.0"},
  {rebar, {pkg, rebar_fork}}, % rebar app under a different pkg name
  {rebar, "1.0.0", {pkg, rebar_fork}},
  %% Source Dependencies
  {rebar, {git, "git://github.com/erlang/rebar3.git"}},
  {rebar, {git, "http://github.com/erlang/rebar3.git"}}]}.

For more information about Rebar dependency manager take a look at this link.

Also for starting or loading them using Rebar you can make a release and let Rebar start or load them. Following is a snippet of a sample Rebar config file for making a release:

{relx, [
    {release, 
    {your_app, "0.1.0"},
        [your_dep_app_1,
         {your_dep_app_2, load}]}]}.

This config loads and starts your_dep_app_1 but just loads your_dep_app_2. For more information about Rebar release manager take a look at this link.

Upvotes: 1

Related Questions