elixir 使用mix umbrella 模块化项目

备注:

   项目比较大, 模块比较多,一般使用mix 的方式是大家进行文件夹的划分,但是使用mix 的umbrella 可能会更方便

1. 安装

默认安装elixir 的时候已经包含了这个功能
2. 基本使用
a. 创建根项目

mix new <projectname> --umbrella  

生成的项目如下:

* creating README.md
* creating .formatter.exs
* creating .gitignore
* creating mix.exs
* creating apps
* creating config
* creating config/config.exs

Your umbrella project was created successfully.
Inside your project, you will find an apps/ directory
where you can create and host many apps:

    cd opsplatform
    cd apps
    mix new my_app

Commands like "mix compile" and "mix test" when executed
in the umbrella project root will automatically run
for each application in the apps/ directory.

b. 添加子项目

cd <projectname>/apps  我的是opsplatform
cd opsplatform/apps/
mix new platformuserlogin
mix new platformrunnuer
mix new platformdao

项目代码结构如下:

├── apps
│   ├── platformdao
│   │   ├── config
│   │   ├── lib
│   │   └── test
│   ├── platformrunnuer
│   │   ├── config
│   │   ├── lib
│   │   └── test
│   └── platformuserlogin
│       ├── config
│       ├── lib
│       └── test
└── config

d.  编译

mix compile  # 根项目,进入子模块进行编译也是可以的

e.  模块引用(比如platformrunnuer 需要platformdao)

进入platformrunnuer 编辑 mix.exs

defp deps do
    [
      # {:dep_from_hexpm, "~> 0.3.0"},
     {:platformdao, in_umbrella: true}
      # {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"},
      # {:sibling_app_in_umbrella, in_umbrella: true},
    ]
  end
3. 子模块说明
mix.exs

defmodule Platformdao.MixProject do
  use Mix.Project

 # 处理引用的是根目录的,对于配置信息以及依赖可以方便的共享
  def project do
    [
      app: :platformdao,
      version: "0.1.0",
      build_path: "../../_build",
      config_path: "../../config/config.exs",
      deps_path: "../../deps",
      lockfile: "../../mix.lock",
      elixir: "~> 1.6",
      start_permanent: Mix.env() == :prod,
      deps: deps()
    ]
  end

  # Run "mix help compile.app" to learn about applications.
  def application do
    [
      extra_applications: [:logger]
    ]
  end

  # Run "mix help deps" to learn about dependencies.
  defp deps do
    [
      # {:dep_from_hexpm, "~> 0.3.0"},
      # {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"},
      # {:sibling_app_in_umbrella, in_umbrella: true},
    ]
  end
end
4. 扩展
一般来说我们在使用umbrella 的时候,同时会使用distillery && edeliver
一个负责构建,一个负责部署,还是比较方便的
5. 参考资料
https://hex.pm/packages/edeliver
https://hex.pm/packages/distillery
https://hexdocs.pm/mix/Mix.html
https://github.com/wojtekmach/acme_bank
原文地址:https://www.cnblogs.com/rongfengliang/p/8743875.html