rebar3 escriptize

Here's a full example using rebar3 escriptize:
λ /tmp → rebar3 new escript hello
===> Writing hello/src/hello.erl
===> Writing hello/src/hello.app.src
===> Writing hello/rebar.config
===> Writing hello/.gitignore
===> Writing hello/LICENSE
===> Writing hello/README.md
λ /tmp → cd hello
→ vim src/hello.erl
λ hello → cat src/hello.erl

-module(hello).

%% API exports
-export([main/1]).

%% escript Entry point
main(_Args) ->
    io:format("Hello World~n", []),
    erlang:halt(0).
λ hello → rebar3 escriptize
===> Verifying dependencies...
===> Compiling hello
===> Building escript...
λ hello → _build/default/bin/hello
Hello World

So what does this require? First of all, let's take a look at the rebar.config file:
{deps, []}.

{escript_incl_apps,
 [hello]}.
{escript_main_app, hello}.
{escript_name, hello}.
{escript_emu_args, "%%! +sbtu +A0\n"}.
the big deal is having the escript_main_app set to the application name I want to use. (here, hello)
That application has a module with the same name (hello) that exports a function main/1
if your application has runtime dependencies, they should be declared in the applications tuple of your .app.src file (in src/)
With these two things we can build an escript. The escript, as @fenollp said, ships without the Erlang VM and requires it to be installed. It makes Erlang become a scripting language the same way Python or Ruby would be, in some ways. It is still compiled and bundles its own code, but depends on a runtime environment being present.
If you want to boot your application in the escript, then calling application:ensure_all_started(YourApp) is likely the best way to go, and a timer:sleep(infinity). will help things keep running.
Let us know if you need more assistance.

 

posted @ 2021-12-02 11:41  孤独信徒  阅读(97)  评论(0编辑  收藏  举报