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

来源:互联网 发布:600756 浪潮软件 编辑:程序博客网 时间:2024/05/16 06:43

概述

用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,如下图所示

    这里写图片描述

修改pom.xml

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

<name>${project.groupId}:${project.artifactId}</name><url>https://github.com/0604hx/nerve-tools</url><description></description><licenses>    <license>        <name>The Apache License, Version 2.0</name>        <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>    </license></licenses><developers>    <developer>        <name>0604hx</name>        <email>zxingming@foxmail.com</email>        <roles>            <role>developer</role>        </roles>        <timezone>+8</timezone>    </developer></developers><scm>    <connection>scm:git:https://github.com/0604hx/nerve-tools.git</connection>    <developerConnection>scm:git:https://github.com/0604hx/nerve-tools.git</developerConnection>    <url>https://github.com/0604hx/nerve-tools</url>    <tag>v${project.version}</tag></scm>

然后添加distributionManagement

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

接着添加plugins

<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-javadoc-plugin</artifactId>        <version>2.10.3</version>        <executions>            <execution>                <id>attach-javadocs</id>                <goals>                    <goal>jar</goal>                </goals>            </execution>        </executions>    </plugin>    <plugin>        <groupId>org.apache.maven.plugins</groupId>        <artifactId>maven-source-plugin</artifactId>        <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-gpg-plugin</artifactId>        <version>1.6</version>        <executions>            <execution>                <id>sign-artifacts</id>                <phase>verify</phase>                <goals>                    <goal>sign</goal>                </goals>            </execution>        </executions>    </plugin></plugins>

修改maven的setting.xml

增加你的帐密信息
这里写图片描述

然后设置gpg的profile
这里写图片描述

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

执行mvn deploy

执行以下命令
mvn deploy -Dmaven.test.skip=true -e
等待结果即可。来个截图哈
这里写图片描述

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

如果出现这样的错误:
No public key:Key with id
这里写图片描述
就说明你没有上传keys到公共服务器,上传就行了:
这里写图片描述

发布项目资源

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

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

这里写图片描述

0 0
原创粉丝点击