Github开源Java项目(Disconf)上传到Maven Central Repository方法详细介绍

最近我作了一个开源项目 Disconf:Distributed Configuration Management Platform(分布式配置管理平台) ,简单来讲,就是为全部业务平台系统管理配置文件的平台系统。更详细的介绍,请看项目主页。java

项目是用Java写的,Maven管理的,那么,天然而然,整个项目应该以Maven仓库POM方式暴露给用户来使用。所以,这两天我一直在折腾Maven Central Repository。react

本文使用Sonatype Nexus做为代理仓库。也就是说先要把软件发布到这里,而后他们会帮同步到Maven的Central Repository上,这貌似是目前最简单有效的办法。 Sonatype使用Nexus为开源项目提供托管服务。你能够经过它发布快照(snapshot)或是稳定版(release)到Maven中央仓库。 咱们只要注册一个Sonatype的JIRA帐号、建立一个JIRA ticket,而后对POM文件稍做配置便可。git

Disconf的项目结构介绍

disconf
    - disconf-client
    - disconf-core
    - disconf-web
    - disconf-demos

我将会把 disconf-core和disconf-client 推送到Maven的Central Repository上,由于这两个包是编程用户须要的。disconf-web 的编译结果是一个war包,所以不会推送到Maven的Central Repository上。github

下面将仔细的介绍 disconf-core 包的推送过程。web

第零步:将你的电脑与Github打通

用ssh-key生成的id_rsa.pub的内容放到 https://github.com/settings/ssh 中。apache

第一步:修改 disconf-core 的 POM文件

它至少应该包括如下标签:编程

具体可参见:https://github.com/knightliao/disconf/blob/master/disconf-core/pom.xml服务器

<modelVersion>4.0.0</modelVersion>

<artifactId>disconf-core</artifactId>
<packaging>jar</packaging>

<version>2.3-SNAPSHOT</version>
<name>disconf-core</name>

<parent>
    <groupId>com.baidu.disconf</groupId>
    <artifactId>disconf-base</artifactId>
    <version>2.1</version>
    <relativePath>../</relativePath>
</parent>

<url>https://github.com/knightliao/disconf/tree/master/disconf-core</url>
<description>https://github.com/knightliao/disconf/tree/master/disconf-core</description>

<organization>
    <name>Baidu</name>
    <url>http://www.baidu.com</url>
</organization>

<scm>
    <url>scm:git:git@github.com:knightliao/disconf</url>
    <connection>scm:git:git@github.com:knightliao/disconf</connection>
    <tag>disconf-core</tag>
</scm>

下面为它设置一个profile, 命名为release,意思就是发布到中央库。这里设置了两个库,一个snapshots,一个是正式的发布库。ssh

另外,中央库须要source和doc,因此增长两个plugin.maven

<profiles>
    <profile>
        <id>release</id>
        <distributionManagement>
            <snapshotRepository>
                <id>nexus-release</id>
                <url>https://oss.sonatype.org/content/repositories/snapshots/</url>
            </snapshotRepository>
            <repository>
                <id>nexus-release</id>
                <url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
            </repository>
        </distributionManagement>
        <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>2.9.1</version>
                    <executions>
                        <execution>
                            <phase>package</phase>
                            <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>

第二步:修改 disconf 的 POM文件

disconf的Pom是disconf-core的parent pom。

具体可参见:https://github.com/knightliao/disconf/blob/master/pom.xml

它至少应该包括如下标签:

<developers>
    <developer>
        <name>Liao Qiqi</name>
        <id>knightliao</id>
        <email>liaoqiqi (AT) baidu.com</email>
        <roles>
            <role>Developer</role>
        </roles>
        <timezone>+8</timezone>
    </developer>
    <developer>
        <name>Wu Ning</name>
        <id>wuning</id>
        <email>wuning01 (AT) baidu.com</email>
        <roles>
            <role>Developer</role>
        </roles>
        <timezone>+8</timezone>
    </developer>
</developers>

第三步:准备好GPG工具

咱们在本地生成好jar包后,要上传到中央库,为了保证上传过程的数据校验是对的,Sonatype要求咱们使用GPG来进行数据校验。

gpg --gen-key

出现以下提示后输入1:

在以下提示后输入想要的KEY的位数,没有特别须要的话直接回车也能够:

接着会出现询问KEY的有效期,默认这是0,也就是说永远不过时:

接着输入y并回车确认永久有效。而后会出现下面的提示,要求输入你的真实姓名:

根据提示输入姓名,邮箱和备注后,而后会提示输入用来保护密钥的密码(passphrase),若是不须要密码的话直接回车,若是输入了密码那么必定要记住,这个密码在后面会用到。接下来系统会要求你输入一些随机字符,在键盘的字母和符号上乱按就能够了,不过别按回车。

注意,这里的姓名和邮箱、密码 三个东西都要记住。!!

生成成功之后会出现信息:

public and secret key created and signed

要查看现有的公钥,输入:

gpg --list-keys

输出的格式是这个样子的:

knightliao@conan:~$ gpg --list-keys           
gpg: checking the trustdb
gpg: 3 marginal(s) needed, 1 complete(s) needed, PGP trust model
gpg: depth: 0  valid:   1  signed:   0  trust: 0-, 0q, 0n, 0m, 0f, 1u
/home/knightliao/.gnupg/pubring.gpg
-----------------------------------
pub   2048R/XX 2014-07-08
uid                  用户名 <邮箱>
sub   2048R/YY 2014-07-08

这里顺便说一下,若是你有多个KEY,想删除其中的某个KEY,方法是:

gpg --delete-keys XX

生成了key之后,按照Sonatype的要求,咱们须要把公钥上传到服务器上。输入:

gpg --keyserver <a href="http://hkp://pool.sks-keyservers.net">hkp://pool.sks-keyservers.net</a> --send-keys XX

这个服务器地址是Sonatype规定的,所以不须要修改。

验证一下:

gpg --keyserver <a href="http://hkp://pool.sks-keyservers.net">hkp://pool.sks-keyservers.net</a> --recv-keys C67A5D26

若是打印如下,则说明上传成功:

gpg: requesting key C67A5D26 from hkp server <a href="http://pool.sks-keyservers.net">pool.sks-keyservers.net</a>
gpg: key C67A5D26: "用户名 <邮箱>" not changed
gpg: Total number processed: 1
gpg:              unchanged: 1

第四步:注册Sonatype

就要像Sonatype提出申请了。首先,要到这里申请他们的Jira账号。得到账号之后,到这里提 交一个申请ticket。点击Create issue, Project要选Support - Open Source Project Repository Hosting,Issue Type选New Project。 再次提醒:groupId的写法很是重要,要么使用本身拥有的域名。要么使用项目托管的子域名,例如com.github.username。为了节省时 间,若是使用的是本身拥有的域名,那么能够在description里写清楚,不然客服有可能会问,这样就一个来回就须要一个工做日了。

一个例子就是:https://issues.sonatype.org/browse/OSSRH-10550

注意,注册时用户名和邮箱必须是 第三步里的 用户名和邮箱。

注册成功后,他们就会给你发送邮件,而后就可 登陆这里 进行查看: https://oss.sonatype.org/

第五步:修改本地maven的 settings.xml

用上一步注册时的用户名和密码填充这里:

nexus-release 用户名 密码

这里的id要和pom.xml文件里面的repository id对应,用户名和密码则是Sonatype的Jira账号和密码。

第六步:编译本地代码

进到 disconf 根目录下,执行 mvn clean install

而后

cd disconf-core
mvn clean install

若是没有问题,说明能够准备上传了。

第七步之一:上传SNAPSHOT版本代码

若是你的项目是 SNAPSHOT的,那么 就可使用本步方法进行上传,它会上传到 https://oss.sonatype.org/content/repositories/snapshots

首先,为了后续的操做顺利执行,咱们要确保项目的整个目录没有本地未提交的修改,最好先pull一遍确保后续的步骤里没有冲突。而后运行:

mvn release:prepare

mvn release:prepare does the following:

  1. Checks that your local source code does not have any modifications
  2. Writes a release.properties file with details of what it is doing
  3. Modifies the pom.xml to the release versions
  4. Does a build as far as "package" in order to assure itself that it's changes have not broken the build
  5. Commits the modified pom.xml files to SCM
  6. Tags the trunk
  7. Modifies the pom.xml to the next snapshot version
  8. Commits the modified pom.xml files to SCM

若是命令顺利运行结束,那么这个步骤就算完成了。若是中间出现了什么问题,能够在修复问题后再次运行这条命令,若是想要得到更详细的信息,能够运行:

mvn release:prepare -X

若是不但愿从终止的地方开始,而是想从头再来的话能够输入:

mvn release:prepare -Dresume=false

若是想回滚,则

mvn release:rollback

若是这步成功了,则上传到 https://oss.sonatype.org/content/repositories/snapshots 这里了。

第七步之二:上传非SNAPSHOT版本代码

SNAPSHOT通常表明着有Bug版本,线上代码通常不会使用,所以,非SNAPSHOT版本的包才是咱们真正须要的。

不要执行"第七步之一”,执行:

mvn clean deploy -P release -Dgpg.passphrase=第三步中的密码

mvn release:perform does the following

  1. Checks out the tagged release into target/checkout
  2. Forks a "mvn deploy site-deploy"
  3. Removes the release.properties file

注:mvn clean deploy -P release -Dgpg.passphrase=第三步中的密码 这句话执行时,若是你的版本是快照的,则上传快照,若是是非快照的则上传非快照的,Maven会根据模块的版本号(pom文件中的version)中是否 带有-SNAPSHOT来判断是快照版本仍是正式版本。

发布构建

进入https://oss.sonatype.org并登录,会在左侧有个staging Repositories点击进入,在右侧面板找到你的构件,状态应该是open,你要将其置为closed,点击上方的close按钮便可

接下来系统会自动验证有效性,若是你的Group Id和pom.xml没有错误,状态会自动变成closed,若是有问题,会在下面提示你那里有问题,加入有问题你能够点击drop按钮删掉这个构件,修改后从新执行步骤7。

接下来你须要点击release按钮发布你的构件。

在Issue中通知工做人员

而后回到JIRA中你的Issue,写个comment,我写的是Component has been successfully issued.告诉工做人员我发布完成了,等待他们审核。审核经过后咱们就能够在中央库搜索到咱们的构件了!搜索的地址 是:http://search.maven.org/

上传的最佳实践

  1. 先让你的电脑与Github创建链接
  2. git clone 代码下下载下来
  3. 对本地代码执行 mvn clean install , 解决代码自己的编译问题
  4. 准备好GPG工具
  5. 申请注册Sonatype
  6. 对于SNAPSHOT版本,则执行 mvn release:prepare , 一旦发现有错误,须要执行 mvn release:rollback,项目作完后,执行 mvn release:clean
  7. 对于release版本,则执行 mvn clean deploy -P release -Dgpg.passphrase=第三步中的密码 。

遇到的一些问题

Return code is: 401, ReasonPhrase: Unauthorized. release:perform

这个是由于你在Maven的settings.xml里的配置与disconf-core的pom.xml配置对应不上来。极可能就是 id没有匹配。

Maven error: “You don't have a SNAPSHOT project in the reactor projects list.”

若是的项目的版本不是SNAPSHOT,而你却要用“第七步之一”进行上传,则会报此错误。

i am getting permgen size error in jenkins how to increase

在Sonatype的界面进行上传

我试过在本地编译成jar包,而后再在界面上传。实践证实这种方法不可取。由于签名验证通不过。

相关文章
相关标签/搜索