Maven 自动化部署

Maven 自动化部署(Deploy)完全实战(2025 最新版)

一句话总结
mvn deploy = 自动打包 + 上传 + 发布到远程仓库,配合 Nexus / GitHub Packages / Docker Registry 实现 一键部署


一、部署目标

目标命令结果
发布 JAR/WARmvn deploy上传到 Nexus
发布 SNAPSHOTmvn deploy -U动态更新
发布正式版mvn release:perform打 Tag + 发布
Docker 镜像mvn deploy推送到 Registry
K8s 部署helm upgrade滚动更新

二、核心配置:distributionManagement

pom.xml 中配置

<distributionManagement>
    <!-- SNAPSHOT 仓库 -->
    <snapshotRepository>
        <id>nexus-snapshots</id>
        <url>http://nexus.company.com/repository/maven-snapshots/</url>
    </snapshotRepository>

    <!-- 正式版仓库 -->
    <repository>
        <id>nexus-releases</id>
        <url>http://nexus.company.com/repository/maven-releases/</url>
    </repository>
</distributionManagement>

Maven 自动判断1.0.0-SNAPSHOTsnapshotRepository
1.0.0repository


三、认证配置:settings.xml

~/.m2/settings.xml

<settings>
  <servers>
    <!-- 与 pom.xml 中的 <id> 一致 -->
    <server>
      <id>nexus-snapshots</id>
      <username>admin</username>
      <password>admin123</password>
    </server>
    <server>
      <id>nexus-releases</id>
      <username>admin</username>
      <password>admin123</password>
    </server>
  </servers>
</settings>

安全建议:CI 环境使用加密密码或 Token


四、发布命令大全

命令作用
mvn deploy发布到远程仓库
mvn deploy -U强制更新 SNAPSHOT
mvn deploy -DskipTests跳过测试发布
mvn deploy -Prelease激活正式版 Profile

五、部署到不同仓库

1. Nexus 私有仓库(企业推荐)

<distributionManagement>
  <repository>
    <id>nexus-releases</id>
    <url>http://nexus:8081/repository/maven-releases/</url>
  </repository>
  <snapshotRepository>
    <id>nexus-snapshots</id>
    <url>http://nexus:8081/repository/maven-snapshots/</url>
  </snapshotRepository>
</distributionManagement>

2. GitHub Packages

<distributionManagement>
  <repository>
    <id>github</id>
    <url>https://maven.pkg.github.com/owner/repo</url>
  </repository>
</distributionManagement>

settings.xml

<server>
  <id>github</id>
  <username>YOUR_GITHUB_USERNAME</username>
  <password>YOUR_GITHUB_TOKEN</password>
</server>

3. Docker Registry(JAR + 镜像)

<plugin>
  <groupId>com.spotify</groupId>
  <artifactId>dockerfile-maven-plugin</artifactId>
  <version>1.5.2</version>
  <executions>
    <execution>
      <id>push-image</id>
      <phase>deploy</phase>
      <goals><goal>push</goal></goals>
    </execution>
  </executions>
  <configuration>
    <repository>registry.company.com/${project.artifactId}</repository>
    <tag>${project.version}</tag>
  </configuration>
</plugin>

六、Spring Boot 项目一键部署

<build>
  <plugins>
    <plugin>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-maven-plugin</artifactId>
      <version>3.3.5</version>
      <executions>
        <execution>
          <goals><goal>repackage</goal></goals>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>
mvn clean deploy -DskipTests

输出:

target/my-app-1.0.0.jar → 上传到 Nexus
Docker 镜像 → 推送到 Registry

七、自动化部署流程(CI/CD)

GitHub Actions 示例

name: Deploy

on:
  push:
    tags:
      - 'v*'

jobs:
  deploy:
    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'

      - name: Deploy to Nexus
        env:
          NEXUS_USER: ${{ secrets.NEXUS_USER }}
          NEXUS_PASS: ${{ secrets.NEXUS_PASS }}
        run: |
          mvn -B deploy -DskipTests \
            -s $GITHUB_WORKSPACE/settings.xml

      - name: Build & Push Docker
        run: |
          docker build -t registry.company.com/my-app:${{ github.ref_name }} .
          echo ${{ secrets.REGISTRY_PASS }} | docker login -u ${{ secrets.REGISTRY_USER }} --password-stdin registry.company.com
          docker push registry.company.com/my-app:${{ github.ref_name }}

八、K8s 自动部署(Helm)

helm/values.yaml

image:
  repository: registry.company.com/my-app
  tag: "1.0.0"

部署命令

helm upgrade --install my-app ./helm-chart

九、Maven Release 插件(自动化版本发布)

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-release-plugin</artifactId>
  <version>3.1.1</version>
  <configuration>
    <tagNameFormat>v@{project.version}</tagNameFormat>
    <autoVersionSubmodules>true</autoVersionSubmodules>
  </configuration>
</plugin>
# 1. 准备发布
mvn release:prepare

# 2. 执行发布
mvn release:perform

自动完成:

  • 版本升级
  • 打 Git Tag
  • mvn deploy
  • 版本回退到 SNAPSHOT

十、完整 pom.xml 部署配置

<distributionManagement>
  <repository>
    <id>nexus-releases</id>
    <url>http://nexus:8081/repository/maven-releases/</url>
  </repository>
  <snapshotRepository>
    <id>nexus-snapshots</id>
    <url>http://nexus:8081/repository/maven-snapshots/</url>
  </snapshotRepository>
</distributionManagement>

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-deploy-plugin</artifactId>
      <version>3.1.1</version>
    </plugin>
  </plugins>
</build>

十一、常见问题解决

问题解决方案
401 Unauthorized检查 settings.xml <id> 匹配
SNAPSHOT 不更新mvn deploy -U
Docker 推送失败docker login 测试
Release 失败mvn release:clean 清理

十二、一句话总结

mvn deploy + distributionManagement + settings.xml = 一键发布全自动!”


恭喜!你已掌握 Maven 自动化部署!


下一步推荐

方向内容
Maven Release 插件自动打 Tag + 发布
ArgoCD 部署GitOps 自动化
Helm ChartK8s 包管理

需要 完整 CI/CD + Helm + Maven 模板
回复 CI Deploy 立即获取 GitHub 仓库!

类似文章

发表回复

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