跳到主內容
UniKit

Makefile 範本

依語言產生 Makefile:install / build / test / lint / run / clean 六個目標,自動登記 .PHONY 與變數,可選用自我說明的 help 目標,直接 make 就能列出全部指令。

瀏覽器本機執行所有計算都在你的瀏覽器裡完成,資料不會離開本機。

專案設定

install / build / test / lint 走 package.json 指令碼,run 對應 dev 指令碼。

預覽

目標數
7
行數
30
目標一覽
  • make help列出全部可用目標
  • make install安裝相依套件
  • make build建置專案
  • make test執行測試
  • make lint程式碼檢查
  • make run本機執行
  • make clean清理建置產物
Makefile
# 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 能立刻驗證。

關鍵字:Makefile 模板构建脚本自动化任务makefilemakefile generatormake help.PHONYmake targetsgnu makebuild automationmake installmake build

同類工具