天天看點

maven 釋出jar到 中央倉庫

語雀最新:https://www.yuque.com/simonalong/ruanjian/byigmt

一、步驟

1.注冊

https://issues.sonatype.org/secure/Signup!default.jspa

2.登入

https://issues.sonatype.org/login.jsp

3.建立issue

https://issues.sonatype.org/secure/CreateIssue.jspa?issuetype=21&pid=10134建立好的就是如下這種

maven 釋出jar到 中央倉庫

建立完之後會收到一封郵件,然後我們建立好的issue就如下,後面跟官方的一些互動和問答基本上都是在這裡完成https://issues.sonatype.org/browse/OSSRH-49415

4.建立完畢後等待通知

我這裡遇到這樣的一個問題,後來收到的官方的回複如下

Please create a public repo called https://github.com/simonalong/OSSRH-49415 to verify github account ownership.

If you do not own this github account, please read:

http://central.sonatype.org/pages/choosing-your-coordinates.html

好像是我還得建立一個公共的倉庫,名字叫OSSRH-49415,去自己的賬号中建立一個這樣的公共倉庫就好了,建立完之後在issue中回複下就好,比如

maven 釋出jar到 中央倉庫

最後會收到對應的回複,比如

maven 釋出jar到 中央倉庫

整個評論回複如下

maven 釋出jar到 中央倉庫

5.生成公鑰

mac 進行安裝gpg

brew install -v gpg

生成密鑰

gps --gen-key
maven 釋出jar到 中央倉庫

輸入o,就是表示确認,然後兩次密碼确認

maven 釋出jar到 中央倉庫

檢視公鑰

gpg --list-key
maven 釋出jar到 中央倉庫

6.釋出公鑰

主要是将我們的公鑰釋出給官方,讓其能夠解析我們本地用私鑰加密後的資料

gpg --keyserver hkp://pool.sks-keyservers.net --send-keys 你的公鑰

比如

gpg --keyserver hkp://pool.sks-keyservers.net --send-keys 14A4854E9312A84AF051C61494254BDBB854A311

檢視是否上傳成功

gpg --keyserver hkp://pool.sks-keyservers.net --recv-keys 你的公鑰
maven 釋出jar到 中央倉庫

7.配置maven

我這裡建立一個settings.xml,然後在裡面添加對應的賬戶資訊

<servers>
    <server>
        <id>oss</id>
        <username>使用者名</username>
        <password>密碼</password>
    </server>
</servers>
           

8.代碼中配置代碼釋出器

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

9.上傳

這裡上傳一定不要點選idea的deploy按鈕,因為deploy和maven-gpg-plugin結合的不是很好,那個gpg插件無法運作,一定要用用戶端或者iterm即可。運作這樣運作

mvn clean deploy --settings /Users/zhouzhenyong/.m2/setting_maven_center.xml

其中會提示輸入gpg的密碼,輸入即可

10.檢視

https://oss.sonatype.org/#stagingRepositories我們可以去這裡檢視,首先登陸,賬号是我們注冊的使用者名和密碼,然後點選左側的“Staging Respositories(暫存倉庫)”,然後在右上角輸入groupId,就可以搜尋我們的這個

maven 釋出jar到 中央倉庫

如果在這裡能夠找到,說明已經釋出到這裡算是成功了,但是還需要從業人員進行審計,需要在我們的Issue中通知一下從業人員即可在下面的評論中,添加這樣的一句評論

The component has been successfully released,please confirm

11.關閉釋放倉庫

這裡其實是暫存庫,我們釋出到這裡之後,如果釋出成功,則進行關閉最後就可以了,關閉的時候,其中确認框中輸入該項目的介紹資訊,這個是會在界面上展示用于搜尋的

maven 釋出jar到 中央倉庫

關閉後,檢視下面,主要是用于稽核,如果成功了,則為下面的。

maven 釋出jar到 中央倉庫

這裡也遇到了一些問題,具體的問題看下面的問題即可,關閉完成的話,點選release,也就是将目前的這個庫釋放,并自動發送到中央倉庫

maven 釋出jar到 中央倉庫

12.回複issue

當我們釋出完成之後,我們可以在issue下面回複一條說明自己已經完成了元件的釋出,請求确認,比如我的:

Although I have encountered some problems, finaly I have released my staging repo.The component has been successfully released,please confirm, thanks very much

13.檢視中央倉庫

當那邊的從業人員審批完成之後,就可以将這個issue關閉了,一般過十分鐘或者兩個小時左右,我們就可以去中央倉庫https://search.maven.org/中檢視,應該就可以看到我們的這個項目了,對于更加流行的倉庫https://mvnrepository.com,可能需要好幾個星期之後才會看到,主要是同步慢。我這邊是過了一天之後,這邊就可以檢視了

maven 釋出jar到 中央倉庫

14.以後的釋出

以後如果釋出該項目,則直接maven釋出即可,然後close,然後release即可。不用像上面一樣建立issue了,如果釋出的項目跟該項目group相同,則按照maven釋出->close->release即可,如果不同則要開始注冊issue了。不過對于每次釋出都手動close和release有點麻煩的話,可以使用nexus-staging-maven-plugin這個插件,但是建議第一次不要使用該插件,手動執行是可以填寫一些描述的,但是這個插件執行是不會有任何再輸入的,而是一次性就執行完,最後等十幾分鐘就直接發到倉庫中心了。

附錄:

1.完整的pom參考

pom.xml 中必須包括:name、description、url、licenses、developers、scm 等基本資訊,使用了 Maven 的 profile 功能,隻有在 release 的時候才建立源碼包、文檔包、使用 GPG 進行數字簽名。
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.github.simonalong</groupId>
    <artifactId>mikilin</artifactId>
    <packaging>jar</packaging>
    <version>1.4.0</version>

    <name>Mikilin</name>
    <url>https://github.com/SimonAlong/Mikilin</url>
    <description>對象屬性核查工具</description>

    <licenses>
        <license>
            <name>The Apache Software License, Version 2.0</name>
            <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
        </license>
    </licenses>

    <developers>
        <developer>
            <name>zhouzhenyong</name>
            <email>[email protected]</email>
            <roles>
                <role>developer</role>
            </roles>
        </developer>
    </developers>

    <scm>
        <connection>scm:git:[email protected]:SimonAlong/Mikilin.git</connection>
        <developerConnection>scm:git:[email protected]:SimonAlong/Mikilin.git</developerConnection>
        <url>[email protected]:SimonAlong/Mikilin.git</url>
    </scm>

    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.0</version>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.47</version>
        </dependency>

        <!--spock測試架構-->
        <dependency>
            <groupId>org.spockframework</groupId>
            <artifactId>spock-core</artifactId>
            <version>1.2-groovy-2.4</version>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <artifactId>groovy-all</artifactId>
                    <groupId>org.codehaus.groovy</groupId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.12</version>
        </dependency>
        <dependency>
            <groupId>org.codehaus.groovy</groupId>
            <artifactId>groovy-all</artifactId>
            <version>2.4.8</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <!-- Source -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <version>2.1</version>
                <configuration>
                    <attach>true</attach>
                </configuration>
                <executions>
                    <execution>
                        <phase>compile</phase>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <!-- Javadoc -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-javadoc-plugin</artifactId>
                <version>2.10.4</version>
                <configuration>
                    <show>private</show>
                    <nohelp>true</nohelp>
                    <charset>UTF-8</charset>
                    <encoding>UTF-8</encoding>
                    <docencoding>UTF-8</docencoding>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

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

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

</project>
           
pom.xml 中必須包括:name、description、url、licenses、developers、scm 等基本資訊,使用了 Maven 的 profile 功能,隻有在 release 的時候才建立源碼包、文檔包、使用 GPG 進行數字簽名。

參考:

https://www.jianshu.com/p/8c3d7fb09bce

https://www.jianshu.com/p/8c3d7fb09bce

https://www.jianshu.com/p/d0e43416aaf6

https://www.jianshu.com/p/d0e43416aaf6

https://www.iteblog.com/archives/1807.html

https://www.iteblog.com/archives/1807.html

https://zhuanlan.zhihu.com/p/41650855

https://zhuanlan.zhihu.com/p/41650855

https://my.oschina.net/u/2335754/blog/476676

https://my.oschina.net/u/2335754/blog/476676

問題:

1.公鑰上傳不上去

如果遇到上傳公鑰上傳不上去的問題,可以檢視這裡

2.maven-gpg-plugin問題

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-gpg-plugin:1.6:sign (sign-artifacts) on project mikilin: Exit code: 2 -> [Help 1]

最終的原因是不能用idea 自帶的那個終端,

注意:!!!!!!!!!!!!!不要使用idea自帶的終端,真想說一百字的髒話,嘗試了好久,各種問題都嘗試了,還有就是要在.bashrc中添加如下的一個即可

export GPG_TTY=$(tty)

3.xxx.asc檔案找不到

這個asc檔案其實是gpg檔案生成的,也就是上面maven-gpg-plugin檔案是必須要生效才能生成這些檔案的

[圖檔上傳失敗…(image-25c101-1561878176712)]

4.版本上傳注意點

第一次不能上傳SNAPSHOT版本

繼續閱讀