[Maven实战]发布maven项目到中央仓库(Central Repository)

概述

用maven已经一段时间,也搭建了公司内部的maven环境。然而有一些通用的可以开源的代码想放到公网的仓库中,以便可以随时使用(公司网络因为经常切换,导致maven库常有无法导入的情况)。

注册Sonatype OSSRH

关于如何注册,可以看这篇文章:向maven中央仓库提交jar

里面介绍了如何注册OSSRH账号。我补充几点:

  1. 项目groupId怎么写? 如果你是个人的名义,然后代码是放到github上面的(比如是https://www.github.com/username/projectName的格式),那么推荐groupId写成:com.github.username, 不然可能会被拒绝(我就是一开始没有写对,被拒绝了。。。)
  2. 当project的status为RESOLVED时才算是ok,如下图所示

    [Maven实战]发布maven项目到中央仓库(Central Repository)_第1张图片

修改pom.xml

账号注册完成,同时也创建了project后,就可以修改pom.xml了。
首先加入name,description,scm,developers等信息

<name>${project.groupId}:${project.artifactId}name>
<url>https://github.com/0604hx/nerve-toolsurl>
<description>description>

<licenses>
    <license>
        <name>The Apache License, Version 2.0name>
        <url>http://www.apache.org/licenses/LICENSE-2.0.txturl>
    license>
licenses>
<developers>
    <developer>
        <name>0604hxname>
        <email>[email protected]email>
        <roles>
            <role>developerrole>
        roles>
        <timezone>+8timezone>
    developer>
developers>
<scm>
    <connection>scm:git:https://github.com/0604hx/nerve-tools.gitconnection>
    <developerConnection>scm:git:https://github.com/0604hx/nerve-tools.gitdeveloperConnection>
    <url>https://github.com/0604hx/nerve-toolsurl>
    <tag>v${project.version}tag>
scm>

然后添加distributionManagement

<distributionManagement>
    <snapshotRepository>
        <id>ossrhid>
        <url>https://oss.sonatype.org/content/repositories/snapshotsurl>
    snapshotRepository>
    <repository>
        <id>ossrhid>
        <name>Maven Central Staging Repositoryname>
        <url>https://oss.sonatype.org/service/local/staging/deploy/maven2/url>
    repository>
distributionManagement>

接着添加plugins

<plugins>
    <plugin>
        <groupId>org.sonatype.pluginsgroupId>
        <artifactId>nexus-staging-maven-pluginartifactId>
        <version>1.6.3version>
        <extensions>trueextensions>
        <configuration>
            <serverId>ossrhserverId>
            <nexusUrl>https://oss.sonatype.org/nexusUrl>
            <autoReleaseAfterClose>trueautoReleaseAfterClose>
        configuration>
    plugin>
    <plugin>
        <groupId>org.apache.maven.pluginsgroupId>
        <artifactId>maven-javadoc-pluginartifactId>
        <version>2.10.3version>
        <executions>
            <execution>
                <id>attach-javadocsid>
                <goals>
                    <goal>jargoal>
                goals>
            execution>
        executions>
    plugin>
    <plugin>
        <groupId>org.apache.maven.pluginsgroupId>
        <artifactId>maven-source-pluginartifactId>
        <executions>
            <execution>
                <id>attach-sourcesid>
                <goals>
                    <goal>jar-no-forkgoal>
                goals>
            execution>
        executions>
    plugin>
    <plugin>
        <groupId>org.apache.maven.pluginsgroupId>
        <artifactId>maven-gpg-pluginartifactId>
        <version>1.6version>
        <executions>
            <execution>
                <id>sign-artifactsid>
                <phase>verifyphase>
                <goals>
                    <goal>signgoal>
                goals>
            execution>
        executions>
    plugin>
plugins>

修改maven的setting.xml

增加你的帐密信息
[Maven实战]发布maven项目到中央仓库(Central Repository)_第2张图片

然后设置gpg的profile
[Maven实战]发布maven项目到中央仓库(Central Repository)_第3张图片

此时要先安装gpg,详细的说明请看:Working with PGP Signatures

执行mvn deploy

执行以下命令
mvn deploy -Dmaven.test.skip=true -e
等待结果即可。来个截图哈
[Maven实战]发布maven项目到中央仓库(Central Repository)_第4张图片

如果deploy时出现以下错误
gpg: no default secret key: No secret key
则表示你没有创建secret key,此时创建key即可。

如果出现这样的错误:
No public key:Key with id
[Maven实战]发布maven项目到中央仓库(Central Repository)_第5张图片
就说明你没有上传keys到公共服务器,上传就行了:
这里写图片描述

发布项目资源

上一步完成后,其实这是将我们的资源上传到oss.sonatype而已,我们还有将其真正的发布出去。步骤如下:

  1. 登录到oss.sonatype.com
  2. 点击 Staging Repositories,可以看到我们刚刚上传的资源,如下图所示[Maven实战]发布maven项目到中央仓库(Central Repository)_第6张图片
  3. 选中相应的资源(处于open状态),然后点击上方的“release”按钮,会弹出确认框,直接确认即可。
  4. 上一步成功后,就表示发布完成。此时去https://issues.sonatype.org(就是你一开始创建的issue)中留言,告诉管理员你已经release了。等审核通过后,就可以在中央仓库中搜索出你的项目(是不是很激动,很有成就感^_^)。

[Maven实战]发布maven项目到中央仓库(Central Repository)_第7张图片

你可能感兴趣的:(项目实践,maven,sonatype,deploy,release)