如何解决Sonatype Maven插件无法解析settings.xml凭证占位符的问题?
解决Sonatype Central Publishing Maven插件无法解析settings.xml环境变量占位符的问题
可行解决方案
方案1:命令行直接传递系统属性
跳过settings.xml的占位符配置,直接在CI/CD的Maven执行命令中传入系统属性参数:
mvn clean deploy -Dcentral-publishing.username=${env.SONATYPE_USERNAME} -Dcentral-publishing.password=${env.SONATYPE_PASSWORD}
插件会直接读取这两个系统属性,无需依赖settings.xml的server标签配置。
方案2:生成临时settings.xml文件
在CI/CD流程中通过脚本生成已解析凭证的临时settings文件,再指定给Maven使用:
- 生成临时配置文件的脚本:
cat > temp-settings.xml << EOF <settings> <servers> <server> <id>central</id> <username>${SONATYPE_USERNAME}</username> <password>${SONATYPE_PASSWORD}</password> </server> </servers> </settings> EOF
- 执行Maven命令时指定该临时文件:
mvn clean deploy --settings temp-settings.xml
方案3:降级插件版本
若业务场景允许,暂时降级到支持settings.xml占位符解析的旧版本(如0.7.0及以下),在pom.xml中指定版本:
<plugin> <groupId>org.sonatype.central</groupId> <artifactId>central-publishing-maven-plugin</artifactId> <version>0.7.0</version> <!-- 其他插件配置 --> </plugin>
验证方式
执行命令后查看日志,若出现如下类似内容,则说明凭证已正确解析:
[INFO] Using Usertoken auth, with namecode: ******
内容的提问来源于stack exchange,提问作者Luciano
相关产品推荐
相关产品推荐

