简体   繁体   English

如何从 mix.exs 启动多个应用程序?

[英]How to launch multiple applications from mix.exs?

I have an Elixir app with two applications inside the lib/ folder:我有一个 Elixir 应用程序,其中 lib/ 文件夹中有两个应用程序:

myproject/lib/app1 myproject/lib/app2

They both have files which use Application:他们都有使用应用程序的文件:

myproject/lib/app1.exs myproject/lib/app2.exs

They each implement start and spawn a supervision tree.他们每个人都实现了start并产生了一个监督树。

In myproject/mix.exs I tried:myproject/mix.exs我试过:

  def application do
    [
      mod: {app1, []},
           {app2, []},
      applications: [:foo, :bar]
    ]
  end

But all I get are syntax errors on the line with {app2, []} .但我得到的只是与{app2, []}的语法错误。

Is such a thing even possible?这样的事情甚至可能吗? If not, what is the right way to launch separate applications with supervision trees in Elixir?如果没有,在 Elixir 中使用监督树启动单独应用程序的正确方法是什么?

You should add app2 as dependency of app1 and call it in applications, like: 您应该将app2添加为app1的依赖项并在应用程序中调用它,例如:

mix.exs for app2: 适用于app2的mix.exs:

  #...
  def application do
    [
      mod: {My.App2, []},
      applications: [:logger]
    ]
  end

  defp deps do
    [
      ...
    ]
  end
  #...
end

mix.exs for app1: 适用于app1的mix.exs:

  def application do
    [
      mod: {My.App1, []},
      applications: [:logger, :my_app2]
    ]
  end

  defp deps do
    [
      {:my_app2, in_umbrella: true}
    ]
  end
  # ...
end

This is the case if both apps are in the same umbrella. 如果两个应用程序都在同一个保护伞中就是这种情况。 If not, just add my_app2 as you would (from hex, path or git). 如果没有,只需添加my_app2(从十六进制,路径或git)。

For more information on dependencies and umbrella projects, take a look @ http://elixir-lang.org/getting-started/mix-otp/dependencies-and-umbrella-apps.html 有关依赖项和伞状项目的更多信息,请查看@ http://elixir-lang.org/getting-started/mix-otp/dependencies-and-umbrella-apps.html

If it makes sense for app1 to be responsible for starting app2 then another option is to use Application.start(app2) inside app1 where appropriate.如果 app1 负责启动 app2 是有意义的,那么另一种选择是在app1中适当使用Application.start(app2)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM