简体   繁体   English

如何获取每个环境的Elixir依赖关系?

[英]How to fetch Elixir dependencies per environment?

There are multiple ways to install Elixir dependencies. 有多种安装Elixir依赖项的方法。 I wonder what happens in the following cases: 我想知道在以下情况下会发生什么:

1. 1。

mix deps.get --only prod

What exactly dependencies are being fetched then? 那么究竟获取了哪些依赖关系?

2. 2。

defp deps do
  [
    {:credo, "~> 0.8", only: ~w(dev)a, runtime: false},
  ]
end

How only option impact on a particular dependency? only选择如何影响特定的依存关系?

3. 3。

def project do
  [
    # ...
    deps: deps(Mix.env()),
  ]
end

What is the difference if we specify dependencies like that? 如果我们这样指定依赖项,有什么区别?


I'm confused a little bit when to use what regarding defining dependencies. 我对于何时使用有关定义依赖项的内容感到有些困惑。

When you write this : 当你写这个:

mix deps.get --only prod

It will fetch all dependencies for the prod environment, namely the dependencies where there is no only option, and dependencies where the only option is specified and containing :prod (eg {:some_dep, "~> 0.8", only: [:prod]} ) 它将获取prod环境的所有依赖项,即没有only选项的依赖项,以及指定了only选项并包含:prod依赖项(例如{:some_dep, "~> 0.8", only: [:prod]}

When you write this : 当你写这个:

defp deps do
  [
    {:some_dep, "~> 0.8"}
  ]
end

That tells mix to install some_dep in any environment it is run into. 这告诉mix在运行的任何环境中安装some_dep

When you write this : 当你写这个:

defp deps do
  [
    {:another_dep, "~> 0.8", only: [:dev]}
  ]
end

It tells mix to install another_dep only when your environment is dev ( MIX_ENV=dev ). 它告诉mix只有在您的环境为devMIX_ENV=dev )时才安装another_dep If you are in any other environment (eg prod), mix deps.get will simply ignore another_dep and won't install it. 如果您在任何其他环境(例如产品)中, mix deps.get只会忽略another_dep而不安装它。

writing this : 写这个:

def project do
  [
    # ...
    deps: deps(Mix.env()),
  ]
end

will result in ** (CompileError) mix.exs:13: undefined function deps/1 because in your mix.exs , only deps/0 is defined. 将导致** (CompileError) mix.exs:13: undefined function deps/1因为在mix.exs ,仅定义了deps/0 Now you might tell me why not implement deps(:dev) , deps(:prod) and so on... Well, if you read what I explained before, you'll see that it is pointless, since the deps separation for each environment is already taken care of :) 现在,您可能会告诉我为什么不实现deps(:dev)deps(:prod)等等...好吧,如果您阅读了我之前解释的内容,您会发现它毫无意义,因为每个deps的分隔环境已经照顾好了:)

I'm going to address these in reverse order. 我将以相反的顺序解决这些问题。

using deps(Mix.env) would force you to specify each of your dependencies multiple times if they are used across multiple environments. 如果在多个环境中使用依赖关系,则使用deps(Mix.env)将强制您多次指定每个依赖关系。 Something along the lines of 遵循以下原则

def deps(:dev) do
  [
    {:ecto, "~> 2.1"}
    {:credo, "~> 0.8", runtime: false}
  ]
end
def deps(:test) do
  [
    {:ecto, "~> 2.1"}
  ]
end

I will admit that I do not even know if this would work, but this is adding too much code for something that is already handled for you if you just specify the :only option. 我承认,我什至不知道这是否行得通,但是如果您仅指定:only选项,那么这会为已经为您处理的内容添加太多代码。

Using :only allows you to specify which environments a dependency should be used in. In your example, {:credo, "~> 0.8", only: [:dev], runtime: false} you are telling mix that the credo package should only be used in the dev environment. 使用:only允许您指定在哪个环境中使用依赖项。在您的示例中, {:credo, "~> 0.8", only: [:dev], runtime: false}您告诉混合credo软件包应该仅在开发环境中使用。 If you do not include the :only option, the package will be used in all environments. 如果不包括:only选项,则该软件包将在所有环境中使用。

$ mix deps.get --only prod will only retrieve the packages relevant to the production environment. $ mix deps.get --only prod将仅检索与生产环境相关的软件包。 From the previous example, the credo package will not be retrieved because you told mix that credo should only be used in the dev environment. 在前面的示例中,将不会检索credo软件包,因为您告诉mix,credo仅应在dev环境中使用。

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

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