Just 命令运行器¶
一句话说明: Just 是一个轻量级命令运行器,用简洁的
justfile语法定义项目常用命令,是 Makefile 用于非编译任务时的理想替代品。
为什么要学¶
- 比 Makefile 简洁 — 不需要
.PHONY、不关心文件时间戳、不需要 tab 缩进 - 专注命令运行 — 不是构建系统,就是命令的快捷方式,概念清晰
- 跨平台 — Windows/macOS/Linux 一致运行,Rust 编写的单二进制
- 参数与变量 — 支持命令参数、环境变量、条件逻辑
- 项目标准化 — 一个
justfile统一团队的开发/测试/部署命令
核心概念详解¶
justfile vs Makefile¶
| 特性 | Just | Make |
|---|---|---|
| 设计目标 | 命令运行器 | 构建系统 |
| 缩进要求 | 空格或 tab 均可 | 必须 tab |
| 文件依赖检测 | 无 | 有(核心特性) |
.PHONY | 不需要 | 常用目标必须声明 |
| 参数传递 | 原生支持 | 复杂 |
| 默认 Shell | sh(可配置) | sh |
| 跨平台 | 好 | 差(Windows 困难) |
| 错误处理 | 默认遇错停止 | 需 -e |
核心语法¶
| 语法 | 说明 | 示例 |
|---|---|---|
recipe: | 定义命令(recipe) | build: |
@recipe: | 静默执行(不打印命令) | @test: |
recipe arg: | 带参数 | deploy env: |
recipe arg="default": | 默认参数 | run port="8080": |
variable := "value" | 变量赋值 | name := "app" |
# comment | 注释 | # 部署到生产 |
recipe: dep1 dep2 | 依赖关系 | deploy: test lint |
安装与配置¶
安装¶
# macOS
brew install just
# Linux (各发行版)
# Arch
pacman -S just
# Ubuntu/Debian (需要 prebuilt-mpr 或 cargo)
cargo install just
# Windows
scoop install just
# 或
winget install Casey.Just
# 通用(cargo)
cargo install just
Shell 补全¶
# Bash
just --completions bash > ~/.local/share/bash-completion/completions/just
# Zsh
just --completions zsh > ~/.zfunc/_just
# Fish
just --completions fish > ~/.config/fish/completions/just.fish
验证¶
快速上手¶
创建 justfile(无扩展名):
# 项目命令合集
# 默认命令(just 不带参数时运行)
default:
@just --list
# 安装依赖
install:
pip install -r requirements.txt
# 运行开发服务器
dev:
python manage.py runserver
# 运行测试
test *args:
pytest {{args}}
# 格式化代码
fmt:
ruff format .
ruff check --fix .
# 构建 Docker 镜像
build:
docker build -t myapp .
# 部署(依赖 test 和 build)
deploy: test build
docker push myapp:latest
使用:
just # 运行 default(列出所有命令)
just install # 运行 install
just test # 运行所有测试
just test -k "test_login" # 传递参数
just deploy # 先 test,再 build,最后 deploy
进阶用法¶
1. 变量与环境变量¶
# 变量
version := "1.0.0"
app_name := "myapp"
# 环境变量
export DATABASE_URL := "postgres://localhost/mydb"
# 从 .env 文件加载
set dotenv-load
# 从命令输出获取
git_hash := `git rev-parse --short HEAD`
date := `date +%Y%m%d`
# 使用变量
build:
docker build -t {{app_name}}:{{version}}-{{git_hash}} .
# 环境变量传入
greet name="World":
echo "Hello, {{name}}!"
2. 条件与平台检测¶
# 操作系统检测
os := os()
install:
#!/usr/bin/env bash
if [ "{{os}}" = "macos" ]; then
brew install ripgrep
elif [ "{{os}}" = "linux" ]; then
sudo apt install ripgrep
fi
# 条件表达式
db_url := if env("CI", "") == "true" {
"postgres://ci-db/test"
} else {
"postgres://localhost/dev"
}
3. 多行脚本与 Shebang¶
# 使用 shebang(每行不独立执行,整体作为脚本)
analyze:
#!/usr/bin/env python3
import pandas as pd
df = pd.read_csv("data.csv")
print(f"行数: {len(df)}")
print(f"列: {list(df.columns)}")
# 使用其他语言
generate:
#!/usr/bin/env node
const data = Array.from({length: 10}, (_, i) => ({id: i}));
console.log(JSON.stringify(data, null, 2));
4. 参数与选项¶
# 位置参数
deploy env target="web":
echo "部署 {{target}} 到 {{env}}"
# 可变参数
test *args:
pytest {{args}}
# 必需参数 + 可选参数
build profile="release" target="":
cargo build --profile {{profile}} {{if target != "" { "--target " + target } else { "" } }}
使用:
5. 确认提示与私有命令¶
# 执行前确认
[confirm("确定要清除所有数据?")]
reset-db:
psql -c "DROP DATABASE myapp; CREATE DATABASE myapp;"
# 私有命令(不显示在 --list 中)
[private]
_setup:
mkdir -p build/
# 依赖私有命令
build: _setup
go build -o build/app .
6. 命令分组与文档¶
# 分组显示
[group: 'development']
dev:
npm run dev
[group: 'development']
test:
npm test
[group: 'deployment']
deploy:
./scripts/deploy.sh
[group: 'deployment']
rollback:
./scripts/rollback.sh
# 文档字符串
# 运行所有检查并部署到生产环境
[doc: '执行完整的部署流程']
ship: test lint build deploy
7. 跨文件引用¶
# 导入其他 justfile
import "ci.just"
import "docker.just"
# 或使用模块
mod docker
# 创建 docker.just 文件,命令通过 `just docker build` 调用
8. Windows 兼容¶
# 设置 Windows 使用 PowerShell
set windows-shell := ["powershell.exe", "-NoLogo", "-Command"]
# 或平台特定命令
[windows]
open:
start index.html
[linux]
open:
xdg-open index.html
[macos]
open:
open index.html
常见问题与排错¶
Q: "error: Justfile does not contain recipe"¶
确认命令名拼写正确:
Q: 变量中的特殊字符¶
Q: 如何在子目录中运行 just¶
或指定 justfile 路径:
Q: 命令执行出错但 just 不停止¶
Q: 如何传递含空格的参数¶
参考资源¶
- 官方手册:https://just.systems/man/en/
- GitHub 仓库:https://github.com/casey/just
- 速查表:https://cheatography.com/linux-china/cheat-sheets/justfile/
- VS Code 插件:搜索 "Just" 语法高亮
- 示例集合:https://github.com/casey/just/tree/master/examples
- 博文介绍:https://just.systems/man/en/introduction.html