Command 'generate' not found, compiling with rebar

1.5k views Asked by At

I am following this blog: http://maplekeycompany.blogspot.se/2012/03/very-basic-cowboy-setup.html

In short, I am trying to compile an application with rebar just as the person in the blog. Everything goes smoothly until I want to run the command:

./rebar get-deps compile generate

This then give me the following errors and warnings,

> User@user-:~/simple_server/rebar$ ./rebar get-deps compile generate
> ==> rebar (get-deps)
> ==> rebar (compile) Compiled src/simple_server.erl Compiled src/simple_server_http.erl src/simple_server_http_static.erl:5:
> Warning: behaviour cowboy_http_handler undefined Compiled
> src/simple_server_http_static.erl
> src/simple_server_http_catchall.erl:2: Warning: behaviour
> cowboy_http_handler undefined Compiled
> src/simple_server_http_catchall.erl WARN:  'generate' command does not
> apply to directory /home/harri/simple_server/rebar Command 'generate'
> not understood or not applicable

I have found a similar post with the same error:

Command 'generate' not understood or not applicable

I think the problem is in the reltool.config but do not know how to proceed, I changed the path to the following: {lib_dirs, ["home/user/simple_server/rebar"]}

Is there a problem with the path? How can rebar get access to all the src files and also the necessary rebar file to compile and build the application?

1

There are 1 answers

0
Steve Vinoski On BEST ANSWER

You need to make sure your directory structure and its contents are arranged so that rebar knows how to build everything in your system and generate a release for it. Your directory structure should look like this:

project
   |
   -- rel
   |
   -- deps
   |
   -- apps
       |
       -- myapp
       |    |
       |    -- src
       |    -- priv
       |
       -- another_app

The rel directory holds all the information needed to generate a release, and the apps directory is where the applications that make up your project live. Application dependencies live in the deps directory. Each app such as myapp and another_app under the apps directory can have their own rebar.config files. While two or more such applications are possible here, normally you'd have just one and all others would be dependencies.

In the top-level project directory there's also a rebar.config file with contents that look like this:

{sub_dirs, ["rel", "apps/myapp", "apps/another_app"]}.
{lib_dirs, ["apps"]}.

If necessary, you can use rebar to generate your apps from application skeletons:

cd apps
mkdir myapp another_app
( cd myapp && rebar create-app appid=myapp )
( cd another_app && rebar create-app appid=another_app )

If an application has dependencies, you'll have to add a rebar.config to its directory and declare each dependency there. For example, if myapp depends on application foo version 1.2, create apps/myapp/rebar.config with these contents:

{deps,
 [{foo, "1.*", {git, "git://github.com/user/foo.git", {tag, "foo-1.2"}}}]
}.

When you run rebar get-deps, rebar will populate the top-level deps directory to hold all dependencies, creating deps if necessary. The top-level rebar.config can also declare dependencies if necessary.

You also need to generate a node, necessary for your releases:

cd ../rel
rebar create-node nodeid=project

You then need to modify the reltool.config file generated by the previous step. You need to change

{lib_dirs, []},

to

{lib_dirs, ["../apps", "../deps"]},

and just after the line {incl_cond, derived}, add {mod_cond, derived}, so that releases contain only the applications needed for correct execution.

Next, wherever the atom 'project' appears, you need to replace it with the applications under the apps directory. For our example, we'd change this part:

{rel, "project", "1",
 [
  kernel,
  stdlib,
  sasl,
  project
 ]},

to this:

{rel, "project", "1",
 [
  kernel,
  stdlib,
  sasl,
  myapp,
  another_app
 ]},

and change this part:

{app, project, [{mod_cond, app}, {incl_cond, include}]}

to this:

{app, myapp, [{mod_cond, app}, {incl_cond, include}]},
{app, another_app, [{mod_cond, app}, {incl_cond, include}]}

You might also need to add the line:

{app, hipe, [{incl_cond, exclude}]},

to exclude the hipe application since sometimes it causes errors during release generation or when trying to run the release. Try without it first, but add it if you see errors related to hipe when generating a release, or if attempts to run the generated release result in this sort of error:

{"init terminating in do_boot",{'cannot load',elf_format,get_files}}

you'll need to add it.

With all this in place you can now execute:

rebar get-deps compile generate

and you should be able to successfully generate the release. Note that running rebar generate at the top level rather than in the rel dir will result in a harmless warning like this, which you can ignore:

WARN:  'generate' command does not apply to directory /path/to/project

Finally, you can run the release. Here's how to run it with an interactive console:

$ ./rel/project/bin/project console
Exec: /path/to/project/rel/project/erts-6.2/bin/erlexec  -boot /path/to/project/rel/project/releases/1/project -mode embedded -config /path/to/project/rel/project/releases/1/sys.config -args_file /path/to/project/rel/project/releases/1/vm.args -- console
Root: /path/to/project/rel/project
Erlang/OTP 17 [erts-6.2] [source] [64-bit] [smp:8:8] [async-threads:10] [kernel-poll:false]

Eshell V6.2  (abort with ^G)
([email protected])1>

or you could run ./rel/project/bin/project start to start it in the background. Run ./rel/project/bin/project with no arguments to see all available options.