Java Maven專案推送到 Maven 中央倉庫

switchvov發表於2024-04-13

準備階段

namespace 域名認證

當需要在 sonatype 認證 com.xxx名稱空間時,需要將 @.xxx.com 配置域名解析。

記錄型別:TXT

文字內容:驗證的 key。

file

file

GPG 公私鑰生成

GPG 下載地址:https://www.gnupg.org/download/index.html

Mac 可以使用 brew install gpg直接安裝

使用方式可參考:

  • https://central.sonatype.org/publish/requirements/gpg/
  • https://www.jianshu.com/p/7f19ceacf57c

生成證書

$ gpg --gen-key

file

file

查詢證書

$ gpg --list-keys 
gpg: 正在檢查信任度資料庫
gpg: marginals needed: 3  completes needed: 1  trust model: pgp
gpg: 深度:0  有效性:  1  已簽名:  0  信任度:0-,0q,0n,0m,0f,1u
gpg: 下次信任度資料庫檢查將於 2027-04-06 進行
[keyboxd]
---------
pub   ed25519 2024-04-06 [SC] [有效至:2027-04-06]
      ABC
uid             [ 絕對 ] xxx <xxx@163.com>
sub   cv25519 2024-04-06 [E] [有效至:2027-04-06]

上傳公鑰到公鑰管理伺服器

$ gpg --keyserver keyserver.ubuntu.com --send-keys ABC
gpg: 正在傳送金鑰 ABC 到 hkp://keyserver.ubuntu.com

如果報錯 gpg: keyserver send failed: No route to host可以參考該文章上傳公鑰:https://vayne.cc/2022/03/13/gpg/

推送階段

pom.xml 檔案配置

配置 url

    <url>https://github.com/xxx/yyy</url>

配置 license

    <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>

配置 issueManagement

    <issueManagement>
        <system>github</system>
        <url>https://github.com/xxx/yyy/issues</url>
    </issueManagement>

配置 SCM

    <scm>
        <connection>scm:git:https://github.com/xxx/yyy.git</connection>
        <developerConnection>scm:git:https://github.com/xxx/yyy.git</developerConnection>
        <url>https://github.com/xxx/yyy</url>
    </scm>

配置開發者資訊

    <developers>
        <developer>
            <name>xxx</name>
            <email>xxx@zzz.com</email>
            <url>https://github.com/xxx</url>
        </developer>
    </developers>

通用外掛配置

    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>flatten-maven-plugin</artifactId>
                <version>1.5.0</version>
                <configuration>
                    <updatePomFile>true</updatePomFile>
                    <flattenMode>oss</flattenMode>
                </configuration>
                <executions>
                    <execution>
                        <id>flatten</id>
                        <phase>process-resources</phase>
                        <goals>
                            <goal>flatten</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>flatten.clean</id>
                        <phase>clean</phase>
                        <goals>
                            <goal>clean</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <!-- 不上傳原始碼,刪除該外掛 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <version>3.1.0</version>
                <inherited>true</inherited>
                <executions>
                    <execution>
                        <id>attach-sources</id>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <excludeResources>true</excludeResources>
                    <useDefaultExcludes>true</useDefaultExcludes>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-javadoc-plugin</artifactId>
                <version>3.1.0</version>
                <inherited>true</inherited>
                <executions>
                    <execution>
                        <id>bundle-sources</id>
                        <phase>package</phase>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <maxmemory>1024</maxmemory>
                    <encoding>UTF-8</encoding>
                    <show>protected</show>
                    <notree>true</notree>

                    <!-- Avoid running into Java 8's very restrictive doclint issues -->
                    <failOnError>false</failOnError>
                    <doclint>none</doclint>
                </configuration>
            </plugin>
        </plugins>
    </build>

Maven 上傳外掛配置

    <profiles>
        <profile>
            <id>release</id>
            <build>
                <plugins>
                    <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>
                        <configuration>
                            <gpgArguments>
                                <!--表示密碼直接輸入,不需要彈出密碼框-->
                                <arg>--pinentry-mode</arg>
                                <arg>loopback</arg>
                            </gpgArguments>
                        </configuration>
                    </plugin>
                    <!-- 配置方式:https://central.sonatype.org/publish/publish-portal-maven/#deploymentname -->
                    <plugin>
                        <groupId>org.sonatype.central</groupId>
                        <artifactId>central-publishing-maven-plugin</artifactId>
                        <version>0.4.0</version>
                        <extensions>true</extensions>
                        <configuration>
                            <publishingServerId>central</publishingServerId>
                            <tokenAuth>true</tokenAuth>
                            <autoPublish>true</autoPublish>
                            <excludeArtifacts>
                                <excludeArtifact>xxx-yyy</excludeArtifact>
                            </excludeArtifacts>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>

settings.xml 檔案配置

  <servers>
    <server>
      <id>central</id>
      <username>xxx</username>
      <password>yyy</password>
    </server>
  </servers>
  <profiles>
    <profile>
      <id>gpg</id>
      <activation>
        <activeByDefault>true</activeByDefault>
      </activation>
      <properties>
        <gpg.executable>gpg</gpg.executable>
        <gpg.keyname>xxx@zzz.com</gpg.keyname>
        <gpg.passphrase>passphrase</gpg.passphrase>
        <gpg.useagent>true</gpg.useagent>
      </properties>
    </profile>
  </profiles>

執行構建並上傳

$ mvn clean deploy -Prelease 

上傳結果

file

報錯參考

  • Javadocs must be provided but not found in entries:需要提供 Javadoc
  • License information is missing:需要提供 license 資訊
  • Project URL is not defined:需要定義專案 URL 資訊
  • SCM URL is not defined:需要定義 SCM 資訊
  • version cannot be a SNAPSHOT:Maven 中央倉庫不支援推送快照版本

參考文件

  • https://central.sonatype.org/publish/publish-portal-maven/
  • https://central.sonatype.org/publish/requirements/gpg/
  • https://www.gnupg.org/download/index.html
  • https://www.jianshu.com/p/7f19ceacf57c

分享並記錄所學所見

相關文章