Maven Release

验证:未记录
适用版本
未记录
验证范围
尚未登记验证记录。

本文缩写(英文全称与中文含义)
缩写 英文全称/名称 中文含义
CI Continuous Integration 持续集成
POM Project Object Model Maven 项目对象模型
https://maven.apache.org/guides/mini/guide-releasing.html
https://maven.apache.org/plugins/maven-deploy-plugin/
https://maven.apache.org/plugins/maven-deploy-plugin/deploy-file-mojo.html
https://maven.apache.org/plugins/maven-deploy-plugin/usage.html
https://maven.apache.org/repository/layout.html
https://maven.apache.org/repositories/remote.html
https://maven.apache.org/plugins/maven-gpg-plugin/
https://maven.apache.org/plugins/maven-release-plugin/

1. Important Points#

SNAPSHOT:
    mutable development version

release:
    immutable version published once

rule:
    release from CI
    tag source code
    publish artifact to release repository

2. Version Policy#

examples:
    1.4.0-SNAPSHOT
    1.4.0
    1.4.1

rules:
    never overwrite release artifact
    do not depend on SNAPSHOT in production
    bump version in a controlled release job

3. Deploy#

./mvnw -B -s settings.xml clean deploy
<distributionManagement>
  <repository>
    <id>company-releases</id>
    <url>https://nexus.example.com/repository/maven-releases/</url>
  </repository>
  <snapshotRepository>
    <id>company-snapshots</id>
    <url>https://nexus.example.com/repository/maven-snapshots/</url>
  </snapshotRepository>
</distributionManagement>

4. Library Release Checklist#

check:
    version has no SNAPSHOT
    dependency versions are stable
    plugin versions are explicit
    tests and quality gates pass
    generated POM is consumer-friendly
    changelog and tag are created

5. Signing#

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-gpg-plugin</artifactId>
  <version>3.2.4</version>
  <executions>
    <execution>
      <id>sign-artifacts</id>
      <phase>verify</phase>
      <goals>
        <goal>sign</goal>
      </goals>
    </execution>
  </executions>
</plugin>

Release artifact 不覆盖重发;签名私钥由受控 CI Secret 提供,并保留版本、提交号与制品对应关系。

signing:
    proves artifact publisher identity
    private key must be stored as CI secret

6. Source And Javadoc Artifacts#

publish library usually includes:
    main jar
    pom
    sources jar
    javadoc jar
    signatures when repository requires them
./mvnw -B clean deploy

7. maven-release-plugin#

maven-release-plugin:
    classic Maven release workflow
    updates versions, commits, tags, deploys

modern CI often prefers:
    explicit version update
    git tag
    mvn deploy

8. Rollback#

snapshot:
    rebuild and redeploy if repository policy allows

release:
    do not overwrite
    publish a new patch version
    revoke or mark bad artifact in repository manager if needed

9. 直接发布已有文件:deploy:deploy-file#

mvn deploy 是执行项目的 deploy 生命周期阶段,通常会先构建、测试再发布当前项目。mvn deploy:deploy-file 则直接调用 Deploy Plugin 的 deploy-file goal,把已经存在的 JAR、ZIP 等文件作为 Maven 制品上传;它不会替你构建或测试该文件。

9.1 上传 ZIP 到 Nexus SNAPSHOT 仓库#

假设当前目录已有 application-name-1.0.0-SNAPSHOT.zip,当前目录的 settings.xml 中配置了 Nexus 凭据:

<settings>
  <servers>
    <server>
      <id>snapshots</id>
      <username>${env.NEXUS_USER}</username>
      <password>${env.NEXUS_PASSWORD}</password>
    </server>
  </servers>
</settings>
mvn -s ./settings.xml deploy:deploy-file \
  -DgroupId=com.example \
  -DartifactId=application-name \
  -Dversion=1.0.0-SNAPSHOT \
  -Dpackaging=zip \
  -Dfile=./application-name-1.0.0-SNAPSHOT.zip \
  -Durl=https://nexus.com/nexus/repository/maven-snapshots_n3p/ \
  -DrepositoryId=snapshots \
  -DgeneratePom=true

在有项目 Maven Wrapper 时可将 mvn 换成 ./mvnw。-s ./settings.xml 使用指定的配置文件;省略 -s 时通常使用用户目录的 ~/.m2/settings.xml。示例文件名只是本地输入文件名,远端文件名由 Maven 制品坐标生成。

参数 本例含义 与什么关联
-DgroupId=com.example 制品所属组 仓库路径中的 com/example/;消费方依赖坐标的 groupId
-DartifactId=application-name 制品名称 仓库路径中的 application-name/;远端文件名的前缀;消费方依赖坐标的 artifactId
-Dversion=1.0.0-SNAPSHOT 快照版本 仓库中的版本目录 1.0.0-SNAPSHOT/;远端文件名通常换成时间戳版本
-Dpackaging=zip 制品类型 远端主文件的 .zip 扩展名;消费方若用 Maven 依赖此 ZIP,通常指定 <type>zip</type>
-Dfile=./application-name-1.0.0-SNAPSHOT.zip 本地待上传文件 路径相对于执行命令的工作目录;不决定远端目录或文件名
-Durl=https://nexus.com/nexus/repository/maven-snapshots_n3p/ 实际上传目的地 maven-snapshots_n3p 是 Nexus 仓库 URL 中的仓库名;应指向允许 SNAPSHOT 的 Maven hosted 仓库
-DrepositoryId=snapshots 用于查找认证信息的 ID 必须与所选 settings.xml 中 <servers><server><id>snapshots</id> 一致;它不是目录名,也不要求与 URL 中的仓库名相同
-DgeneratePom=true 同时生成并上传简易 POM POM 包含本例提供的坐标和类型;若已有含依赖信息的 POM,改用 -DpomFile=路径

-DrepositoryId 不指定时插件使用默认 ID remote-repository 查找凭据;只要 settings.xml 中配置的是 snapshots,就应显式传入 -DrepositoryId=snapshots。用户名和密码来自该 <server>,不会从 -Durl 自动推断。url 控制上传到哪里,repositoryId 控制用哪组凭据,两者需要由操作者核对为同一目标。

9.2 上传后的目录与文件名#

标准 Maven 仓库布局按 groupId/artifactId/version 组织。示例 URL 是仓库根目录,上传后通常可在 Nexus 中看到类似结构(时间戳和序号仅为示意,每次发布不同):

maven-snapshots_n3p/
└── com/
    └── example/
        └── application-name/
            ├── maven-metadata.xml
            └── 1.0.0-SNAPSHOT/
                ├── maven-metadata.xml
                ├── application-name-1.0.0-20261016.083000-1.zip
                └── application-name-1.0.0-20261016.083000-1.pom

com.example 变成 com/example;application-name 和 1.0.0-SNAPSHOT 分别形成制品目录和版本目录。远端 SNAPSHOT 文件名通常把 SNAPSHOT 替换为 YYYYMMDD.HHMMSS-构建序号,所以不会直接沿用本地 ZIP 的文件名。maven-metadata.xml 记录版本与快照解析信息;Nexus 还可能显示校验和等附属文件。后续再次发布同一 SNAPSHOT 版本,通常会新增一组带新时间戳的 ZIP/POM,并更新元数据;具体保留数量由仓库策略决定。

9.3 参数名易错处#

示例必须写 artifactId、packaging、url=,不能写成 artofactId、package、url-。-s 后的文件名以真实存在的配置文件为准;若文件实际叫 setting.xml,就写 -s ./setting.xml。每行末尾的 \ 是 shell 续行符;也可以把整条命令写在一行。发布前确认仓库 URL、凭据和版本,避免把制品上传到错误仓库。

搜索文档

输入关键词,搜索所有文档。

↑ ↓ 选择 · Enter 打开 · Esc 关闭