将Java项目发布到Maven中
如何通过Sonatype进行发布Maven操作,并且能够在http://search.maven.org 中搜索到
关键步骤:
1. 申请OSS账号
2. 通过gpg创建密钥
3. 完善setting.xml和pom.xml
4. 发布审核
A. 申请Sonatype账号
B. 创建Issue
创建地址: https://issues.sonatype.org/secure/Dashboard.jspa,点击 Create Issue
选择 Community Support - Open Source 下的 New Project
填入 Group Id 、 Project URL 、 SCM url (SVN 或者 GIT地址) 信息
C. 等待审核
当发现有人回复 Configuration has been prepared, now you can ,说明审核通过
需要注意如下重要内容
snapshotRepository
repository
Configuration has been prepared, now you can:
# snapshotRepository:该地址将会被用在pom.xml中的distributionManagement中的snapshotRepository的url
Deploy snapshot artifacts into repository https://oss.sonatype.org/content/repositories/snapshots
# repository:该地址将会被用在pom.xml中的distributionManagement中的repository的url
Deploy release artifacts into the staging repository https://oss.sonatype.org/service/local/staging/deploy/maven2
Promote staged artifacts into repository 'Releases'
Download snapshot and release artifacts from group https://oss.sonatype.org/content/groups/public
Download snapshot, release and staged artifacts from staging group https://oss.sonatype.org/content/groups/staging
please comment on this ticket when you promoted your first release, thanks
D. 创建公钥私钥
如果在Windows下,你需要安装 Cygwin, 不过为了一步到位,直接下载Git Bash就好了,这个GIT Bash就是Cypwin集成了Git而已
在Linux下就不用了,一般都会有
gpg
命令
# 创建公钥,创建时,会提示填入账号和邮箱
gpg --gen-key
# 查看已有的公钥私钥
gpg --list-keys
# 找到自己创建账号的私钥
pub 2048R/公钥内容 2014-10-07
uid username<xxxxx@mail.com>
sub 2048R/私钥内容 2014-10-07
# 发布公钥
gpg --keyserver hkp://pool.sks-keyservers.net --send-keys 公钥内容
E. 修改settings.xml
settings.xml
是属于Maven的配置文件,可以通过这两个方法找到全局的配置在
$MAVEN_HOME/conf/settings.xml
中找到,也就是Maven的安装位置用户的配置在 用户目录 下的
~/.m2/settings.xml
中找到,例如C:/Users/用户名/.m2/settings.xml
,当然,有可能不存在这个文件,那么就可以把全局配置复制到这里即可
在servers
添加账号
<servers>
...
<server>
<id>oss</id>
<username>账号</username>
<password>密码</password>
</server>
</servers>
F. 修改pom.xml
为pm.xml
中添加description
,licenses
, developers
,scm
以及profiles
<description>Validation Framework for Java Development</description>
<licenses>
<license>
<name>MIT</name>
<url>http://mit-license.org/</url>
</license>
</licenses>
<developers>
<developer>
<name>开发人员姓名</name>
<email>开发人员邮箱</email>
</developer>
</developers>
<scm>
<connection>scm:SVN或者GIT地址</connection>
<developerConnection>scm:SVN或者GIT地址</developerConnection>
<url>SVN或者GIT地址</url>
</scm>
<profiles>
<profile>
<id>release</id>
<distributionManagement>
<!-- 初步审核通过后,会给你这个内容,在上面已经给出 -->
<snapshotRepository>
<id>oss</id>
<url>https://oss.sonatype.org/content/repositories/snapshots/</url>
</snapshotRepository>
<!-- 初步审核通过后,会给你这个内容,在上面已经给出 -->
<repository>
<id>oss</id>
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
</repository>
</distributionManagement>
<build>
<plugins>
<!-- Source -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- Javadoc -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.9.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</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>
</plugins>
</build>
</profile>
</profiles>
G. 发布到OSS中
如果通过Eclipse进行执行可能会出问题,所以可以通过命令行进行操作
mvn clean deploy -P release -Dgpg.passphrease=公钥内容
H. 发布构建
进入 https://oss.sonatype.org/,这个加载会有些慢
左侧点击 Staging Repositories
会发现有以
GroupID - XXXX
格式的记录,例如ccfozonevalidation-1000
,选中后,选择上方的Close
稍等一会儿进行刷新,如果在下方的
Summary
中的Activity
出现 红色 标识,说明有错误,点击旁边的Activity
进行查看 红色 标识的内容按照操作,之后再进行 G 步骤操作,直到成功
Activity
为 绿色 标识此时选择上方的
Release
,进行正式发布
J. 最后的审核
发布成功后,进入之前创建的Issue,添加Comment,说已经成功了,可以写
Project has been successfully issued.
审核成功后,即可在 http://search.maven.org 中进行搜索我们的框架了
当然也可以预先检索我们的框架,https://oss.sonatype.org/content/groups/public