Maven 自动化构建
Maven自动化构建全流程(CI/CD) 本文介绍了使用Maven实现自动化构建的全流程方案。主要内容包括: 构建流程:从清理、编译、测试到打包、发布、部署的完整步骤 核心插件配置: 测试插件(Surefire/Failsafe) 代码覆盖率(Jacoco) Docker镜像构建(dockerfile-maven) 自动化命令: mvn clean verify(完整验证) mvn deploy(
·
Maven 自动化构建(CI/CD 全流程 · 2025 最新版)
一句话总结:
一行命令mvn clean verify+ GitHub Actions / Jenkins = 自动编译 → 测试 → 打包 → 发布 → 部署!
一、自动化构建目标
| 步骤 | 命令 | 输出 |
|---|---|---|
| 1. 清理 | mvn clean |
删除 target/ |
| 2. 编译 | mvn compile |
target/classes |
| 3. 测试 | mvn test |
单元测试报告 |
| 4. 集成测试 | mvn verify |
集成测试 + 覆盖率 |
| 5. 打包 | mvn package |
target/*.jar |
| 6. 发布 | mvn deploy |
上传 Nexus |
| 7. 部署 | Docker / K8s |
运行容器 |
二、核心插件配置(pom.xml)
<properties>
<java.version>17</java.version>
<spring-boot.version>3.3.5</spring-boot.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
</properties>
<build>
<plugins>
<!-- 1. 编译跳过测试 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.2.5</version>
<configuration>
<skipTests>${skipTests}</skipTests>
</configuration>
</plugin>
<!-- 2. 集成测试 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>3.2.5</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- 3. 代码覆盖率 -->
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.12</version>
<executions>
<execution>
<goals><goal>prepare-agent</goal></goals>
</execution>
<execution>
<id>report</id>
<phase>verify</phase>
<goals><goal>report</goal></goals>
</execution>
</executions>
</plugin>
<!-- 4. Docker 镜像构建 -->
<plugin>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-plugin</artifactId>
<version>1.5.2</version>
<executions>
<execution>
<id>build-image</id>
<phase>package</phase>
<goals><goal>build</goal></goals>
</execution>
<execution>
<id>push-image</id>
<phase>deploy</phase>
<goals><goal>push</goal></goals>
</execution>
</executions>
<configuration>
<repository>registry.example.com/${project.artifactId}</repository>
<tag>${project.version}</tag>
</configuration>
</plugin>
</plugins>
</build>
三、自动化构建命令(一键执行)
# 开发环境(跳过测试)
mvn clean package -DskipTests
# CI 环境(完整验证)
mvn clean verify
# 发布 SNAPSHOT
mvn deploy -U
# 发布正式版
mvn deploy -Prelease
四、GitHub Actions 自动化(.github/workflows/ci.yml)
name: CI/CD
on:
push:
branches: [ main, develop ]
pull_request:
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up JDK 17
uses: actions/setup-java@v4
with:
java-version: '17'
distribution: 'temurin'
cache: maven
- name: Build & Test
run: mvn -B clean verify
- name: Upload Coverage
uses: codecov/codecov-action@v4
- name: Build Docker Image
if: github.ref == 'refs/heads/main'
run: mvn -B package -DskipTests
- name: Login to Registry
if: github.ref == 'refs/heads/main'
uses: docker/login-action@v3
with:
registry: registry.example.com
username: ${{ secrets.REGISTRY_USER }}
password: ${{ secrets.REGISTRY_PASS }}
- name: Push Docker Image
if: github.ref == 'refs/heads/main'
run: mvn -B deploy -DskipTests
五、Jenkins Pipeline 脚本
pipeline {
agent any
tools { maven 'Maven 3.9' }
environment {
DOCKER_REPO = 'registry.example.com'
}
stages {
stage('Build') {
steps {
sh 'mvn clean compile'
}
}
stage('Test') {
steps {
sh 'mvn test'
}
post {
always {
junit 'target/surefire-reports/*.xml'
}
}
}
stage('Package') {
steps {
sh 'mvn package -DskipTests'
}
}
stage('Deploy') {
when { branch 'main' }
steps {
sh 'mvn deploy -DskipTests'
}
}
}
}
六、Dockerfile(配合 Maven)
FROM openjdk:17-jre-slim
VOLUME /tmp
COPY target/*.jar app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
七、版本管理策略
| 环境 | 版本 | 命令 |
|---|---|---|
| 开发 | 1.0.0-SNAPSHOT |
mvn install |
| 发布 | 1.0.0 |
mvn release:prepare |
| 回滚 | 1.0.1 |
手动修改 |
八、完整自动化流程图
九、常见问题解决
| 问题 | 解决方案 |
|---|---|
| 测试卡住 | mvn verify -DforkCount=1 |
| 内存溢出 | export MAVEN_OPTS="-Xmx2g" |
| Docker 权限 | sudo usermod -aG docker $USER |
| Nexus 认证失败 | ~/.m2/settings.xml 配置 <servers> |
十、一键自动化脚本
#!/bin/bash
# build.sh
set -e
echo "开始自动化构建..."
mvn clean verify -U
if [ "$1" == "deploy" ]; then
echo "发布到 Nexus..."
mvn deploy -DskipTests
fi
echo "构建完成!"
chmod +x build.sh
./build.sh deploy
恭喜!你已掌握 Maven 自动化构建!
下一步推荐
| 方向 | 内容 |
|---|---|
| Maven Release 插件 | 自动打 Tag + 发布 |
| ArgoCD 部署 | GitOps 自动化 |
| SonarQube 集成 | 代码质量检查 |
需要 Maven Release 自动发布脚本?
回复 Maven Release 立即获取!
更多推荐


所有评论(0)