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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
2014-04-08 web 分享代码片段
2014-04-08 Microsoft OWIN