Makefile 模板
按语言生成 Makefile:install / build / test / lint / run / clean 六个目标,自动登记 .PHONY 与变量,可选自文档化的 help 目标,直接 make 就能列出全部命令。
浏览器本地运行所有计算都在你的浏览器里完成,数据不会离开本机。
项目设置
install / build / test / lint 走 package.json 脚本,run 对应 dev 脚本。
预览
make help列出全部可用目标make install安装依赖make build构建项目make test运行测试make lint代码检查make run本地运行make clean清理构建产物
# Makefile — generated with UniKit
PROJECT := app
PKG := pnpm
.DEFAULT_GOAL := help
.PHONY: help install build test lint run clean
help: ## Show available targets
@echo "$(PROJECT): available targets"
@grep -E '^[a-zA-Z0-9_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf " \033[36m%-14s\033[0m %s\n", $$1, $$2}'
install: ## Install dependencies
pnpm install
build: ## Build the project
pnpm run build
test: ## Run the test suite
pnpm test
lint: ## Run the linter
pnpm run lint
run: ## Run the project locally
pnpm run dev
clean: ## Remove build artifacts
rm -rf dist coverage
配方行用 Tab 缩进,目标全部登记到 .PHONY;help 目标靠 ## 注释自文档化,直接 make 即可列出全部命令。
这个工具能做什么
- 新仓库补一份 Makefile:把安装依赖、构建、测试、lint、运行、清理统一成 make install / make build 这样的固定入口。
- 多语言项目里给每种语言生成对应的命令写法,不用每次回想 gofmt 要不要跟 go vet、pytest 是 python -m 还是直接调用。
- 团队约定统一入口:新人只要 make help 就能看到全部可用命令,不必读 README 找脚本。
- CI 里直接调用 make test / make lint,本地与流水线跑的是同一套命令,避免「本地能过 CI 挂掉」。
示例
输入
语言 Node.js、包管理器 pnpm、项目名 app、开启 help 目标
输出
# Makefile — generated with UniKit
PROJECT := app
PKG := pnpm
.DEFAULT_GOAL := help
.PHONY: help install build test lint run clean
help: ## Show available targets
@echo "$(PROJECT): available targets"
@grep -E '^[a-zA-Z0-9_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf " \033[36m%-14s\033[0m %s\n", $$1, $$2}'
install: ## Install dependencies
pnpm install
build: ## Build the project
pnpm run build
test: ## Run the test suite
pnpm test
lint: ## Run the linter
pnpm run lint
run: ## Run the project locally
pnpm run dev
clean: ## Remove build artifacts
rm -rf dist coverage
配方行必须以 Tab 开头(复制时注意别被编辑器换成空格);$$1、$$2 是 Makefile 里写 $1、$2 的转义写法。
常见问题
为什么每行命令前面是 Tab?
GNU Make 要求配方行用 Tab 缩进,用空格会直接报 missing separator。生成器已经写死 Tab,但粘贴到某些编辑器时如果配置了「Tab 转空格」需要关掉,或者把 .editorconfig 里的 indent_style 设成 tab。
help 目标是怎么列出命令的?
每个目标后面用 ## 写一句说明,help 目标用 grep 抓取所有形如 target: ## 说明 的行,再交给 awk 打印。新增目标时只要补一句 ## 注释,make help 就会自动带上它。
变量为什么写成 ?= 或 :=?
:= 是立即求值,适合内部变量;?= 是「没有外部赋值时才生效」,适合让用户用环境变量覆盖,例如 PYTHON=python3.12 make test。生成器对工具命令(python3、cargo、mvn)用 ?=,对项目名这类固定值用 :=。
Node 项目里 run 目标跑的是什么?
run 对应 package.json 里的 dev 脚本,也就是本地开发模式;生产启动请另加 start 目标或直接在容器里执行。包管理器决定调用方式:yarn 直接跟脚本名,npm / pnpm / bun 用 run。
生成的 Makefile 需要改哪些地方?
通常只需要确认三处:package.json 里真的有 build / lint / dev 脚本、Python 项目的 requirements.txt 与模块入口名、Java 项目打出来的 jar 名是否与 JAR 变量一致。改完后 make help 能立刻验证。
关键词:makefilemakefile generatorMakefile 模板make help.PHONYmake targetsgnu makebuild automation构建脚本自动化任务make installmake build