mac發佈項目到maven中央倉庫

1.環境準備

mac系統

maven

java

2.註冊 Sonatype 賬號

Sonatype官網:http://www.sonatype.org/
註冊地址:https://issues.sonatype.org/secure/Signup!default.jspa
oss地址:https://oss.sonatype.org

登錄 Sonaytype Jira 創建一個 issue
創建一個 issue

Project 選擇 Community Support - Open Source Project Repository Hosting (OSSRH)
Issue Type 選擇 new Project
Summary 輸入你的 項目介紹
GroupId 填寫你的工程使用的 groupId,如 io.github.xxx,如果是其他,必須是自己的域名,必要時,管理員會要求你用填寫域名的郵箱發送驗證郵件來證明域名是你的;
其他不用填寫,直接點擊 Create,創建一個 issue
接下來就是等待回覆了;

審批通過之後,會回覆你這樣的內容

 3.更新本地 Maven 的 setting.xml 文件

<server>
     <id>ossrh</id>
     <username>剛註冊的用戶名</username>
     <password>密碼</password>
</server>    

添加setting.xml添加profile

    <profile>
      <id>ossrh</id>
      <activation>
        <activeByDefault>true</activeByDefault>
      </activation>
      <properties>
        <gpg.executable>gpg2</gpg.executable>
        <gpg.passphrase>the_pass_phrase</gpg.passphrase>
      </properties>
    </profile>

4.生成 GPG 簽名

下載安裝gpg2

生成gpg2簽名

$ gpg2 --gen-key

# 接下來會要求輸入 username 和 email,請對號入座,確認信息之後會彈出一個 shell 對話框,要求輸入簽名祕鑰passphrase,輸入一個自己記得住的祕鑰,一定要記下來,以後每次 deploy 都會使用到。確認後完成簽名的生成。

pub   rsa2048 2019-04-25 [SC] [有效至:2021-04-24]
      4140A213ED5136AB8130E3E0B66F18CEA315F945
uid                      yingxiaoyong <[email protected]>
sub   rsa2048 2019-04-25 [E] [有效至:2021-04-24]

其中4140A213ED5136AB8130E3E0B66F18CEA315F945 這個是我們後面要用到的公鑰key
# 上傳 GPG 公鑰到祕鑰服務器
$ gpg2 --keyserver hkp://pool.sks-keyservers.net --send-keys 公鑰ID
 
# 通過下面的命令驗證是否上傳成功
$ gpg2 --keyserver hkp://pool.sks-keyservers.net --recv-keys 公鑰ID

5.修改pom.xml文件

<?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>io.github.yxylemon</groupId>
    <artifactId>excel-boot</artifactId>
    <version>1.1.RELEASE</version>

    <parent>
        <groupId>org.sonatype.oss</groupId>
        <artifactId>oss-parent</artifactId>
        <version>9</version>
    </parent>

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

    <scm>
        <url>https://github.com/yxylemon/excel-boot</url>
        <connection>https://github.com/yxylemon/excel-boot.git</connection>
        <developerConnection>https://github.com/yxylemon/excel-boot</developerConnection>
    </scm>
    <developers>
        <developer>
            <name>yingxiaoyong</name>
            <email>[email protected]</email>
            <url>https://github.com/yxylemon/excel-boot</url>
        </developer>
    </developers>

    <dependencies>
        <!--poi-->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>4.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml-schemas</artifactId>
            <version>4.0.1</version>
        </dependency>
        <!--org.apache.xerces.parsers.SAXParser-->
        <dependency>
            <groupId>xerces</groupId>
            <artifactId>xercesImpl</artifactId>
            <version>2.12.0</version>
        </dependency>
        <!--lombok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.4</version>
            <scope>provided</scope>
        </dependency>
        <!--HttpServletResponse-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>3.0-alpha-1</version>
            <scope>provided</scope>
        </dependency>
        <!--log-->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.8.0-beta2</version>
        </dependency>
        <!--Guava-->
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>27.0.1-jre</version>
        </dependency>
    </dependencies>

    <distributionManagement>
        <snapshotRepository>
            <id>ossrh</id>
            <url>https://oss.sonatype.org/content/repositories/snapshots</url>
        </snapshotRepository>
        <repository>
            <id>ossrh</id>
            <url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
        </repository>
    </distributionManagement>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <target>1.6</target>
                    <source>1.6</source>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <profiles>
        <profile>
            <id>release</id>
            <build>
                <plugins>
<!--                    <plugin>
                        <groupId>org.sonatype.plugins</groupId>
                        <artifactId>nexus-staging-maven-plugin</artifactId>
                        <version>1.6.3</version>
                        <extensions>true</extensions>
                        <configuration>
                            <serverId>ossrh</serverId>
                            <nexusUrl>https://oss.sonatype.org/</nexusUrl>
                            <autoReleaseAfterClose>true</autoReleaseAfterClose>
                        </configuration>
                    </plugin>-->
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-source-plugin</artifactId>
                        <version>2.1.2</version>
                        <executions>
                            <execution>
                                <id>attach-sources</id>
                                <goals>
                                    <goal>jar</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>

                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-javadoc-plugin</artifactId>
                        <version>2.10.1</version>
                        <executions>
                            <execution>
                                <id>attach-javadocs</id>
                                <goals>
                                    <goal>jar</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-gpg-plugin</artifactId>
                        <version>1.5</version>
                        <executions>
                            <execution>
                                <id>sign-artifacts</id>
                                <phase>verify</phase>
                                <goals>
                                    <goal>sign</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>

    </profiles>
</project>

其中scm,developer修改成自己的地址,plugins中source,javadoc,gpg是必須的

發佈構建到 Sonatype

我 pom.xml 裏面定義的 profiles id 爲“release”,所以我執行下面的 Command

$ mvn clean deploy -P release

## 過程中會要求輸入前面生成 GPG 祕鑰的 passphrase,輸入即可

如果不出意外,整個過程將會一帆風順,刷過幾屏日誌之後,本地 Maven 構架就已經上傳到 Sonatype 的 OSS 暫存倉庫上了,注意只是StagingRepositories,還沒有到中央倉庫;

這是就可以登錄 https://oss.sonatype.org/ 查看了
點擊 左側菜單欄當中的 Staging Repositiries,在右上角的搜索框中輸入自己的 groupId 進行模糊檢索;找到自己剛纔 deploy 的構建;
Staging Repositiries
勾選構建,點擊 Close ,輸入一段 Description 之後點擊 Confirm 按鈕;
之後需要等待一段時間,等 Close Staging 的狀態生效之後,就可以再次登錄oss找到自己的構建,選中之後,點擊 Release 按鈕;再次 Confirm 之後,恭喜你,你的構建已經正式發佈到 Maven 中央倉庫了。

接下來,到中央倉庫:https://search.maven.org/ 搜索到你的構建了,如果沒有搜索到,稍等片刻一定會有的,OSS 同步到中央倉庫需要一點點時間;

如果你已經在中央倉庫搜索到自己的構建了,記得回到 sonatype 的 jira issue 裏面回覆管理員你已經完成了構建,可以 Close this issue 了;

tips:如果想自動close-release 可以把上面的註解的組建nexus-staging-maven-plugin註解打開

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章