|
| 1 | +# 发布项目到中央仓库 |
| 2 | + |
| 3 | +## 创建工单 |
| 4 | + |
| 5 | +在发布Java包到Maven中央仓库首先需要在[https://issues.sonatype.org/secure/Dashboard.jspa](https://www.iteblog.com/redirect.php?url=aHR0cHM6Ly9pc3N1ZXMuc29uYXR5cGUub3JnL3NlY3VyZS9EYXNoYm9hcmQuanNwYQ==&article=true)网站创建一个工单(Issues),第一次使用这个网站的时候需要注册自己的帐号(这个帐号和密码需要记住,后面会用到),之后创建自己的Issue。 |
| 6 | + |
| 7 | +然后根据你Java包的功能分别写上`Summary`、`Description`、`Group Id`、`SCM url`以及`Project URL`等必要信息,可以参见我之前创建的Issue:[https://issues.sonatype.org/browse/OSSRH-33765](https://issues.sonatype.org/browse/OSSRH-33765)。创建完之后需要等待Sonatype的工作人员审核处理,审核时间还是很快的,我的审核差不多等待了两小时。当Issue的Status变为`RESOLVED`后,就可以进行下一步操作了。 |
| 8 | + |
| 9 | +> 注:如果你的Group Id填写的是自己的网站(我的就是这种情况),Sonatype的工作人员会询问你那个Group Id是不是你的域名,你只需要在上面回答是就行,然后就会通过审核。 |
| 10 | +
|
| 11 | +## gpg |
| 12 | + |
| 13 | +### 安装、下载 gpg |
| 14 | + |
| 15 | +上面创建的issuce经过审核之后,我们可以使用gpg生成密钥对,这里分两种情况: |
| 16 | + |
| 17 | +1. 如果使用的是Windows,可以到[https://www.gpg4win.org/download.html](https://www.iteblog.com/redirect.php?url=aHR0cHM6Ly93d3cuZ3BnNHdpbi5vcmcvZG93bmxvYWQuaHRtbA==&article=true)下载gpg4win,推荐使用 Gpg4win-Vanilla 2.3.3版本,因为它仅包括 GnuPG,这个工具才是我们所需要的; |
| 18 | +2. 如果使用的是Linux,可以通过`yum install gpg`命令安装gpg。 |
| 19 | + |
| 20 | +之后可以通过`gpg --version`命令查看是否安装成功,如果出现版本等信息说明安装成功了。 |
| 21 | + |
| 22 | +### 生成密钥 |
| 23 | + |
| 24 | +安装 gpg 成功后,执行命令: |
| 25 | + |
| 26 | +```bash |
| 27 | +$ gpg --gen-key |
| 28 | +``` |
| 29 | + |
| 30 | +按照提示信息依次设置好名字、邮箱等信息。不过输入Passphrase的值需要记住,这个相当于密钥的密码,发布过程中进行签名操作的时候会用到。 |
| 31 | + |
| 32 | +到这里我们就设置好密钥对了。上面代码中导数第四行的`B15C5AA3`需要记住,其相当于我们生成的key,后面会用到。 |
| 33 | + |
| 34 | +### 上传keys |
| 35 | + |
| 36 | +```bash |
| 37 | +$ gpg --list-keys |
| 38 | +xxxx/gnupg/pubring.gpg |
| 39 | +-------------------------------------------------------- |
| 40 | +pub 2048R/D416FE08 2017-12-06 |
| 41 | +uid [ultimate] Zhang Peng (author) <forbreak@163.com> |
| 42 | +sub 2048R/CBD8FA39 2017-12-06 |
| 43 | + |
| 44 | + |
| 45 | +$ gpg --keyserver hkp://keyserver.ubuntu.com:11371 --send-keys D416FE08 |
| 46 | +gpg: sending key D416FE08 to hkp server keyserver.ubuntu.com |
| 47 | +``` |
| 48 | + |
| 49 | +## settings 配置 |
| 50 | + |
| 51 | +修改 maven 的 settings.xml 文件 |
| 52 | + |
| 53 | +```xml |
| 54 | +<servers> |
| 55 | + <server> |
| 56 | + <id>sonatype-nexus-snapshots</id> |
| 57 | + <username>Sonatype网站的账号</username> |
| 58 | + <password>Sonatype网站的密码</password> |
| 59 | + </server> |
| 60 | + <server> |
| 61 | + <id>sonatype-nexus-staging</id> |
| 62 | + <username>Sonatype网站的账号</username> |
| 63 | + <password>Sonatype网站的密码</password> |
| 64 | + </server> |
| 65 | +</servers> |
| 66 | +``` |
| 67 | + |
| 68 | +## pom 配置 |
| 69 | + |
| 70 | +licenses、scm、developers 配置: |
| 71 | + |
| 72 | +```xml |
| 73 | +<licenses> |
| 74 | + <license> |
| 75 | + <name>The Apache Software License, Version 2.0</name> |
| 76 | + <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url> |
| 77 | + <distribution>repo</distribution> |
| 78 | + </license> |
| 79 | +</licenses> |
| 80 | + |
| 81 | +<developers> |
| 82 | + <developer> |
| 83 | + <name>VictorZhang</name> |
| 84 | + <email>forbreak@163.com</email> |
| 85 | + <url>https://github.com/dunwu</url> |
| 86 | + </developer> |
| 87 | +</developers> |
| 88 | + |
| 89 | +<scm> |
| 90 | + <url>https://github.com/dunwu/tent</url> |
| 91 | + <connection>git@github.com:dunwu/tent.git</connection> |
| 92 | + <developerConnection>https://github.com/dunwu</developerConnection> |
| 93 | +</scm> |
| 94 | +``` |
| 95 | + |
| 96 | +distributionManagement 配置 |
| 97 | + |
| 98 | +```xml |
| 99 | +<distributionManagement> |
| 100 | + <snapshotRepository> |
| 101 | + <id>nexus</id> |
| 102 | + <url>https://oss.sonatype.org/content/repositories/snapshots</url> |
| 103 | + </snapshotRepository> |
| 104 | + <repository> |
| 105 | + <id>nexus</id> |
| 106 | + <url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url> |
| 107 | + </repository> |
| 108 | +</distributionManagement> |
| 109 | +``` |
| 110 | + |
| 111 | +profiles 配置 |
| 112 | + |
| 113 | +```xml |
| 114 | +<profiles> |
| 115 | + <profile> |
| 116 | + <id>release</id> |
| 117 | + <build> |
| 118 | + <plugins> |
| 119 | + <plugin> |
| 120 | + <groupId>org.sonatype.plugins</groupId> |
| 121 | + <artifactId>nexus-staging-maven-plugin</artifactId> |
| 122 | + <version>1.6.7</version> |
| 123 | + <extensions>true</extensions> |
| 124 | + <configuration> |
| 125 | + <serverId>nexus</serverId> |
| 126 | + <nexusUrl>https://oss.sonatype.org/</nexusUrl> |
| 127 | + <autoReleaseAfterClose>true</autoReleaseAfterClose> |
| 128 | + </configuration> |
| 129 | + </plugin> |
| 130 | + <plugin> |
| 131 | + <groupId>org.apache.maven.plugins</groupId> |
| 132 | + <artifactId>maven-javadoc-plugin</artifactId> |
| 133 | + <version>2.10.3</version> |
| 134 | + <configuration> |
| 135 | + <failOnError>false</failOnError> |
| 136 | + <quiet>true</quiet> |
| 137 | + </configuration> |
| 138 | + <executions> |
| 139 | + <execution> |
| 140 | + <id>attach-javadocs</id> |
| 141 | + <goals> |
| 142 | + <goal>jar</goal> |
| 143 | + </goals> |
| 144 | + </execution> |
| 145 | + </executions> |
| 146 | + </plugin> |
| 147 | + <plugin> |
| 148 | + <groupId>org.apache.maven.plugins</groupId> |
| 149 | + <artifactId>maven-gpg-plugin</artifactId> |
| 150 | + <version>1.6</version> |
| 151 | + <executions> |
| 152 | + <execution> |
| 153 | + <id>sign-artifacts</id> |
| 154 | + <phase>verify</phase> |
| 155 | + <goals> |
| 156 | + <goal>sign</goal> |
| 157 | + </goals> |
| 158 | + </execution> |
| 159 | + </executions> |
| 160 | + </plugin> |
| 161 | + </plugins> |
| 162 | + </build> |
| 163 | + </profile> |
| 164 | + </profiles> |
| 165 | +``` |
| 166 | + |
| 167 | +## 部署和发布 |
| 168 | + |
| 169 | +```bash |
| 170 | +$ mvn clean deploy -P sonatype-oss-release -Darguments="gpg.passphrase=设置gpg设置密钥时候输入的Passphrase" -Dmaven.test.skip=true |
| 171 | +``` |
0 commit comments