动态

详情 返回 返回

Maven中央倉庫OSSRH服務630結束後如何調整原有配置發佈實操指北 - 动态 详情

Maven中央倉庫OSSRH服務630結束後如何調整原有配置發佈實操指北

‌🏷️ 標籤‌:#maven #經驗分享 #java #mavenplugin #maven中央倉庫

📚背景

OSSRH服務於250630服務結束,無法再使用原nexus-staging-maven-plugin插件配置直接上傳到中央倉庫,影響release和快照版deploy。

影響的相關倉庫地址有:

https://oss.sonatype.org/

https://s01.oss.sonatype.org/

訪問首頁後可見:

The OSSRH service will reach end-of-life on June 30th, 2025. Learn more about how to transfer to the Central Publishing Portal here.

對應説明鏈接:

https://central.sonatype.org/pages/ossrh-eol/

💡 前言

本文作者 “新程快咖員” ,轉載請註明出處~
原文地址 -> 戳這裏

官方下線了原有的服務,無法再通過原有nexus-staging-maven-plugin插件直接進行發佈。博主也遇到了這個問題,就進行了梳理,讓我帶你快速切換 ~

一種是(方式1)切換nexus-staging-maven-plugin插件為central-publishing-maven-plugin插件,一種是(方式2)保持原有插件不變只進行替換token和相關url即可( 發佈release成功後會部署記錄會包含(via OSSRH Staging API)標識 )。

注:方式1和方式2按需使用即可,同一分支代碼只保留一種,下文出現方式1和方式2關鍵字時需留意。

插件推薦:

💥 IDEA 神器 Maven With Me(MPVP) 插件,Maven 開發加速必備!

一鍵輕鬆幫您搞定版本值升級或回退(再也不用頭疼和花費大量時間調整版本值啦)

支持項目視圖中展示版本值,一眼便能知曉當下版本~

支持查詢中央倉庫最新依賴版本,也可以快速查詢Nexus倉庫(遠程/私服)依賴版本~

詳情直達 -> 戳這裏

🌟 操作步驟

1、登錄網站

https://central.sonatype.com/

用户名、密碼和原OSSRH服務網站一致

2、啓用快照版

點擊頭像,選擇View Namespaces菜單。也可直接訪問下面的鏈接:

https://central.sonatype.com/publishing/namespaces

找到已有的Central Portal Namespaces,點擊最右邊的按鈕,選擇Enable SNAPSHOTs即可

3、獲取發佈token並修改setting.xml

點擊頭像,選擇View Account菜單。也可直接訪問下面的鏈接:

https://central.sonatype.com/account

外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳

然後點擊Generate User Token按鈕進行操作(彈窗後選擇OK)即可得到最新發布需要的Username和Password。

方式1:切換為新central-publishing-maven-plugin插件? (重要!!!)

在當前使用的 maven 所生效的setting.xml 找到 < servers> 標籤新增 < server> 配置即可 (這裏id使用central,username和password進行替換上面獲取的數據)。



    <server>
        <id>central</id>
        <username></username>
        <password></password>
    </server>


方式2:仍使用原插件nexus-staging-maven-plugin? (重要!!!)

在當前使用的 maven 所生效的setting.xml 找到 < servers> 原ossrh的 < server> 配置進行替換即可 (這裏id保持原有不變,username和password進行替換上面獲取的數據)。



    <server>
        <id>ossrh</id>
        <username></username>
        <password></password>
    </server>


4、修改pom.xml
4.1 修改插件配置
方式1:切換為新central-publishing-maven-plugin插件? (重要!!!)

註釋或刪除原有nexus-staging-maven-plugin(包含屬性配置):



          <!--  <plugin>
                <groupId>org.sonatype.plugins</groupId>
                <artifactId>nexus-staging-maven-plugin</artifactId>
                <version>${nexus-staging-maven-plugin.version}</version>
                <extensions>true</extensions>
                <configuration>
                    <serverId>ossrh</serverId>
                    <nexusUrl>https://s01.oss.sonatype.org/</nexusUrl>
                    <autoReleaseAfterClose>true</autoReleaseAfterClose>
                </configuration>
            </plugin>
-->

新增插件配置:



            <plugin>
                <groupId>org.sonatype.central</groupId>
                <artifactId>central-publishing-maven-plugin</artifactId>
                <version>0.8.0</version>
                <extensions>true</extensions>
                <configuration>
                    <publishingServerId>central</publishingServerId>
                    <autoPublish>true</autoPublish>
                    <!-- 
                    <waitUntil>published</waitUntil>
                    -->
                </configuration>
            </plugin>

方式2:仍使用原插件nexus-staging-maven-plugin? (重要!!!)

修改<nexusUrl>標籤值為新url



<plugin>
                <groupId>org.sonatype.plugins</groupId>
                <artifactId>nexus-staging-maven-plugin</artifactId>
                <version>1.6.7</version>
                <extensions>true</extensions>
                <configuration>
                    <serverId>ossrh</serverId>
<!--
                    <nexusUrl>https://oss.sonatype.org/</nexusUrl>
-->

<!--
                    <nexusUrl>https://s01.oss.sonatype.org/</nexusUrl>
-->
                    <nexusUrl>https://ossrh-staging-api.central.sonatype.com/</nexusUrl>
                    <autoReleaseAfterClose>true</autoReleaseAfterClose>
                </configuration>
            </plugin>


4.2 <distributionManagement> 標籤進行調整配置
方式1:切換為新central-publishing-maven-plugin插件? (重要!!!)

進行註釋或刪除原有ossrh的 snapshotRepository 和 repository配置,新增central的snapshotRepository配置

當前採用註釋的方式進行處理,如下所示:



    <distributionManagement>
     <!--   <snapshotRepository>
            <id>ossrh</id>
            <url>https://s01.oss.sonatype.org/content/repositories/snapshots</url>
        </snapshotRepository>
        <repository>
            <id>ossrh</id>
            <url>https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/</url>
        </repository>
-->

        <!-- https://central.sonatype.org/publish/publish-portal-snapshots/ -->
        <snapshotRepository>
            <id>central</id>
            <url>https://central.sonatype.com/repository/maven-snapshots/</url>
        </snapshotRepository>
    </distributionManagement>


方式2:仍使用原插件nexus-staging-maven-plugin? (重要!!!)

替換snapshotRepository的url為新url,替換repository的url為新url

如下所示:



    <distributionManagement>
        <snapshotRepository>
            <id>ossrh</id>
<!--
            <url>https://oss.sonatype.org/content/repositories/snapshots</url>
-->
<!--
            <url>https://s01.oss.sonatype.org/content/repositories/snapshots</url>
-->
            <url>https://central.sonatype.com/repository/maven-snapshots/</url>
        </snapshotRepository>
        <repository>
            <id>ossrh</id>
<!--
            <url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
-->
<!--
            <url>https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/</url>
-->
            <url>https://ossrh-staging-api.central.sonatype.com/service/local/</url>
        </repository>
    </distributionManagement>


4.3 (可選)<repositories> 標籤新增Central Portal Snapshots repository用於加載依賴


    <repositories>
        <repository>
            <name>Central Portal Snapshots</name>
            <id>central-portal-snapshots</id>
            <url>https://central.sonatype.com/repository/maven-snapshots/</url>
            <releases>
                <enabled>false</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
    </repositories>


5、deploy

正常deploy即可

其他

如何獲取最新快照版本(img.shields.io)徽章?

這裏可以訪問 https://shields.io/badges/maven-metadata-url 進行生成,主要參數就是 metadataUrl 和 label (自定義,一般為latest snapshot)。

metadataUrl即依賴的 maven-metadata.xml 地址 (可從deploy的快照版maven-metadata.xml地址進行復制,不包含快照版本名稱的路徑),格式如下:



https://central.sonatype.com/repository/maven-snapshots/  + {groupId中的.替換為/} + / + {artifactId} + /maven-metadata.xml

官方文檔

生成token:

https://central.sonatype.org/publish/generate-portal-token/

通過maven發佈:

https://central.sonatype.org/publish/publish-portal-maven/

發佈快照版:

https://central.sonatype.org/publish/publish-portal-snapshots/

Publishing By Using the Portal OSSRH Staging API:

https://central.sonatype.org/publish/publish-portal-ossrh-sta...

🎉 結尾

歡迎關注公眾號 “新程快咖員” 解鎖更多內容!

以上就是本篇文章的全部內容啦,感謝您的閲讀和觀看。歡迎點贊、轉發(分享)和推薦~

user avatar juicefs 头像 u_17558081 头像 kohler21 头像 daqianduan 头像 eolink 头像 aitibao_shichangyingxiao 头像 jianghushinian 头像 cloudace 头像 best_6455a509a2177 头像 chenbimo 头像 ftkj_2018 头像 fennudemantou 头像
点赞 25 用户, 点赞了这篇动态!
点赞

Add a new 评论

Some HTML is okay.