一句话总结:
mvn deploy = 自动打包 + 上传 + 发布到远程仓库,配合 nexus / github packages / docker registry 实现 一键部署!
一、部署目标
| 目标 | 命令 | 结果 |
|---|---|---|
| 发布 jar/war | mvn deploy | 上传到 nexus |
| 发布 snapshot | mvn 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-snapshot → snapshotrepository
1.0.0 → repository
三、认证配置: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 自动化部署的文章就介绍到这了,更多相关maven 自动化部署内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论