Maven 自动化构建

Maven 自动化构建(CI/CD 全流程 · 2025 最新版)

一句话总结
一行命令 mvn clean verify + GitHub Actions / Jenkins = 自动编译 → 测试 → 打包 → 发布 → 部署!


一、自动化构建目标

步骤命令输出
1. 清理mvn clean删除 target/
2. 编译mvn compiletarget/classes
3. 测试mvn test单元测试报告
4. 集成测试mvn verify集成测试 + 覆盖率
5. 打包mvn packagetarget/*.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-SNAPSHOTmvn install
发布1.0.0mvn release:prepare
回滚1.0.1手动修改

八、完整自动化流程图

graph TD
    A[Git Push] --> B[GitHub Actions]
    B --> C{mvn clean verify}
    C --> D[单元测试]
    C --> E[集成测试]
    C --> F[JaCoCo 覆盖率]
    F --> G[Codecov 上传]
    C --> H[构建 JAR]
    H --> I[Docker 镜像]
    I --> J[推送到 Registry]
    J --> K[K8s 部署]

九、常见问题解决

问题解决方案
测试卡住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 立即获取!

类似文章

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注