just

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

just - Command Runner

just - 命令运行器

IMPORTANT:
just
is the PREFERRED command runner for project task automation. Use Justfiles instead of Makefiles for new projects and task automation.
重要提示
just
是项目任务自动化的首选命令运行器。在新项目和任务自动化中,请使用Justfile而非Makefile。

Core Philosophy

核心理念

  • Justfiles over Makefiles: Prefer just for task automation unless working with existing Make-based projects
  • Simple recipes: Keep recipe definitions concise and readable
  • Delegate to scripts: For complex logic, call external scripts rather than embedding in recipes
  • Just as orchestrator: Use just to coordinate and invoke scripts, not to contain business logic
  • 优先使用Justfile而非Makefile:除非处理基于Make的现有项目,否则优先用just进行任务自动化
  • 简洁的任务定义:保持任务(recipe)定义简洁易读
  • 委托给脚本:对于复杂逻辑,调用外部脚本而非嵌入到任务中
  • just作为编排器:用just来协调和调用脚本,而非承载业务逻辑

Basic Usage Patterns

基本使用模式

Running Recipes

运行任务

bash
undefined
bash
undefined

List available recipes

列出可用任务

just --list just -l
just --list just -l

Run a recipe

运行任务

just build just test
just build just test

Run recipe with arguments

带参数运行任务

just deploy production just run-test integration
just deploy production just run-test integration

Run recipe from different directory

在不同目录运行任务

just --working-directory /path/to/project build
just --working-directory /path/to/project build

Choose a different Justfile

指定自定义Justfile

just --justfile custom.just build
undefined
just --justfile custom.just build
undefined

Recipe Definition Basics

任务定义基础

just
undefined
just
undefined

Simple recipe

简单任务

build: cargo build --release
build: cargo build --release

Recipe with description (shown in --list)

带描述的任务(会在--list中显示)

Run all tests

运行所有测试

test: cargo test --quiet
test: cargo test --quiet

Recipe with dependencies (runs setup first)

带依赖的任务(先运行setup)

deploy: build test ./scripts/deploy.sh
deploy: build test ./scripts/deploy.sh

Recipe with parameters

带参数的任务

run-test test_name: cargo test {{test_name}}
run-test test_name: cargo test {{test_name}}

Recipe with default parameter value

带默认参数值的任务

serve port="8080": python -m http.server {{port}}
serve port="8080": python -m http.server {{port}}

Recipe with multiple parameters

带多个参数的任务

deploy env branch="main": ./scripts/deploy.sh {{env}} {{branch}}
undefined
deploy env branch="main": ./scripts/deploy.sh {{env}} {{branch}}
undefined

Delegation Pattern (PREFERRED)

委托模式(推荐)

Good - Delegates to script:
just
undefined
推荐 - 委托给脚本:
just
undefined

Build the project

构建项目

build: ./scripts/build.sh
build: ./scripts/build.sh

Run integration tests

运行集成测试

test-integration: ./scripts/test-integration.sh
test-integration: ./scripts/test-integration.sh

Deploy to environment

部署到环境

deploy env: ./scripts/deploy.sh {{env}}

**Avoid - Complex logic in recipe:**
```just
deploy env: ./scripts/deploy.sh {{env}}

**避免 - 在任务中编写复杂逻辑:**
```just

❌ Too complex - should be in a script

❌ 过于复杂 - 应放到脚本中

deploy env: #!/usr/bin/env bash set -euo pipefail if [ "{{env}}" = "production" ]; then echo "Deploying to production..." git fetch origin git checkout main git pull docker build -t app:latest . docker push registry.example.com/app:latest kubectl apply -f k8s/production/ kubectl rollout status deployment/app else echo "Deploying to staging..." # ... many more lines ... fi

**Better - Simple orchestration:**
```just
deploy env: #!/usr/bin/env bash set -euo pipefail if [ "{{env}}" = "production" ]; then echo "Deploying to production..." git fetch origin git checkout main git pull docker build -t app:latest . docker push registry.example.com/app:latest kubectl apply -f k8s/production/ kubectl rollout status deployment/app else echo "Deploying to staging..." # ... 更多代码行 ... fi

**更优 - 简单编排:**
```just

Deploy to production

部署到生产环境

deploy-prod: ./scripts/deploy.sh production
deploy-prod: ./scripts/deploy.sh production

Deploy to staging

部署到预发布环境

deploy-staging: ./scripts/deploy.sh staging
deploy-staging: ./scripts/deploy.sh staging

Generic deploy with validation

带验证的通用部署

deploy env: ./scripts/validate-env.sh {{env}} ./scripts/deploy.sh {{env}}
undefined
deploy env: ./scripts/validate-env.sh {{env}} ./scripts/deploy.sh {{env}}
undefined

Recipe Features

任务特性

Dependencies

依赖关系

just
undefined
just
undefined

Recipe runs after its dependencies

任务在其依赖完成后运行

all: clean build test deploy
all: clean build test deploy

Dependencies run in order specified

依赖按指定顺序运行

deploy: build test package ./scripts/deploy.sh
deploy: build test package ./scripts/deploy.sh

Multiple dependency chains

多依赖链

full-deploy: lint build test docker-build docker-push k8s-deploy
undefined
full-deploy: lint build test docker-build docker-push k8s-deploy
undefined

Variables

变量

just
undefined
just
undefined

Set variables at top of file

在文件顶部设置变量

version := "1.2.3" registry := "registry.example.com" image := registry + "/app:" + version
version := "1.2.3" registry := "registry.example.com" image := registry + "/app:" + version

Use in recipes

在任务中使用变量

docker-build: docker build -t {{image}} .
docker-push: docker push {{image}}
docker-build: docker build -t {{image}} .
docker-push: docker push {{image}}

Environment variables

环境变量

deploy: API_KEY=$API_KEY ./scripts/deploy.sh
undefined
deploy: API_KEY=$API_KEY ./scripts/deploy.sh
undefined

Conditional Execution

条件执行

just
undefined
just
undefined

Use shell conditionals in scripts

在脚本中使用shell条件判断

test: ./scripts/test.sh
test: ./scripts/test.sh

Or delegate to script

或委托给脚本

check-and-test: ./scripts/check-and-test.sh
undefined
check-and-test: ./scripts/check-and-test.sh
undefined

Private Recipes

私有任务

just
undefined
just
undefined

Private recipe (not shown in --list, can't be run directly)

私有任务(不会在--list中显示,无法直接运行)

_setup: ./scripts/setup-env.sh
_setup: ./scripts/setup-env.sh

Public recipe uses private recipe

公开任务调用私有任务

deploy: _setup ./scripts/deploy.sh
undefined
deploy: _setup ./scripts/deploy.sh
undefined

Recipe Attributes

任务属性

just
undefined
just
undefined

Ignore errors (continue even if fails)

忽略错误(即使失败也继续执行)

[ignore-errors] lint: ./scripts/lint.sh
[ignore-errors] lint: ./scripts/lint.sh

Run in specific directory

在指定目录运行

[working-directory: 'frontend'] build-ui: npm run build
[working-directory: 'frontend'] build-ui: npm run build

Confirm before running

运行前确认

[confirm] deploy-prod: ./scripts/deploy.sh production
[confirm] deploy-prod: ./scripts/deploy.sh production

Don't print recipe before running

运行前不打印任务内容

[no-quiet] status: ./scripts/status.sh
undefined
[no-quiet] status: ./scripts/status.sh
undefined

Common Patterns

常见模式

Development Workflow

开发工作流

just
undefined
just
undefined

Default recipe (runs when you type 'just')

默认任务(输入'just'时运行)

default: check test
default: check test

Quick validation

快速验证

check: ./scripts/check.sh
check: ./scripts/check.sh

Run all tests

运行所有测试

test: ./scripts/test.sh
test: ./scripts/test.sh

Full CI workflow

完整CI工作流

ci: check test lint build
undefined
ci: check test lint build
undefined

Build Automation

构建自动化

just
undefined
just
undefined

Clean build artifacts

清理构建产物

clean: ./scripts/clean.sh
clean: ./scripts/clean.sh

Build project

构建项目

build: ./scripts/build.sh
build: ./scripts/build.sh

Full rebuild

完整重构建

rebuild: clean build
rebuild: clean build

Build with caching

带缓存的构建

build-cached: ./scripts/build.sh --cache
undefined
build-cached: ./scripts/build.sh --cache
undefined

Multi-Environment Deployment

多环境部署

just
undefined
just
undefined

Development environment

开发环境

dev: ./scripts/run-dev.sh
dev: ./scripts/run-dev.sh

Staging deployment

预发布环境部署

deploy-staging: ./scripts/deploy.sh staging
deploy-staging: ./scripts/deploy.sh staging

Production deployment (with confirmation)

生产环境部署(带确认)

[confirm] deploy-prod: ./scripts/deploy.sh production
[confirm] deploy-prod: ./scripts/deploy.sh production

Deploy to any environment

部署到任意环境

deploy env: ./scripts/validate-env.sh {{env}} ./scripts/deploy.sh {{env}}
undefined
deploy env: ./scripts/validate-env.sh {{env}} ./scripts/deploy.sh {{env}}
undefined

Docker Workflows

Docker工作流

just
undefined
just
undefined

Build Docker image

构建Docker镜像

docker-build: ./scripts/docker-build.sh
docker-build: ./scripts/docker-build.sh

Run container locally

本地运行容器

docker-run: ./scripts/docker-run.sh
docker-run: ./scripts/docker-run.sh

Push to registry

推送到镜像仓库

docker-push: docker-build ./scripts/docker-push.sh
docker-push: docker-build ./scripts/docker-push.sh

Complete Docker workflow

完整Docker工作流

docker: docker-build docker-push
undefined
docker: docker-build docker-push
undefined

Database Operations

数据库操作

just
undefined
just
undefined

Run migrations

运行迁移

migrate: ./scripts/db-migrate.sh
migrate: ./scripts/db-migrate.sh

Rollback migration

回滚迁移

migrate-rollback: ./scripts/db-rollback.sh
migrate-rollback: ./scripts/db-rollback.sh

Seed database

填充测试数据

seed: ./scripts/db-seed.sh
seed: ./scripts/db-seed.sh

Reset database (dev only)

重置数据库(仅开发环境)

[confirm] db-reset: ./scripts/db-reset.sh
undefined
[confirm] db-reset: ./scripts/db-reset.sh
undefined

Script Organization

脚本组织

Recommended Structure

推荐结构

project/
├── justfile
├── scripts/
│   ├── build.sh
│   ├── test.sh
│   ├── deploy.sh
│   ├── validate-env.sh
│   └── utils/
│       ├── common.sh
│       └── logging.sh
└── src/
    └── ...
project/
├── justfile
├── scripts/
│   ├── build.sh
│   ├── test.sh
│   ├── deploy.sh
│   ├── validate-env.sh
│   └── utils/
│       ├── common.sh
│       └── logging.sh
└── src/
    └── ...

Script Best Practices

脚本最佳实践

Make scripts executable:
bash
chmod +x scripts/*.sh
Use shebang in scripts:
bash
#!/usr/bin/env bash
set -euo pipefail
设置脚本可执行权限:
bash
chmod +x scripts/*.sh
在脚本中添加shebang:
bash
#!/usr/bin/env bash
set -euo pipefail

Script logic here

脚本逻辑


**Keep scripts focused:**
```bash

**保持脚本专注单一功能:**
```bash

Good - single purpose

推荐 - 单一功能

scripts/build.sh

scripts/build.sh

#!/usr/bin/env bash set -euo pipefail
echo "Building project..." cargo build --release

**Source common utilities:**
```bash
#!/usr/bin/env bash set -euo pipefail
echo "正在构建项目..." cargo build --release

**引入通用工具脚本:**
```bash

scripts/deploy.sh

scripts/deploy.sh

#!/usr/bin/env bash set -euo pipefail
source "$(dirname "$0")/utils/common.sh" source "$(dirname "$0")/utils/logging.sh"
log_info "Starting deployment..."
#!/usr/bin/env bash set -euo pipefail
source "$(dirname "$0")/utils/common.sh" source "$(dirname "$0")/utils/logging.sh"
log_info "开始部署..."

Deployment logic

部署逻辑

undefined
undefined

Advanced Features

高级特性

Command Evaluation

命令求值

just
undefined
just
undefined

Backticks execute shell commands

反引号执行shell命令

commit :=
git rev-parse --short HEAD
timestamp :=
date +%Y%m%d-%H%M%S
commit :=
git rev-parse --short HEAD
timestamp :=
date +%Y%m%d-%H%M%S

Use in recipes

在任务中使用

tag: echo "Tagging {{commit}} at {{timestamp}}"
undefined
tag: echo "为{{commit}}打标签,时间:{{timestamp}}"
undefined

Multi-line Constructs

多行结构

just
undefined
just
undefined

Multi-line variables

多行变量

help := ''' Available commands: build - Build the project test - Run tests deploy - Deploy to production '''
help := ''' 可用命令: build - 构建项目 test - 运行测试 deploy - 部署到生产环境 '''

Print help

打印帮助信息

help: @echo {{help}}
undefined
help: @echo {{help}}
undefined

Settings

设置项

just
undefined
just
undefined

Change shell (default is sh)

修改默认shell(默认是sh)

set shell := ["bash", "-c"]
set shell := ["bash", "-c"]

Make all recipes silent (@echo becomes echo)

所有任务静默运行(@echo变为echo)

set quiet := true
set quiet := true

Use PowerShell on Windows

在Windows上使用PowerShell

set windows-shell := ["powershell.exe", "-c"]
set windows-shell := ["powershell.exe", "-c"]

Use Python for recipes

使用Python执行任务

set shell := ["python3", "-c"]
undefined
set shell := ["python3", "-c"]
undefined

Imports

导入

just
undefined
just
undefined

Import recipes from other files

从其他文件导入任务

import 'ci.just' import 'deploy.just'
import 'ci.just' import 'deploy.just'

Use imported recipes

使用导入的任务

all: ci-check deploy-staging
undefined
all: ci-check deploy-staging
undefined

Integration with Other Tools

与其他工具集成

With Cargo (Rust)

与Cargo(Rust)集成

just
undefined
just
undefined

Rust development workflow

Rust开发工作流

check: cargo check --quiet
test: cargo test --quiet
clippy: cargo clippy
build: check test clippy cargo build --release
check: cargo check --quiet
test: cargo test --quiet
clippy: cargo clippy
build: check test clippy cargo build --release

Or delegate to script

或委托给脚本

rust-workflow: ./scripts/rust-workflow.sh
undefined
rust-workflow: ./scripts/rust-workflow.sh
undefined

With npm/pnpm/yarn

与npm/pnpm/yarn集成

just
undefined
just
undefined

Install dependencies

安装依赖

install: pnpm install
install: pnpm install

Run dev server

启动开发服务器

dev: pnpm run dev
dev: pnpm run dev

Build frontend

构建前端

build: pnpm run build
build: pnpm run build

Or use scripts

或使用脚本

ui-build: ./scripts/build-ui.sh
undefined
ui-build: ./scripts/build-ui.sh
undefined

With Docker

与Docker集成

just
undefined
just
undefined

Docker operations via scripts

通过脚本执行Docker操作

docker-build: ./scripts/docker/build.sh
docker-run: ./scripts/docker/run.sh
docker-compose-up: docker-compose up -d
undefined
docker-build: ./scripts/docker/build.sh
docker-run: ./scripts/docker/run.sh
docker-compose-up: docker-compose up -d
undefined

With Git

与Git集成

just
undefined
just
undefined

Git workflow helpers

Git工作流助手

sync: git fetch origin git pull
push: test git push origin main
release version: ./scripts/release.sh {{version}}
undefined
sync: git fetch origin git pull
push: test git push origin main
release version: ./scripts/release.sh {{version}}
undefined

Complete Workflow Examples

完整工作流示例

Workflow 1: Web Application

示例1:Web应用

just
undefined
just
undefined

Default - run dev server

默认任务 - 启动开发服务器

default: dev
default: dev

Install dependencies

安装依赖

install: ./scripts/install.sh
install: ./scripts/install.sh

Development server

开发服务器

dev: ./scripts/dev.sh
dev: ./scripts/dev.sh

Run tests

运行测试

test: ./scripts/test.sh
test: ./scripts/test.sh

Build for production

生产环境构建

build: ./scripts/build.sh
build: ./scripts/build.sh

Deploy to staging

部署到预发布环境

deploy-staging: test build ./scripts/deploy.sh staging
deploy-staging: test build ./scripts/deploy.sh staging

Deploy to production

部署到生产环境

[confirm] deploy-prod: test build ./scripts/deploy.sh production
undefined
[confirm] deploy-prod: test build ./scripts/deploy.sh production
undefined

Workflow 2: Rust Project

示例2:Rust项目

just
undefined
just
undefined

Default - check code

默认任务 - 代码检查

default: check
default: check

Fast check

快速检查

check: cargo check --quiet
check: cargo check --quiet

Run tests

运行测试

test: cargo test --quiet
test: cargo test --quiet

Lint code

代码检查

lint: cargo clippy
lint: cargo clippy

Full validation

完整验证

ci: test lint ./scripts/verify-formatting.sh
ci: test lint ./scripts/verify-formatting.sh

Build release

构建发布版本

build: cargo build --release
build: cargo build --release

Install binary

安装二进制文件

install: build cargo install --path .
undefined
install: build cargo install --path .
undefined

Workflow 3: Monorepo

示例3:单体仓库

just
undefined
just
undefined

Build all services

构建所有服务

build-all: ./scripts/build-all.sh
build-all: ./scripts/build-all.sh

Test all services

测试所有服务

test-all: ./scripts/test-all.sh
test-all: ./scripts/test-all.sh

Build specific service

构建指定服务

build-service service: ./scripts/build-service.sh {{service}}
build-service service: ./scripts/build-service.sh {{service}}

Deploy specific service

部署指定服务

deploy service env: ./scripts/deploy-service.sh {{service}} {{env}}
deploy service env: ./scripts/deploy-service.sh {{service}} {{env}}

Full deployment

完整部署

deploy-all env: test-all build-all ./scripts/deploy-all.sh {{env}}
undefined
deploy-all env: test-all build-all ./scripts/deploy-all.sh {{env}}
undefined

Common Commands Quick Reference

常用命令速查

bash
undefined
bash
undefined

List recipes

列出任务

just --list
just --list

Run recipe

运行任务

just <recipe>
just <recipe>

Run with arguments

带参数运行任务

just <recipe> arg1 arg2
just <recipe> arg1 arg2

Set variable

设置变量

just var=value recipe
just var=value recipe

Choose Justfile

指定Justfile

just --justfile path/to/justfile recipe
just --justfile path/to/justfile recipe

Work in different directory

在指定目录运行

just --working-directory /path recipe
just --working-directory /path recipe

Dry run (show what would run)

空运行(显示将要执行的命令)

just --dry-run recipe
just --dry-run recipe

Verbose output

详细输出

just --verbose recipe
just --verbose recipe

Choose recipe interactively

交互式选择任务

just --choose
undefined
just --choose
undefined

Best Practices Summary

最佳实践总结

  1. Keep recipes simple: 1-3 lines max; delegate complex logic to scripts
  2. Use descriptive names:
    deploy-prod
    not
    dp
    ,
    test-integration
    not
    ti
  3. Add comments: Describe what each recipe does
  4. Organize scripts: Keep scripts in
    scripts/
    directory with logical subdirectories
  5. Make scripts executable: Use
    chmod +x scripts/*.sh
  6. Use dependencies: Chain recipes with dependencies rather than duplicating commands
  7. Confirm destructive operations: Use
    [confirm]
    for deployments, database resets, etc.
  8. Private recipes for setup: Use
    _recipe
    naming for internal setup steps
  9. Default recipe: Provide sensible default (usually
    dev
    or
    check
    )
  10. Document in comments: Add comments explaining complex workflows
  1. 保持任务简洁:最多1-3行;将复杂逻辑委托给脚本
  2. 使用描述性名称:用
    deploy-prod
    而非
    dp
    ,用
    test-integration
    而非
    ti
  3. 添加注释:描述每个任务的作用
  4. 组织脚本:将脚本放在
    scripts/
    目录,并按逻辑划分子目录
  5. 设置脚本可执行权限:使用
    chmod +x scripts/*.sh
  6. 使用依赖关系:通过依赖链串联任务,而非重复命令
  7. 对破坏性操作添加确认:对部署、数据库重置等操作使用
    [confirm]
  8. 用私有任务做准备工作:用
    _recipe
    命名格式作为内部准备步骤
  9. 设置默认任务:提供合理的默认任务(通常是
    dev
    check
  10. 在注释中记录:添加注释说明复杂工作流

When to Use Scripts vs. Recipes

何时使用脚本vs任务

Use Recipe Directly:

直接使用任务:

  • Single command execution
  • Simple chaining (3 commands or less)
  • Setting environment variables for a command
  • Quick aliases
  • 单命令执行
  • 简单串联(3个命令以内)
  • 为命令设置环境变量
  • 快速别名

Use Script (PREFERRED for most cases):

使用脚本(大多数情况推荐):

  • Complex conditional logic
  • Multiple steps with error handling
  • Reusable logic across recipes
  • Long command sequences (>3 commands)
  • Anything requiring significant bash scripting
  • 复杂条件逻辑
  • 带错误处理的多步骤操作
  • 可在多个任务间复用的逻辑
  • 长命令序列(超过3个命令)
  • 任何需要大量bash脚本的场景

Justfile Template

Justfile模板

just
undefined
just
undefined

Set shell for all recipes

为所有任务设置shell

set shell := ["bash", "-c"]
set shell := ["bash", "-c"]

Variables

变量

version := "1.0.0"
version := "1.0.0"

Default recipe

默认任务

default: check
default: check

List all recipes

列出所有任务

help: @just --list
help: @just --list

Development

开发环境

dev: ./scripts/dev.sh
dev: ./scripts/dev.sh

Testing

测试

test: ./scripts/test.sh
test: ./scripts/test.sh

Build

构建

build: ./scripts/build.sh
build: ./scripts/build.sh

Full CI check

完整CI检查

ci: test build ./scripts/verify.sh
ci: test build ./scripts/verify.sh

Deploy to staging

部署到预发布环境

deploy-staging: ci ./scripts/deploy.sh staging
deploy-staging: ci ./scripts/deploy.sh staging

Deploy to production

部署到生产环境

[confirm] deploy-prod: ci ./scripts/deploy.sh production
[confirm] deploy-prod: ci ./scripts/deploy.sh production

Clean build artifacts

清理构建产物

clean: ./scripts/clean.sh
undefined
clean: ./scripts/clean.sh
undefined

Migration from Make

从Make迁移

Key Differences from Make

与Make的核心差异

  1. Not a build system: just is for running commands, not tracking file dependencies
  2. Recipes always run: Unlike Make targets, recipes run every time (no file timestamp checking)
  3. Simpler syntax: No need for
    .PHONY
    , tabs vs. spaces issues, etc.
  4. Better error messages: Clear, helpful error output
  1. 不是构建系统:just用于运行命令,而非跟踪文件依赖
  2. 任务总是会运行:与Make目标不同,任务每次都会运行(不检查文件时间戳)
  3. 更简单的语法:无需
    .PHONY
    ,无制表符与空格的问题等
  4. 更友好的错误提示:清晰、有用的错误输出

Converting Makefile to Justfile

将Makefile转换为Justfile

Makefile:
make
.PHONY: build test deploy

build:
	cargo build --release

test:
	cargo test

deploy: build test
	./deploy.sh
Justfile:
just
build:
    cargo build --release

test:
    cargo test

deploy: build test
    ./scripts/deploy.sh
Makefile:
make
.PHONY: build test deploy

build:
	cargo build --release

test:
	cargo test

deploy: build test
	./deploy.sh
Justfile:
just
build:
    cargo build --release

test:
    cargo test

deploy: build test
    ./scripts/deploy.sh

Resources

资源