如何安装 just 命令运行器:编写 justfile、运行 recipe 与 make 对比

安装 just,写 justfile 跑 recipe,并知道何时优于 Makefile。

仓库里总有「先 lint 再 test 再 build」这类重复命令,写在 README 里没人看,塞进 package.json 的 scripts 又和语言绑定。just 是 Casey Rodarmor 写的命令运行器:在项目根放一个 justfile,用 just 跑具名 recipe,语法比 Make 友好、跨平台也更省心。本教程覆盖 macOS/Linux 安装 just、编写 justfile、运行 recipe,以及与 make 的对照,面向 hereink 读者常用的多仓库终端工作流。

检索词是如何安装 just 命令运行器并写 justfile 运行 recipe 对比 make。读完应能安装 just、写第一条 recipe、用 just --list 查看任务,并理解何时用 just 替代 Makefile。

安装brew · cargo · mise
配置justfile · recipe
运行just · --list
install justwrite justfilejust test

just 解决什么问题

可发现的项目命令

新同事不用翻五个文档:just --list 列出所有 recipe 和注释。比「复制粘贴 Slack 里的 curl」更稳。

比 shell 脚本更轻

recipe 默认在项目根执行,可设变量、依赖其他 recipe、传参。不必为每个任务写 ./scripts/foo.sh 并记得 chmod +x。

上游:github.com/casey/just,文档 just.systems。安装后运行 just --version 确认。

安装 just

  1. 1
    macOS(Homebrew)

    brew install just。验证:which just 与 just --version。

  2. 2
    Linux(发行版包)

    Arch:sudo pacman -S just。Debian/Ubuntu 若仓库较旧,可用 cargo install just 或官方预编译包(见 just.systems 安装页)。

  3. 3
    Rust 工具链

    已装 Rust:cargo install just。确保 ~/.cargo/bin 在 PATH 里。

  4. 4
    版本管理器

    mise use -g just@latest 或 asdf plugin add just(若团队已用 asdf/mise)。

编写 justfile

在项目根创建 justfile(或 .justfile)。最小示例:

# 默认 recipe:只输入 just 时运行
default:
    just --list

# 带注释,会出现在 just --list
# 跑单元测试
test:
    cargo test

# 依赖其他 recipe
check: lint test
    echo "all green"

lint:
    cargo clippy -- -D warnings

常用语法要点:

  • recipe 名:name: 下一行起缩进命令(空格,不是 tab)。
  • 参数:deploy env: 然后命令里用 {{env}}。
  • 变量:文件顶部 port := "3000",recipe 内 {{port}}。
  • shebang recipe:以 #!/usr/bin/env bash 开头可写多行 shell。
port := "8080"

# 启动本地服务
serve:
    python -m http.server {{port}}

# 带默认值的可选参数
greet name="world":
    echo "hello {{name}}"

运行 recipe

cd ~/projects/my-app
just --list          # 列出 recipe
just test            # 运行 test recipe
just check           # 先 lint 再 test(依赖顺序)
just greet Alice     # 传参
just                 # 若定义了 default recipe

just 会在包含 justfile 的目录(或父目录)查找文件;也可用 just --justfile path/to/justfile recipe。加 -n / --dry-run 只打印将要执行的命令,不真正运行。

与 make 对照

方面justmake
主要用途跑项目命令、编排脚本编译依赖图、构建产物
语法recipe + 现代变量 {{var}}target: prereq + tab 规则
Windows官方支持较好(含 PowerShell 等文档)常依赖 GNU make / WSL
文件时间戳不默认当构建系统核心:比 mtime 决定是否重建
学习曲线README 级即可上手大型 Makefile 难维护
适合 just
统一 test / lint / dev 入口;多语言仓库不想绑 npm scripts;想要比 Make 更可读的 task 列表。
仍用 make
C/C++ 增量编译、复杂依赖图、已有成熟 Makefile 且团队熟悉。
just 和 npm scripts / task 重复吗?

Node 项目可以只用 npm scripts;just 适合 polyglot 仓库(Go + Python + 生成步骤)或希望命令与包管理器解耦。两者可并存:just recipe 里调用 npm run build。

recipe 里 cd 子目录有效吗?

每条 recipe 默认在 justfile 所在目录启动;recipe 内 cd 只影响该 recipe 后续行。跨目录任务可用 just --working-directory subdir recipe 或 shebang 脚本。

能调用 .env 里的变量吗?

just 可 set dotenv-load(见官方手册)自动加载 .env。敏感值仍建议 direnv 或 CI secret,不要提交明文。

命令对齐后画一张图

本地 just dev 跑通后,服务边界仍适合摊在同一张白板上。见 如何创建空间。

即刻开会