Taskfile: 使用介绍
已于 2025年09月15日 10:16 修改
访问次数:0
Taskfile 是一个 现代化的任务运行工具(相当于 Makefile 的替代品),常用在 Go / Python / Docker / 前端项目里。它的核心就是:把一堆命令写到 Taskfile.yml,以后只要 task xxx 就能运行,不用每次去记复杂命令。
我给你分步骤介绍一下怎么用:
1️⃣ 安装 Task
不同系统安装方式:
macOS / Linux (Homebrew):
brew install go-task/tap/go-task
Linux (脚本):
sh -c "$(curl --location https://taskfile.dev/install.sh)" -- -d
Windows (Scoop):
scoop install task
安装完以后测试:
task --version
2️⃣ 创建 Taskfile
在项目根目录新建 Taskfile.yml:
version: '3'
tasks:
hello:
desc: "打印 Hello World"
cmds:
- echo "Hello, World!"
lint:
desc: "运行 flake8"
cmds:
- flake8 .
typecheck:
desc: "运行 mypy"
cmds:
- mypy .
test:
desc: "运行 pytest"
cmds:
- pytest -v
3️⃣ 运行任务
- 查看所有任务: task --list
- 运行指定任务: task hello task lint
- 串联运行多个: task lint typecheck test
- 设置默认任务(用户直接输入 task 时执行): tasks: default: desc: "默认执行 lint + typecheck + test" cmds: - task: lint - task: typecheck - task: test
4️⃣ 高级用法
- 带参数 tasks: greet: desc: "打招呼" cmds: - echo "Hello, {{.NAME}}" vars: NAME: stranger 运行时: task greet NAME=tongqing
- 依赖任务 tasks: build: desc: "构建项目" deps: [lint, typecheck, test] cmds: - echo "✅ Build completed"
✅ 这样你就可以用 Taskfile.yml 把 flake8、mypy、pytest、docker build、部署等命令全都管理起来。
要不要我帮你写一个 结合 flake8 + mypy + pytest 的 Taskfile 模板?这样你 Python 项目直接用就行。
评论(0)