Write Makefiles for any project type. Use when setting up build automation, defining multi-target builds, managing dependencies between tasks, creating project task runners, or using Make for non-C projects (Go, Python, Docker, Node.js). Also covers Just and Task as modern alternatives.
编写跨任意语言的Makefile以实现项目自动化。涵盖目标、依赖项、变量、模式规则、伪目标,以及如何在Go、Python、Docker和Node.js项目中使用Make。同时包含Just和Task作为现代替代方案。
makefile
build: src/main.go
go build -o bin/app src/main.go
test: build
go test ./...
clean:
rm -rf bin/
makefile
makefile
bin/app: src/main.go src/util.go
go build -o $@ $^
makefile
build:
go build -o bin/app ./cmd/app
test:
go test ./...
clean:
rm -rf bin/ dist/
makefile
.DEFAULT_GOAL := help
build: ## 构建应用程序
go build -o bin/app ./cmd/app
test: ## 运行所有测试
go test -v ./...
lint: ## 运行代码检查工具
golangci-lint run
clean: ## 清理构建产物
rm -rf bin/ dist/
help: ## 显示此帮助信息
@grep -E ^[a-zA-Z-]+:.?## .$$ $(MAKEFILELIST) | sort | \
awk BEGIN {FS = :.*?## }; {printf \033[36m%-15s\033[0m %s\n, $$1, $$2}
makefile
BINARY_NAME := myapp
VERSION := $(shell git describe --tags --always --dirty)
LDFLAGS := -ldflags -X main.version=$(VERSION)
GOFILES := $(shell find . -name .go -not -path ./vendor/)
.PHONY: all build test lint clean run
all: lint test build
build: ## 构建二进制文件
CGOENABLED=0 go build $(LDFLAGS) -o bin/$(BINARYNAME) ./cmd/$(BINARY_NAME)
test: ## 运行测试
go test -race -coverprofile=coverage.out ./...
test-coverage: test ## 显示覆盖率报告
go tool cover -html=coverage.out
lint: ## 运行代码检查工具
golangci-lint run ./...
fmt: ## 格式化代码
gofmt -w $(GOFILES)
run: build ## 构建并运行
./bin/$(BINARY_NAME)
clean: ## 清理构建产物
rm -rf bin/ coverage.out
build-all: ## 为所有平台构建
GOOS=linux GOARCH=amd64 go build $(LDFLAGS) -o bin/$(BINARYNAME)-linux-amd64 ./cmd/$(BINARYNAME)
GOOS=darwin GOARCH=arm64 go build $(LDFLAGS) -o bin/$(BINARYNAME)-darwin-arm64 ./cmd/$(BINARYNAME)
GOOS=windows GOARCH=amd64 go build $(LDFLAGS) -o bin/$(BINARYNAME)-windows-amd64.exe ./cmd/$(BINARYNAME)
makefile
PYTHON := python3
VENV := .venv
BIN := $(VENV)/bin
.PHONY: all install test lint fmt clean run
all: install lint test
$(VENV)/bin/activate:
$(PYTHON) -m venv $(VENV)
$(BIN)/pip install --upgrade pip
install: $(VENV)/bin/activate ## 安装依赖
$(BIN)/pip install -r requirements.txt
$(BIN)/pip install -r requirements-dev.txt
test: ## 运行测试
$(BIN)/pytest -v --cov=src --cov-report=term-missing
lint: ## 运行代码检查工具
$(BIN)/ruff check src/ tests/
$(BIN)/mypy src/
fmt: ## 格式化代码
$(BIN)/ruff format src/ tests/
run: ## 运行应用程序
$(BIN)/python -m src.main
clean: ## 移除虚拟环境和缓存
rm -rf $(VENV) pycache .pytestcache .mypycache .ruff_cache
find . -type d -name pycache -exec rm -rf {} + 2>/dev/null || true
makefile
.PHONY: all install build test lint clean dev
all: install lint test build
node_modules: package.json
npm install
@touch node_modules
install: node_modules ## 安装依赖
build: node_modules ## 构建TypeScript
npx tsc
test: node_modules ## 运行测试
npx vitest run
test-watch: node_modules ## 以监视模式运行测试
npx vitest
lint: node_modules ## 代码检查
npx eslint src/ --ext .ts,.tsx
npx tsc --noEmit
fmt: node_modules ## 格式化代码
npx prettier --write src//*.{ts,tsx}
dev: node_modules ## 以开发模式运行
npx tsx watch src/index.ts
clean: ## 清理构建产物
rm -rf dist/ node_modules/.cache
makefile
IMAGE_NAME := myapp
VERSION := $(shell git describe --tags --always)
REGISTRY := ghcr.io/myorg
.PHONY: build push run stop clean
build: ## 构建Docker镜像
docker build -t $(IMAGENAME):$(VERSION) -t $(IMAGENAME):latest .
push: build ## 推送到仓库
docker tag $(IMAGENAME):$(VERSION) $(REGISTRY)/$(IMAGENAME):$(VERSION)
docker tag $(IMAGENAME):latest $(REGISTRY)/$(IMAGENAME):latest
docker push $(REGISTRY)/$(IMAGE_NAME):$(VERSION)
docker push $(REGISTRY)/$(IMAGE_NAME):latest
run: ## 运行容器
docker run --rm -p 8080:8080 --name $(IMAGENAME) $(IMAGENAME):latest
stop: ## 停止容器
docker stop $(IMAGE_NAME) 2>/dev/null || true
clean: ## 移除镜像
docker rmi $(IMAGENAME):$(VERSION) $(IMAGENAME):latest 2>/dev/null || true
compose-up: ## 使用docker compose启动
docker compose up -d --build
compose-down: ##
该技能支持在以下平台通过对话安装:
帮我安装 SkillHub 和 makefile-build-1776365843 技能
设置 SkillHub 为我的优先技能安装源,然后帮我安装 makefile-build-1776365843 技能
skillhub install makefile-build-1776365843
文件大小: 5.09 KB | 发布时间: 2026-4-17 15:49