Maven发布服务器切换问题:迁移至Central Portal后仍连接OSSRH
问题
我有一个曾通过OSSRH Sonatype发布的Maven项目,现需迁移至Central Publisher Portal,已按官方迁移步骤操作。
此前发布流程:
mvn clean install -Dskip=false mvn clean release:prepare -Dskip=false --batch-mode mvn clean release:perform -Dskip=false
迁移后.m2/settings.xml配置:
<servers> <server> <id>central</id> <username>mycentralusername</username> <password>mycentralpassword</password> </server> </servers>
pom.xml中添加的新插件配置:
<plugin> <groupId>org.sonatype.central</groupId> <artifactId>central-publishing-maven-plugin</artifactId> <version>0.7.0</version> <extensions>true</extensions> <configuration> <publishingServerId>central</publishingServerId> <autoPublish>true</autoPublish> <waitUntil>published</waitUntil> </configuration> </plugin>
但执行mvn release:perform时出现错误:
[ERROR] Failed to execute goal org.sonatype.plugins:nexus-staging-maven-plugin:1.6.13:deploy (injected-nexus-deploy) on project (...): Execution injected-nexus-deploy of goal org.sonatype.plugins:nexus-staging-maven-plugin:1.6.13:deploy failed: Server credentials with ID "ossrh" not found!
疑问:为何Maven仍尝试连接OSSRH服务器?mvn release:perform的配置存于何处,如何修改使其指向Central服务器?
解决方法
原因分析
Maven仍调用旧OSSRH配置,核心原因有两个:
- 遗留的release状态文件:
mvn release:prepare执行后会生成release.properties、pom.xml.tag等文件,这些文件缓存了旧的发布插件配置或服务器ID,导致release:perform沿用旧设置。 - pom.xml残留旧插件:项目pom.xml可能仍包含
nexus-staging-maven-plugin的配置(或通过父pom间接引入),这个OSSRH专属插件会强制要求使用ossrh服务器ID。
具体修复步骤
清理旧release状态文件
执行命令清除之前的release缓存文件,避免旧配置干扰:mvn release:clean该命令会删除项目根目录下的
release.properties、pom.xml.releaseBackup、pom.xml.tag等文件。移除旧发布插件配置
检查项目pom.xml,确保完全移除nexus-staging-maven-plugin的相关配置。如果该插件是通过父pom间接引入的,可在当前pom中显式禁用:<plugin> <groupId>org.sonatype.plugins</groupId> <artifactId>nexus-staging-maven-plugin</artifactId> <version>1.6.13</version> <configuration> <skip>true</skip> </configuration> </plugin>配置release插件使用Central服务器
确保maven-release-plugin的配置中指定正确的服务器ID,避免硬编码旧的ossrh值:<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-release-plugin</artifactId> <version>3.0.1</version> <configuration> <serverId>central</serverId> <!-- 其他release相关配置 --> </configuration> </plugin>重新执行发布流程
清理完成后,重新执行发布步骤:mvn clean install -Dskip=false mvn release:prepare -Dskip=false --batch-mode mvn release:perform -Dskip=false
内容的提问来源于stack exchange,提问作者Astor Bizard
相关产品推荐
相关产品推荐

