手把手教学,教你把你的我的项目推送到maven中央仓库

前言

最近闲下来了,想要本身造点小轮子用,方便本身在不一样项目使用,同时想偷懒,不想每次都在项目里面copy代码。因而想到了重要仓库这个东西,把本身的代码托管到github(虽然如今的github已经再也不单纯),而后发布jar包到maven中央仓库,之后每一个项目都只用引入maven依赖就能够了。同时还能方便好基友们使用,万一哪一天好基友高兴就献身了呢,想一想就有点小激动呢。好了废话很少说,下面正式开始手把手教学活动。

附上小轮子传送门给感兴趣的小伙伴
将项目发布到maven仓库须要如下几步:
1.在sonatype中建立issue
2.使用gpg生成秘钥
3.配置Maven进行deploy和releasehtml

1 在sonatype中建立issue

首先咱们须要在https://issues.sonatype.org/secure/Dashboard.jspa中新建一个issues。若是没有sonatype帐号的话,分分钟注册一个

建立完成后就是以下图示

这个时候只须要耐心等待工做人员审核就好了,由于个人domain问题,经历了二次审核,可是都很快,通常提交后两个小时内就会有结果。当issues状态变为Resolved就能够继续下一步操做了。java

2 使用gpg生成秘钥

2.1 安装

选择对应的OS版本进行下载安装便可,下载地址传送门:https://www.gnupg.org/download/index.htmlgit

2.2 生成key

首先查看安装成功没有github

gpg –version

(MAC 和 Linux系统须要 gpg2 –version)
经过算法

gpg –gen-key

生成key,也能够经过UI界面生成和管理key

运行后gpg要求你选择加密算法,过时时间等等,这些直接选择默认值便可。经过对比发现,gpg 2.0以上的版本运行gpg –gen-key命令 会跳过这些步骤。
以后gpg要求你输入姓名,邮箱以及关键的Passphrase,依次输入便可。而后gpg为你生成了一对秘钥。
经过apache

gpg –list-keys

查看生成的key列表

这里能够看到个人公钥是:34754DFE562C10E1A09907B7F4797C9A95E36DB6,记住这个key,下面咱们须要用到。服务器

 

2.3 上传公钥

生成秘钥后,咱们须要把公钥上传到服务器上。运行如下命令:dom

gpg2 –keyserver hkp://pool.sks-keyservers.net –send-keys 34754DFE562C10E1A09907B7F4797C9A95E36DB6(刚才生成的公钥)

 

3 配置Maven

3.1 配置maven setting.xml

须要在本地的maven配置server 和 profile 两个地方,啰嗦的话就很少少了,直接上配置:jsp

<server>
        <id>ossrh</id>
        <username>第一步注册的用户名</username>
        <password>第一步注册的密码</password>
    </server>
<profile>
         <id>ossrh</id>
         <activation>
             <activeByDefault>true</activeByDefault>
         </activation>
         <properties>
             <gpg.executable>gpg(MAC 和 Linux用户使用gpg2)</gpg.executable>
             <gpg.passphrash>生成密钥时输入的密码</gpg.passphrash>
         </properties>

3.2 配置项目的pom.xml

具体的配置能够查看小轮子里面的pom设置 https://github.com/weechang/JUtil/blob/master/pom.xml
首先须要添加指向sonatype仓库的<distributionManagement>maven

 

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


继续配置pfofiles,添加各类推送、DOC、加密的插件

 

 

<profiles>
        <profile>
            <id>release
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.sonatype.plugins</groupId>
                        <artifactId>nexus-staging-maven-plugin</artifactId>
                        <version>1.6.8</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>3.0.1</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
                                </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>
            </build>
        </profile>
    </profiles>


而后还能够添加一些开发者信息和license信息,具体的就很少说了。具体能够参考小轮子的配置

 

3.3 部署到中央仓库

运行如下代码进行deploy

 

mvn clean deploy -P release


以下图表示成功部署到中央仓库

由于咱们在pom中添加了自动发布插件,因此能够不用管理,直接到中央仓库去查看就能看到你发布的项目了
PS:在部署过成功可能会遇到401的问题,具体请参考传送门 https://stackoverflow.com/questions/24830610/why-am-i-getting-a-401-unauthorized-error-in-maven

本文转自:https://blog.weechang.xyz/2018/07/20/手把手教学,教你把你的我的项目推送到maven中央仓库/

相关文章
相关标签/搜索