---
title: "向maven中央仓库提交jar"
author: "IXHONG"
date: "2017-12-05T21:03:19Z"
tags: []
categories: ["编程语言"]
slug: "maven-jar"
draft: false
lastmod: "2018-01-04T12:29:12Z"
id: 2403852
url: "https://wely.iteye.com/blog/2403852"
---

本文转自 http://www.cnblogs.com/gaoxing/p/4359795.html

 

从来都是从中央仓库下载jar,这次需要向中央仓库提交jar, 利用Sonatype OSSRH可以把jar等资源提交给Maven的中央仓库。

Sonatype OSSRH介绍：

Sonatype OSSRH使用[Nexus](http://www.sonatype.com/nexus) 为开源项目提供仓库管理服务，该仓库就是所谓maven的中央仓库，OSSRH允许我们向Maven中央仓库提交二进制文件。

1:提交(deploy)开发版本的二进制文件（snapshorts)

2: 阶段性的发布版本

3：发布一个release,然后同步他们到中央仓库。

## 初始阶段

1：注册一个JIRA账号：https://issues.sonatype.org/secure/Signup!default.jspa

2：创建一个新工程的单：https://issues.sonatype.org/secure/CreateIssue.jspa?issuetype=21&pid=10134

只有当这个jira单的状态我resolved时，才可以提交jar包

## 审查要求

1：提供javadoc和source

2: 使用gpg或者pgp对文件进行签名

3: pom.xml文件

4：正确的坐标：groupId,artifactId,version

```
   <groupId>com.sequoiadb</groupId>
   <artifactId>sequoiadb-driver</artifactId>
   <version>1.12</version>
```

5: projectName,description,url等。

```
   <name>${project.groupId}:${project.artifactId}</name>
   <description>SequoiaDB Driver Library</description>
   <url>https://github.com/SequoiaDB/SequoiaDB</url>
```

6： license 信息

[![复制代码](http://common.cnblogs.com/images/copycode.gif)]()

```
 <licenses>
     <license>
       <name>The Apache License, Version 2.0</name>
       <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
     </license>
   </licenses>
```

[![复制代码](http://common.cnblogs.com/images/copycode.gif)]()

7 ： 开发者信息

[![复制代码](http://common.cnblogs.com/images/copycode.gif)]()

```
   <developers>
    <developer>
      <name>linyoubing</name>
      <email>linyoubing@sequoiadb.com</email>
      <organization>sequoiadb</organization>
      <organizationUrl>http://www.sequoiadb.com</organizationUrl>
    </developer>
  </developers>
```

[![复制代码](http://common.cnblogs.com/images/copycode.gif)]()

8： SCM信息

[![复制代码](http://common.cnblogs.com/images/copycode.gif)]()

```
   <scm>
      <connection>
         scm:git:https://github.com/SequoiaDB/SequoiaDB.git
      </connection>
      <developerConnection>
         scm:git:https://github.com/SequoiaDB/SequoiaDB.git
      </developerConnection>
      <url>https://github.com/SequoiaDB/SequoiaDB</url>
     <tag>v1.12</tag>
  </scm>
```

[![复制代码](http://common.cnblogs.com/images/copycode.gif)]()

 

### 部署

可以使用多种方式部署，这是使用maven的方式

1：分布管理和认证:

我使用了maven部署插件，所以pom.xml中加入：

[![复制代码](http://common.cnblogs.com/images/copycode.gif)]()

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

[![复制代码](http://common.cnblogs.com/images/copycode.gif)]()

需要在maven\_home/conf/settings.xml配置jira的账号和密码

[![复制代码](http://common.cnblogs.com/images/copycode.gif)]()

```
<settings>
  <servers>
    <server>
      <id>ossrh</id>
      <username>your-jira-id</username>
      <password>your-jira-pwd</password>
    </server>
  </servers>
</settings>
```

[![复制代码](http://common.cnblogs.com/images/copycode.gif)]()

2：配置生成javadoc和sources包的插件：

[![复制代码](http://common.cnblogs.com/images/copycode.gif)]()

```
 <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-source-plugin</artifactId>
      <version>2.2.1</version>
      <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>
      <version>2.9.1</version>
      <executions>
        <execution>
          <id>attach-javadocs</id>
          <goals>
            <goal>jar</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
```

[![复制代码](http://common.cnblogs.com/images/copycode.gif)]()

3：GPG自动签名的插件：

[![复制代码](http://common.cnblogs.com/images/copycode.gif)]()

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

[![复制代码](http://common.cnblogs.com/images/copycode.gif)]()

在settings.xml中配置gpg的签名 ：（需要先用gpg来生成）

[![复制代码](http://common.cnblogs.com/images/copycode.gif)]()

```
<settings>
  <profiles>
    <profile>
      <id>ossrh</id>
      <activation>
        <activeByDefault>true</activeByDefault>
      </activation>
      <properties>
        <gpg.executable>gpg2</gpg.executable>
        <gpg.passphrase>the_pass_phrase</gpg.passphrase>
      </properties>
    </profile>
  </profiles>
</settings>
```

[![复制代码](http://common.cnblogs.com/images/copycode.gif)]()

4: 使用Profile

应该javadoc和source的jar包生成也需要使用gpg来签名，所以很浪费时间，而且这些执行通常都独立于标准构建流程，所以把他们移动到一个profile.

[![复制代码](http://common.cnblogs.com/images/copycode.gif)]()

```
   
   <profiles>
   <profile> 
    <id>release</id>
    <build>
        <plugins>
            <plugin>
               <groupId>org.sonatype.plugins</groupId>
               <artifactId>nexus-staging-maven-plugin</artifactId>
               <version>1.6.3</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-release-plugin</artifactId>
                 <version>2.5</version>
                 <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>
                <version>3.0</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </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>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <version>2.2.1</version>
                <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>
                <version>2.9</version>
                <executions>
                    <execution>
                        <id>attach-javadocs</id>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
         </plugins>
    </build>
  </profile>
</profiles>
```

[![复制代码](http://common.cnblogs.com/images/copycode.gif)]()

提交一个snapshot版本：

1：修改version加一个-SNAPSHOT, 执行 mvn clean deploy

发布一个release版本

1：修改version 不要加-SNAPSHOT,  可以手动修改，也可以执行 

```
mvn versions:set -DnewVersion=1.2.3
```

2: 执行 mvn clean deploy -P release