jar包發佈到中央倉庫詳細教程

之前將一個經過修改的shiro tag jar包發佈到了中央倉庫,過程曲折,因爲網上的教程大都已經過期。今天爲了寫這篇文章,又把過程走了一遍,現將最新發布過程記錄下來


#進行如下操作,建議打開翻牆軟件


第一步:新建開源項目

第二步:新建賬戶

第三步:創建Issue

第四步:配置pom.xml 和 settings.xml

第五步:使用GPA生成祕鑰

第六步:發佈jar包

碰到的問題


第一步:新建開源項目

在倉庫中新建你的開源項目


第二步:新建賬戶

新建一個Sonatype的賬戶,註冊地址,記住自己的賬戶密碼,後面會用到

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


第三步:創建Issue

創建Issue,登錄後點擊頭部Create按鈕

新彈出窗口,填下面幾項信息

創建完後,在這裏查看提交的Issues

提交完不久,你會收到一個回覆,詢問是否有權限管理域名

回覆yes or no,yes的話按提供的三種方法進行驗證

  • 在域名解析中添加一條TXT解析到該Issues的地址

  • 域名解析中添加一個跳轉到你的github項目頁面地址

  • 用域名郵箱發送email到[email protected]

如果no,你就得用github的域名了

這是我的域名解析截圖


第四步:配置pom.xml 和 settings.xml

a.上面的工作都昨完了,我們就要配置項目的pom.xml了

這是我的pom.xml文件,你可以按照上面的進行配置


<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at
      http://www.apache.org/licenses/LICENSE-2.0
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
  -->

<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.zhiplusyun</groupId>
    <artifactId>common-base</artifactId>
    <version>1.0.1</version>

    <name>zhiplusyun common-base</name>
    <description>zhiplusyun sass common-base</description>
    <url>https://www.zhiplusyun.com/</url>

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


    <licenses>
        <license>
            <name>The Apache Software License, Version 2.0</name>
            <url>https://github.com/zhiplusyun/sass-common/blob/master/LICENSE</url>
        </license>
    </licenses>

    <developers>
        <developer>
            <name>jujunchen</name>
            <email>[email protected]</email>
            <url>http://www.178le.net</url>
        </developer>
    </developers>

    <scm>
        <connection>scm:git:https://github.com/zhiplusyun/sass-common.git</connection>
        <developerConnection>scm:git:https://github.com/zhiplusyun/sass-common.git</developerConnection>
        <url>https://github.com/zhiplusyun/sass-common.git</url>
    </scm>


    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <maven.compiler.encoding>UTF-8</maven.compiler.encoding>
    </properties>


    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <version>2.0.0.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.47</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <scope>compile</scope>
            <version>1.16.20</version>
        </dependency>

        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus</artifactId>
            <version>2.3</version>
        </dependency>

    </dependencies>


    <profiles>
        <profile>
            <id>release</id>
            <build>
                <plugins>
                    <!-- Source -->
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-source-plugin</artifactId>
                        <version>2.2.1</version>
                        <executions>
                            <execution>
                                <phase>package</phase>
                                <goals>
                                    <goal>jar-no-fork</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                    <!-- Javadoc -->
                   <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-javadoc-plugin</artifactId>
                        <version>3.0.1</version>
                        <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.5</version>
                        <executions>
                            <execution>
                                <phase>verify</phase>
                                <goals>
                                    <goal>sign</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                    <plugin>
                        <groupId>org.sonatype.plugins</groupId>
                        <artifactId>nexus-staging-maven-plugin</artifactId>
                        <version>1.6.8</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-compiler-plugin</artifactId>
                        <version>3.3</version>
                        <configuration>
                            <source>1.8</source>
                            <target>1.8</target>
                            <encoding>UTF-8</encoding>
                        </configuration>
                    </plugin>
                </plugins>
            </build>

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

        </profile>
    </profiles>


</project>



b.在Maven settings.xml 的 servers 節點配置如下信息

<server>
      <id>sonatype-nexus-snapshots</id>
      <username>第二步註冊的sonatype賬號</username>
      <password>sonatype密碼</password>
    </server>
    <server>
      <id>sonatype-nexus-staging</id>
      <username>sonatype賬號</username>
      <password>sonatype密碼</password>
</server>


第五步:使用GPA生成祕鑰

win版下載地址:https://gpg4win.org/

mac,linux 直接用 命令安裝,我相信你知道該怎麼裝的

這有幾個命令

1. 查看是否安裝成功, 顯示版本號等信息表明安裝成功

gpg --version


2. 生成祕鑰 ,提示讓你輸入 姓名,郵箱,還有一個密碼,完了後生成祕鑰,如圖

gpg --gen-key

FD6B7B6531EDB4567F2870B5978A660E8D315F31 這個就是公鑰


3. 上傳公鑰到服務器

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

如果發送失敗會顯示,我這裏因爲是網絡原因,多嘗試幾次

gpg: sending key 978A660E8D315F31 to hkp://pool.sks-keyservers.net
gpg: keyserver send failed: No data
gpg: keyserver send failed: No data



4. 查看是否發送成功

gpg --keyserver hkp://pool.sks-keyservers.net --recv-keys FD6B7B6531EDB4567F2870B5978A660E8D315F31
gpg: key 978A660E8D315F31: "zhiplusyun <[email protected]>" not changed
gpg: Total number processed: 1
gpg:              unchanged: 1



第六步:發佈jar包

1. 發佈 快照版本

mvn clean deploy


2. 發佈正式版本

mvn clean deploy -P release


正式發佈後,去Issue回覆,過1-2天查看回復

類似這樣


碰到的問題

1. 快照版本能正常發佈,發佈正式版的時候,碰到 Access denied 或者 403問題 ,這要在Issue上回復一下,工作人員會給你的賬號進行權限修復


2. MAC下


Fixing GPG "Inappropriate ioctl for device" errors
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-gpg-plugin:1.5:sign (defaulton project shiro-freemarker-tags: Exit code: 127 -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-gpg-plugin:1.5:sign (defaulton project shiro-freemarker-tags: Exit code: 127
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:212)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
        at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:116)
        at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:80)
        at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51)
        at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128)
        at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:307)
        at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:193)
        at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:106)
        at org.apache.maven.cli.MavenCli.execute(MavenCli.java:863)
        at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:288)
        at org.apache.maven.cli.MavenCli.main(MavenCli.java:199)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
        at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
        at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
        at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
Caused by: org.apache.maven.plugin.MojoExecutionException: Exit code: 127
        at org.apache.maven.plugin.gpg.GpgSigner.generateSignatureForFile(GpgSigner.java:168)
        at org.apache.maven.plugin.gpg.AbstractGpgSigner.generateSignatureForArtifact(AbstractGpgSigner.java:205)
        at org.apache.maven.plugin.gpg.GpgSignAttachedMojo.execute(GpgSignAttachedMojo.java:140)
        at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:134)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:207)
        ... 20 more
[ERROR
[ERROR
[ERRORFor more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

解決方法:

To solve the problem, you need to enable loopback pinentry mode. Add this to ~/.gnupg/gpg.conf:

use-agent 
pinentry-mode loopback

And add this to ~/.gnupg/gpg-agent.conf, creating the file if it doesn't already exist:

allow-loopback-pinentry


3.  Rule failure while trying to close staging repository with ID

發生的場景,第一次jar包爲 快照版本,但我用了mvn clean deploy -P release 發佈,在修改爲正式版本後,再執行命令就報錯了

解決方法:刪除原來的GPA祕鑰,重新生成

本文分享自微信公衆號 - 阿提說說(itsaysay)。
如有侵權,請聯繫 [email protected] 刪除。
本文參與“OSC源創計劃”,歡迎正在閱讀的你也加入,一起分享。

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