问题描述
我正在编写一个Elixir软件包,我想指定一个默认的应用程序配置(用户可以通过在其 config.exs
中指定自定义值来覆盖它)。我最初将它们放在项目的 config.exs
中,直到我意识到依赖该库的项目不会加载配置文件。
I'm writing an Elixir package, and I want to specify a default application configuration (that the user can override by specifying custom values in their config.exs
). I was originally putting them in my project's config.exs
until I realized that the config file won't be loaded for projects that depend on this library.
配置文件本身告诉您:
我一直在努力了解如何在 mix.exs中指定应用程序默认值
并使用它们。我当前的解决方案是使用具有默认参数,但对我来说似乎不对,因为应用程序默认值会分散在整个代码中。
I've been struggling to understand how to specify application defaults in my mix.exs
and use them. My current solution is to use Application.get_env/3
with a default argument but that doesn't seem right to me as the application defaults would be scattered through out the code.
Application.get_env(:my_library, :arg, "default value")
所以,如何在 mix.exs
中指定应用程序默认值?
So, How can I specify application defaults in mix.exs
?
推荐答案
您可以在 mix.exs
中为应用程序设置默认配置值-当在另一个项目中用作依赖项时,这些值也将可用。例如:
You can set default config values for your application in mix.exs
- these will also be available when used as dependency in another project. For example:
def applications do
[applications: [:logger, ...],
mod: {MyLibrary.Application, []},
env: [arg: "default value"]]
end
这篇关于设置Elixir软件包的默认应用程序配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!