---
title: "JAVA开源软件开发必备技能-发布构建到maven中央仓库"
author: "xtuhcy"
date: "2015-12-23T10:48:26Z"
tags: []
categories: ["开源软件"]
slug: "java-maven"
draft: false
lastmod: "2015-12-23T13:03:25Z"
id: 2265895
url: "https://xtuhcy.iteye.com/blog/2265895"
---

# JAVA开源软件开发必备技能

> 发布构建到maven中央仓库的方法和详细步骤

- **sonatype介绍**
- **向sonatype提交申请**
- **配置maven**
- **GPG签名**
- **发布版本**
- **通知sonatype**

* * *

## sonatype介绍

Maven项目托管在Apache上的，但是中央仓库不是Apache的资源，中央仓库是由Sonatype出资维护的。目前来说，http://repo1.maven.org/maven2/是真正的Maven中央仓库的地址。Nexus仓库就是Sonatype开发的，搭建maven私服普遍采用Nexus。

## 向sonatype提交申请

- 首先需要注册sonatype，地址：https://issues.sonatype.org/browse/OSSRH-19497?filter=-2
- 创建issue，点击Create，选择New Project ![这里写图片描述](http://img.blog.csdn.net/20151222214205248)
- 提交申请，主要填写项目简介，groupId，projectUrl，scmUrl。这里的groupId必须是自己拥有的域名，或者采用com.github.{user} ![这里写图片描述](http://img.blog.csdn.net/20151222214337931)![这里写图片描述](http://img.blog.csdn.net/20151222214358491)
- 提交申请后，sonatype的工作人员会进行审核，通过后issue的状态变为RESOLVED ![这里写图片描述](http://img.blog.csdn.net/20151222214940279)

## 配置maven

- 项目基本信息配置，主要包括名称，描述，github地址，开发者信息等
- ```
  <name>Gecco</name>
  <description>Easy to use lightweight web crawler</description>
  <url>https://github.com/xtuhcy/gecco</url>
  <scm>
  	<connection>scm:git:https://github.com/xtuhcy/gecco.git</connection>
  	<developerConnection>scm:git:https://github.com/xtuhcy/gecco.git</developerConnection>
  	<url>https://github.com/xtuhcy/gecco</url>
  	<tag>v1.0.0</tag>
  </scm>
  <developers>
  	<developer>
  		<name>xtuhcy</name>
  		<email>xtuhcy@163.com</email>
  		<organization>geccocrawler</organization>
  		<organizationUrl>http://www.geccocrawler.com</organizationUrl>
  	</developer>
  </developers>
  <licenses>
  	<license>
  		<name>The MIT License (MIT)</name>
  		<url>https://raw.githubusercontent.com/xtuhcy/gecco/master/LICENSE</url>
  	</license>
  </licenses>
  ```
- 配置source、javadoc打包和gpg签名。上传maven中央库的构建必须包含源码、javadoc，并且对构件jar、源码jar、javadoc的jar进行gpg签名
- ```
  <profiles>
  	<profile>
  		<id>release</id>
  		<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.sonatype.plugins</groupId>
  					<artifactId>nexus-staging-maven-plugin</artifactId>
  					<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-release-plugin</artifactId>
  					<configuration>
  						<autoVersionSubmodules>true</autoVersionSubmodules>
  						<useReleaseProfile>false</useReleaseProfile>
  						<releaseProfiles>release</releaseProfiles>
  						<goals>deploy</goals>
  					</configuration>
  				</plugin>
  				<plugin>
  					<groupId>org.apache.maven.plugins</groupId>
  					<artifactId>maven-compiler-plugin</artifactId>
  					<configuration>
  						<source>1.6</source>
  						<target>1.6</target>
  						<encoding>UTF-8</encoding>
  					</configuration>
  				</plugin>
  				<plugin>
  					<groupId>org.apache.maven.plugins</groupId>
  					<artifactId>maven-gpg-plugin</artifactId>
  					<executions>
  						<execution>
  							<id>sign-artifacts</id>
  							<phase>verify</phase>
  							<goals>
  								<goal>sign</goal>
  							</goals>
  						</execution>
  					</executions>
  				</plugin>
  				<plugin>
  					<groupId>org.apache.maven.plugins</groupId>
  					<artifactId>maven-source-plugin</artifactId>
  					<configuration>
  						<encoding>UTF-8</encoding>
  					</configuration>
  					<executions>
  						<execution>
  							<id>attach-sources</id>
  							<goals>
  								<goal>jar-no-fork</goal>
  							</goals>
  						</execution>
  					</executions>
  				</plugin>
  				<plugin>
  					<groupId>org.apache.maven.plugins</groupId>
  					<artifactId>maven-javadoc-plugin</artifactId>
  					<configuration>
  						<encoding>UTF-8</encoding>
  					</configuration>
  					<executions>
  						<execution>
  							<id>attach-javadocs</id>
  							<goals>
  								<goal>jar</goal>
  							</goals>
  						</execution>
  					</executions>
  				</plugin>
  			</plugins>
  		</build>
  	</profile>
  </profiles>
  ```
- 打包时只需要添加-P release即可执行上述构件jar、源码jar、javadoc的jar以及签名等一系列操作。如mvn deploy -P release
- 配置上传账户，修改maven的setting.xml，添加刚才注册的sonatype的账号和密码
- ```
  <servers>
  	<server>
  		<id>ossrh</id>
  		<username>memory</username>
  		<password>your password</password>
  	</server>
  </servers>
  ```
- 上述配置完成后还不能成功上传中央库，需要配置gpg签名。生成密钥。

## GPG签名

- gpg签名的主要目的就是生成公钥、私钥，并将公钥上传到gpg的密钥服务器上，之后上传maven服务器的文件都可以通过该私钥进行数字签名，以保证是您自己上传到maven服务器上的。
- 第一步，安装gng window下载地址：https://www.gpg4win.org/download.html
- 第二步，生成密钥 gpg --gen-key 除了用户名和邮箱已经密码，其他均可以默认 ![生成密钥](http://img.blog.csdn.net/20151222223723957)生成密钥后，可以通过gpg --list-keys查看 ![密钥查看](http://img.blog.csdn.net/20151222223939590)
- 第三步，上传公钥到gpg服务器 gpg --keyserver hkp://pool.sks-keyservers.net --send-k eys 3E8E57E4 有时不成功，多尝试几次。

## 发布版本

一切准备就绪，发布版本，mvn deploy -P release。编译后在签名是需要提供刚才生成gpg密钥时输入的密码 ![输入gpg密码](http://img.blog.csdn.net/20151222224300709) 到此，一个构件已经发布到中央库，但是能正式下载还需要sonatype的工作人员审核。

## 最后一步通知sonatype

回到sonatype网站的issue，回复issue，等待审核。审核通过的结果如下： ![这里写图片描述](http://img.blog.csdn.net/20151223100545344) 等待2个小时，您就可以在中央库下载自己的jar了。

这里说明一下自己使用的软件环境：maven3.2.2，eclipse luna