发布到 Maven 中央仓库的异常处理

说明

没有正确配置 Maven 发布的目标地址

5979 [INFO] 
5979 [INFO] --- deploy:3.1.2:deploy (default-deploy) @ maven-deploy ---
5997 [INFO] ------------------------------------------------------------------------
5997 [INFO] BUILD FAILURE
5997 [INFO] ------------------------------------------------------------------------
5998 [INFO] Total time:  5.056 s
5998 [INFO] Finished at: 2026-02-13T15:23:21Z
5998 [INFO] ------------------------------------------------------------------------
5999 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:3.1.2:deploy (default-deploy) on project maven-deploy: Deployment failed: repository element was not specified in the POM inside distributionManagement element or in -DaltDeploymentRepository=id::url parameter -> [Help 1]
  • 解决方案 1(Maven 中央仓库配置,推荐)

    <plugin>
        <groupId>org.sonatype.central</groupId>
        <artifactId>central-publishing-maven-plugin</artifactId>
        <version>0.10.0</version>
        <extensions>true</extensions>
        <configuration>
            <publishingServerId>central</publishingServerId>
        </configuration>
    </plugin>
    
  • 解决方案 2(非 Maven 中央仓库)

    <distributionManagement>
        <!-- 快照仓库配置 -->
        <snapshotRepository>
            <id>central</id>
            <url>https://central.sonatype.com/repository/maven-snapshots</url>
        </snapshotRepository>
        <!-- 正式仓库配置 -->
        <repository>
            <id>central</id>
            <url>xxx</url>
        </repository>
    </distributionManagement>
    

凭证未配置/配置错误

9119 [INFO] ------------------------------------------------------------------------
9119 [INFO] BUILD FAILURE
9119 [INFO] ------------------------------------------------------------------------
9122 [INFO] Total time:  8.162 s
9122 [INFO] Finished at: 2026-02-13T15:30:57Z
9122 [INFO] ------------------------------------------------------------------------
9123 [ERROR] Failed to execute goal org.sonatype.central:central-publishing-maven-plugin:0.10.0:publish (injected-central-publishing) on project maven-deploy: Failed to deploy artifacts: Could not transfer artifact cn.com.xuxiaowei:maven-deploy:jar:0.0.1-20260213.153049-1 from/to central (https://central.sonatype.com/repository/maven-snapshots/): status code: 401, reason phrase: Unauthorized (401) -> [Help 1]
  • 解决方案
    1. pom.xml 与 settings.xml 使用相同的 serverId
    2. 配置正确的凭证

缺少源码

pkg:maven/cn.com.xuxiaowei/maven-deploy@0.0.1:
 - Sources must be provided but not found in entries
  • 解决方案

    mvn -V -B clean source:jar deploy
    

缺少 javadoc

pkg:maven/cn.com.xuxiaowei/maven-deploy@0.0.1:
 - Javadocs must be provided but not found in entries
  • 解决方案

    mvn -V -B clean javadoc:jar deploy
    

缺少签名

pkg:maven/cn.com.xuxiaowei/maven-deploy@0.0.1:
 - Missing signature for file: maven-deploy-0.0.1.jar
 - Missing signature for file: maven-deploy-0.0.1.pom
  • 解决方案

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-gpg-plugin</artifactId>
        <version>3.2.8</version>
        <executions>
            <execution>
                <id>sign-artifacts</id>
                <phase>verify</phase>
                <goals>
                    <goal>sign</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
    
    gpg --list-keys
    gpg --import ./xxx.gpg
    gpg --list-keys
    

缺少项目名称

pkg:maven/cn.com.xuxiaowei/maven-deploy@0.0.1:
 - Project name is missing
  • 解决方案

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    
        <name>maven-deploy</name>
    
    </project>
    

缺少项目描述

pkg:maven/cn.com.xuxiaowei/maven-deploy@0.0.1:
 - Project description is missing
  • 解决方案

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    
        <description>maven-deploy</description>
    
    </project>
    

缺少项目地址

pkg:maven/cn.com.xuxiaowei/maven-deploy@0.0.1:
 - Project URL is not defined
  • 解决方案

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    
        <url>https://xuxiaowei.io</url>
    
    </project>
    

缺少开源协议

pkg:maven/cn.com.xuxiaowei/maven-deploy@0.0.1:
 - License information is missing
  • 解决方案 1:使用空协议

    <licenses>
        <license/>
    </licenses>
    
  • 解决方案 2:使用开源协议,如:Apache 2.0 协议

    <licenses>
      <license>
        <name>Apache License, Version 2.0</name>
        <url>https://www.apache.org/licenses/LICENSE-2.0</url>
      </license>
    </licenses>
    

缺少版本控制

pkg:maven/cn.com.xuxiaowei/maven-deploy@0.0.1:
 - SCM URL is not defined
  • 解决方案

    <scm>
        <connection>
            scm:git:https://git@gitlab.xuxiaowei.com.cn/xuxiaowei-io/spring-security-oauth2-authorization-server-password.git
        </connection>
        <developerConnection/>
        <tag/>
        <url>https://gitlab.xuxiaowei.com.cn/xuxiaowei-io/spring-security-oauth2-authorization-server-password</url>
    </scm>
    

缺少开发者

pkg:maven/cn.com.xuxiaowei/maven-deploy@0.0.1:
 - Developers information is missing
  • 解决方案 1:使用空协议

    <developers>
        <developer/>
    </developers>
    
  • 解决方案 2

    <developers>
        <developer>
            <name>徐晓伟</name>
            <email>xuxiaowei@xuxiaowei.com.cn</email>
            <organization>徐晓伟工作室</organization>
            <organizationUrl>http://xuxiaowei.com.cn</organizationUrl>
        </developer>
    </developers>