【狂人小白】如何将Java项目发布到Maven中

将Java项目发布到Maven中

如何通过Sonatype进行发布Maven操作,并且能够在http://search.maven.org 中搜索到
关键步骤:
1. 申请OSS账号
2. 通过gpg创建密钥
3. 完善setting.xml和pom.xml
4. 发布审核

A. 申请Sonatype账号

  1. 注册地址: https://issues.sonatype.org/secure/Signup!default.jspa

B. 创建Issue

  1. 创建地址: https://issues.sonatype.org/secure/Dashboard.jspa,点击 Create Issue

  2. 选择 Community Support - Open Source 下的 New Project

  3. 填入 Group Id  Project URL  SCM url (SVN 或者 GIT地址) 信息

C. 等待审核

  1. 当发现有人回复 Configuration has been prepared, now you can ,说明审核通过

  2. 需要注意如下重要内容

  • snapshotRepository

  • repository

1
2
3
4
5
6
7
8
9
10
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. 创建公钥私钥

  1. 如果在Windows下,你需要安装 Cygwin, 不过为了一步到位,直接下载Git Bash就好了,这个GIT Bash就是Cypwin集成了Git而已

  2. 在Linux下就不用了,一般都会有 gpg 命令

1
2
3
4
5
6
7
8
9
10
11
12
13
# 创建公钥,创建时,会提示填入账号和邮箱
gpg --gen-key
 
# 查看已有的公钥私钥
gpg --list-keys
 
# 找到自己创建账号的私钥
pub 2048R/公钥内容 2014-05-28 
uid username<username@mail.com>
sub 2048R/私钥内容 2014-05-28
 
# 发布公钥
gpg --keyserver hkp://pool.sks-keyservers.net --send-keys 公钥内容

E. 修改settings.xml

  1. settings.xml 是属于Maven的配置文件,可以通过这两个方法找到

  2. 全局的配置在 $MAVEN_HOME/conf/settings.xml 中找到,也就是Maven的安装位置

  3. 用户的配置在 用户目录 下的 ~/.m2/settings.xml 中找到,例如 C:/Users/用户名/.m2/settings.xml 当然,有可能不存在这个文件,那么就可以把全局配置复制到这里即可

servers添加账号

1
2
3
4
5
6
7
8
<servers>
    ...
    <server>
        <id>oss</id>
            <username>账号</username>
            <password>密码</password>
    </server>
</servers>

F. 修改pom.xml

pm.xml中添加descriptionlicenses developers scm以及profiles

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<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:git@github.com:41zone/Validation.FO.git</connection>
    <developerConnection>scm:git@github.com:41zone/Validation.FO.git</developerConnection>
    <url>git@github.com:41zone/Validation.FO.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进行执行可能会出问题,所以可以通过命令行进行操作

1
mvn clean deploy -P release -Dgpg.passphrease=公钥内容

H. 发布构建

  1. 进入 https://oss.sonatype.org/,这个加载会有些慢

  2. 左侧点击 Staging Repositories

  3. 会发现有以 GroupID - XXXX 格式的记录,例如 ccfozonevalidation-1000,选中后,选择上方的 Close

  4. 稍等一会儿进行刷新,如果在下方的 Summary 中的 Activity 出现 红色 标识,说明有错误,点击旁边的 Activity 进行查看 红色 标识的内容

  5. 按照操作,之后再进行 G 步骤操作,直到成功 Activity  绿色 标识

  6. 此时选择上方的 Release ,进行正式发布

J. 最后的审核

  1. 发布成功后,进入之前创建的Issue,添加Comment,说已经成功了,可以写 My Project has been successfully released.

  2. 审核成功后,即可在 http://search.maven.org 中进行搜索我们的框架了

  3. 当然,你可以在OSS中预览检索你的组件,https://oss.sonatype.org/content/groups/public










本文转自 sundunjam 51CTO博客,原文链接:http://blog.51cto.com/sunspot/1560907,如需转载请自行联系原作者
上一篇:sqlserver2010兼容sqlserver2008解决方案


下一篇:iOS网络编程-iOS中Socket编程介绍