本文目录导读:

自动化脚本清理构建产物有多种方法,具体取决于你使用的构建工具和项目类型,以下是几种常见且高效的实现方式:
使用构建工具自带功能
Maven(Java项目)
<!-- pom.xml 中添加插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-clean-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<id>auto-clean</id>
<phase>clean</phase>
<goals>
<goal>clean</goal>
</goals>
</execution>
</executions>
</plugin>
执行:mvn clean package 或 mvn clean deploy
Gradle(Java/Kotlin项目)
// build.gradle
clean {
delete 'build'
delete 'out'
}
// 自定义清理任务
task cleanExtra(type: Delete) {
delete 'dist', 'temp'
}
执行:gradle clean build 或 gradle cleanExtra
npm/yarn(前端项目)
// package.json
{
"scripts": {
"clean": "rm -rf dist build .next out node_modules/.cache",
"build": "npm run clean && next build"
}
}
Shell/Bash脚本(适用于通用场景)
Linux/Mac
#!/bin/bash # clean_build.sh echo "开始清理构建产物..." # 清理特定目录 rm -rf dist/ rm -rf build/ rm -rf target/ rm -rf .next/ rm -rf out/ rm -rf coverage/ rm -rf *.log # 清理node_modules缓存(可选) # rm -rf node_modules/ # 清理编译缓存 rm -rf .cache/ rm -rf .gradle/ rm -rf .m2/repository/(谨慎使用,会删除所有Maven本地依赖) echo "✅ 构建产物已清理完成"
Windows (PowerShell)
# clean_build.ps1 Write-Host "开始清理构建产物..." # 删除特定目录 Remove-Item -Recurse -Force "dist", "build", "target", ".next", "out" -ErrorAction SilentlyContinue # 删除日志文件 Remove-Item -Filter "*.log" -Force -ErrorAction SilentlyContinue # 清理编译缓存(可选) # Remove-Item -Recurse -Force "node_modules" -ErrorAction SilentlyContinue Write-Host "✅ 构建产物已清理完成"
CI/CD流水线集成
GitHub Actions
# .github/workflows/cleanup.yml
name: Clean Build Artifacts
on:
push:
branches: [main]
pull_request:
branches: [main]
jobs:
clean:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Clean build artifacts
run: |
rm -rf dist/ build/ target/
find . -type d -name "node_modules" -exec rm -rf {} + 2>/dev/null || true
- name: Verify cleanup
run: |
if [ -d "dist" ] || [ -d "build" ]; then
echo "❌ 清理失败"
exit 1
else
echo "✅ 清理成功"
fi
GitLab CI
# .gitlab-ci.yml
stages:
- clean
- build
clean_job:
stage: clean
script:
- rm -rf dist/ build/ target/
artifacts:
expire_in: 1 hour
only:
- main
Jenkins
// Jenkinsfile
pipeline {
agent any
stages {
stage('Clean') {
steps {
script {
cleanWs() // 清理工作空间
sh 'rm -rf dist/ build/ target/'
}
}
}
stage('Build') {
steps {
sh 'npm install && npm run build'
}
}
}
post {
always {
cleanWs() // 无论构建是否成功都清理
}
}
}
Docker容器内清理
# Dockerfile FROM node:18-alpine AS build WORKDIR /app COPY package*.json ./ RUN npm ci COPY . . RUN npm run build # 清理构建产物(创建更小的生产镜像) FROM nginx:alpine COPY --from=build /app/dist /usr/share/nginx/html # 保留最终镜像只包含运行所需文件
高级清理策略
基于时间的清理
#!/bin/bash
# 删除30天前的构建产物
find dist/ -type f -mtime +30 -delete
find build/ -type d -mtime +30 -exec rm -rf {} +
# 保留最新的5个构建
ls -dt dist/*/ | tail -n +6 | xargs rm -rf
磁盘空间告警自动清理
#!/bin/bash
DISK_USAGE=$(df -h / | tail -1 | awk '{print $5}' | sed 's/%//')
if [ "$DISK_USAGE" -gt 80 ]; then
echo "磁盘使用超过80%,执行清理..."
rm -rf dist/ build/ target/ .cache/
docker system prune -af 2>/dev/null || true
echo "清理完成,释放了$(df -h / | tail -1 | awk '{print $4}')空间"
fi
最佳实践建议
- 版本控制文件:不要在Git中追踪构建产物(将
dist/、build/等添加到.gitignore) - 并行清理:在流水线中并行执行多个清理任务节省时间
- 选择性清理:使用交互式或配置文件支持选择性清理
- 日志记录:记录清理操作以便审计和调试
- 自动化触发:在构建前、构建后或定时自动执行清理
选择哪种方式取决于你的项目技术栈、CI/CD环境和团队工作流程,推荐至少实现构建前自动清理和定期定时清理两种机制。