001-将本身的jar提交maven中央仓

1、Maven中央仓库提交过程

① https://issues.sonatype.org 工单管理地址,就是申请上传资格和groupId 的地方。html

② https://oss.sonatype.org/#welcome  构件仓库,把jar包上传到这里,Release 以后就会同步到maven中央仓库。java

③ http://search.maven.org/  最终表如今能够在这里搜索到。git

利用Sonatype OSSRH能够把jar等资源提交给Maven的中央仓库。github

2、Sonatype OSSRH介绍

  Sonatype OSSRH使用Nexus 为开源项目提供仓库管理服务,该仓库就是所谓maven的中央仓库,OSSRH容许咱们向Maven中央仓库提交二进制文件。apache

  1:提交(deploy)开发版本的二进制文件(snapshorts)json

  2: 阶段性的发布版本ubuntu

  3:发布一个release,而后同步他们到中央仓库。mybatis

3、工单使用

3.一、注册帐号

https://issues.sonatype.org 注册一个账号,注册地址,登陆帐号密码后续还要配置  maven  的setting.xml 中,而后建立一个工单,以下图(记住是先注册,下面是建立工单的图)。app

3.二、建立工单

  

  

注意:jsp

推荐设置方式

Group Id ,惟一标识,推荐使用com.github.xxxxx  ,xxxx要与github 用户名一致,会大概5分钟经过

  若是用的是其余的好比:com.sojson.core 之类的,管理员会问你这个是否是属于你的网站等等,官方文档:http://central.sonatype.org/pages/choosing-your-coordinates.html 

② ProjectURL ,填项目源码的地址,若是不想公布源码,那么填写一个只含README的项目的地址就能够了。

*其实管理员主要就是审核Group Id,其余的不重要

而后点击左侧列表中的Resolved recently 能够找到你刚建立的工单。

这时你的工单的状态Status 是Open ,等到审核经过状态会变为RESOLVED 

  

审查要求:

  • 一、提供JavaDoc和source
  • 二、使用gpg或者pgp对文件进行签名
  • 三、正确的坐标:groupId、artifactId、version
  • 四、projectName、description、url、license、developers、scm等信息

4、项目中的 pom.xml的基本配置

一、正确的坐标

    <groupId>com.github.bjlhx15</groupId>
    <artifactId>xgen</artifactId>
    <version>1.0-SNAPSHOT</version>

二、项目信息描述、liscens、开发者、scm软件配置管理

    <!-- 基础信息-->
    <name>${project.groupId}:${project.artifactId}</name>
    <description>mybatis plugins</description>
    <url>https://github.com/bjlhx15/xgen</url>
    <!--开源协议-->
    <licenses>
        <license>
            <name>The Apache Software License, Version 2.0</name>
            <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
            <distribution>repo</distribution>
        </license>
    </licenses>
    <!--开发者信息-->
    <developers>
        <developer>
            <name>lihongxu</name>
            <email>bjlhx15@163.com</email>
            <url>https://github.com/bjlhx15/</url>
        </developer>
    </developers>
    <!-- 软件配置管理-->
    <scm>
        <url>https://github.com/bjlhx15/xgen</url>
        <connection>scm:git:https://github.com/bjlhx15/xgen.git</connection>
        <developerConnection>scm:git:https://github.com/bjlhx15/xgen.git</developerConnection>
    </scm>

5、打包部署

方式1、maven的方式

5.一、POM配置要上传的仓库地址

    <!--定义snapshots库和releases库的nexus地址-->
    <distributionManagement>
        <snapshotRepository>
            <id>sonatype-nexus-snapshots</id>
            <url>https://oss.sonatype.org/content/repositories/snapshots</url>
        </snapshotRepository>
        <repository>
            <id>sonatype-nexus-staging</id>
            <url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
        </repository>
    </distributionManagement>

5.二、配置上传中央仓库的用户名密码

  配置到 户密码配置  须要在maven_home/conf/settings.xml配置jira的帐号和密码【上文注册的】

与pom中的 

  <!--定义snapshots库和releases库的nexus地址-->

  <distributionManagement>

对应
 <server>
    <id>sonatype-nexus-snapshots</id>
    <username>https://issues.sonatype.org的帐号</username>
    <password>https://issues.sonatype.org的密码</password>
  </server>
  <server>
    <id>sonatype-nexus-staging</id>
    <username>https://issues.sonatype.org的帐号</username>
    <password>https://issues.sonatype.org的密码</password>
  </server>

5.三、POM 配置生成javadoc和sources包的插件【使用第五部分的profiles】

  新建组件的时候,必须发布javadoc和sources上去中央仓库,具体命名是经过你在 pom中定义的 groupId,artifactId和version,这个java开发者通常都很熟悉了,再也不说明。

  最终打包的要求知足如下格式:artifactId-version-classifier.packaging 

    其中, artifactId 和 version 就不说了;关于classifier就是对这个包的说明,好比javadoc,或是sources, packaging表示包的形式,好比war、jar等

5.四、GPG加密和自动签名的插件

a》基础密钥生成

  使用gpg或者pgp对文件进行签名,发布到Maven仓库中的全部文件都要使用GPG签名,以保障完整性。所以,咱们须要在本地安装并配置GPG。

  通常的发布文件都有一个对应的签名文件,即 .asc 文件

  注、使用gpg生成密钥,关于GPG的安装使用:http://www.javashuo.com/article/p-vddenoov-em.html

mac安装:brew install gpg

  基本使用:gpg --version

  生成密钥对:gpg --gen-key

    期间会提示用户名,邮箱  输入密码等,能够看到生成了并放到用户的文件夹下,包含公钥和私钥,最下面那部分就是生成的结果。其中输入密码是乱码,由于是中文系统元婴

  查看本机包含的密钥对: gpg --list-keys  ;pub表示是公钥,而sub表示私钥。

  发布公钥到GPG key-servers,很是的关键的,Sonatype 公司须要一个公钥来验证你deploy的文件,默认发送send-keys 应该是pub_key_id , D17AD4F314772283A415624……

gpg --keyserver hkp://pool.sks-keyservers.net:11371 --send-keys D17AD4F314772283A41562431AF7567……
gpg --keyserver hkp://pgp.mit.edu:11371 --send-keys D17AD4F314772283A41562431AF7567……
gpg --keyserver hkp://keyserver.ubuntu.com:11371 --send-keys D17AD4F314772283A41562431AF7567……

   上传时候老是报错

    

尝试使用如下上传

gpg --keyserver hkp://keyserver.ubuntu.com:80 --send-keys D17AD4F314772283A41562431AF7567……

使用查看命令查看

gpg --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys D17AD4F314772283A41562431AF7567…… 

b》POM 配置【使用第五部分的profiles】

5.五、使用Profile修改pom文件

应该javadoc和source的jar包生成也须要使用gpg来签名,因此很浪费时间,并且这些执行一般都独立于标准构建流程,因此把他们移动到一个profile.

由于插件也须要下载,可使用pom中的dependencies下载后 在删除掉

        <dependency>
            <groupId>commons-beanutils</groupId>
            <artifactId>commons-beanutils-core</artifactId>
            <version>1.8.3</version>
        </dependency>

        <!--打包插件-->
        <dependency>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-release-plugin</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.sonatype.plugins</groupId>
            <artifactId>nexus-staging-maven-plugin</artifactId>
            <version>1.6.8</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-gpg-plugin</artifactId>
            <version>1.5</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-javadoc-plugin</artifactId>
            <version>2.9.1</version>
            <scope>provided</scope>
        </dependency>
View Code

如下是环境配置

    <!--综合打包信息-->
    <profiles>
        <!--快照版本-->
        <!--mvn clean deploy -P snapshots -Dmaven.test.skip=true -->
        <profile>
            <id>snapshots</id>
            <build>
                <plugins>
                    <!--用来将工程发布到中央仓库-->
                    <plugin>
                        <groupId>org.sonatype.plugins</groupId>
                        <artifactId>nexus-staging-maven-plugin</artifactId>
                        <version>1.6.8</version>
                        <extensions>true</extensions>
                        <configuration>
                            <serverId>sonatype-nexus-snapshots</serverId>
                            <nexusUrl>https://oss.sonatype.org/</nexusUrl>
                            <autoReleaseAfterClose>true</autoReleaseAfterClose>
                        </configuration>
                    </plugin>
                    <!-- maven relaese-->
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-release-plugin</artifactId>
                        <version>2.5</version>
                        <configuration>
                            <autoVersionSubmodules>true</autoVersionSubmodules>
                            <useReleaseProfile>false</useReleaseProfile>
                            <releaseProfiles>release</releaseProfiles>
                            <goals>deploy</goals>
                        </configuration>
                    </plugin>
                    <!-- maven 编译-->
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-compiler-plugin</artifactId>
                        <version>3.0</version>
                        <configuration>
                            <source>1.8</source>
                            <target>1.8</target>
                        </configuration>
                    </plugin>
                    <!-- 源码打包插件-->
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-source-plugin</artifactId>
                        <version>2.2.1</version>
                        <executions>
                            <execution>
                                <id>attach-sources</id>
                                <goals>
                                    <goal>jar-no-fork</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                    <!--生成Javadoc,关闭doclint,避免注解检查不经过-->
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-javadoc-plugin</artifactId>
                        <version>2.9.1</version>
                        <executions>
                            <execution>
                                <id>attach-javadocs</id>
                                <goals>
                                    <goal>jar</goal>
                                </goals>
                            </execution>
                        </executions>
                        <configuration>
                            <!-- 解决 java8 下发布到 maven 中心库时,生成 doc 的异常 -->
                            <additionalparam>-Xdoclint:none</additionalparam>
                            <encoding>${project.build.sourceEncoding}</encoding>
                            <outputDirectory>${basedir}</outputDirectory>
                            <reportOutputDirectory>${basedir}</reportOutputDirectory>
                        </configuration>
                    </plugin>

                    <!-- GPG自动签名的插件-->
                    <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>

        <!--release-->
        <!--mvn clean deploy -P release -Dmaven.test.skip=true -->
        <profile>
            <id>release</id>
            <build>
                <plugins>
                    <!--用来将工程发布到中央仓库-->
                    <plugin>
                        <groupId>org.sonatype.plugins</groupId>
                        <artifactId>nexus-staging-maven-plugin</artifactId>
                        <version>1.6.8</version>
                        <extensions>true</extensions>
                        <configuration>
                            <serverId>sonatype-nexus-staging</serverId>
                            <nexusUrl>https://oss.sonatype.org/</nexusUrl>
                            <autoReleaseAfterClose>true</autoReleaseAfterClose>
                        </configuration>
                    </plugin>
                    <!-- maven relaese-->
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-release-plugin</artifactId>
                        <version>2.5</version>
                        <configuration>
                            <autoVersionSubmodules>true</autoVersionSubmodules>
                            <useReleaseProfile>false</useReleaseProfile>
                            <releaseProfiles>release</releaseProfiles>
                            <goals>deploy</goals>
                        </configuration>
                    </plugin>
                    <!-- maven 编译-->
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-compiler-plugin</artifactId>
                        <version>3.0</version>
                        <configuration>
                            <source>1.8</source>
                            <target>1.8</target>
                        </configuration>
                    </plugin>
                    <!-- 源码打包插件-->
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-source-plugin</artifactId>
                        <version>2.2.1</version>
                        <executions>
                            <execution>
                                <id>attach-sources</id>
                                <goals>
                                    <goal>jar-no-fork</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                    <!--生成Javadoc,关闭doclint,避免注解检查不经过-->
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-javadoc-plugin</artifactId>
                        <version>2.9.1</version>
                        <executions>
                            <execution>
                                <id>attach-javadocs</id>
                                <goals>
                                    <goal>jar</goal>
                                </goals>
                            </execution>
                        </executions>
                        <configuration>
                            <!-- 解决 java8 下发布到 maven 中心库时,生成 doc 的异常 -->
                            <additionalparam>-Xdoclint:none</additionalparam>
                            <encoding>${project.build.sourceEncoding}</encoding>
                            <outputDirectory>${basedir}</outputDirectory>
                            <reportOutputDirectory>${basedir}</reportOutputDirectory>
                        </configuration>
                    </plugin>

                    <!-- GPG自动签名的插件-->
                    <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>

5.六、编译打包上传

5.6.一、快照版本【上传后便可使用】

mvn clean deploy -P snapshots -Dmaven.test.skip=true

测试在pom上配置

    <repositories>
        <repository>
            <id>sonatype-snapshots</id>
            <name>snapshot</name>
            <url>https://oss.sonatype.org/content/repositories/snapshots/</url>
            <snapshots>
                <enabled>true</enabled>
                <!-- 4种频率类型。若是配置间隔时间更新,能够写做 interval:XX (XX是间隔分钟数)。daily配置是默认值。-->
                <!-- A. always是每次都去远程仓库查看是否有更新,-->
                <!-- B. daily是只在天天第一次的时候查看是否有更新,当天的其它时候则不会查看;-->
                <!-- C .interval容许设置一个分钟为单位的间隔时间,在这个间隔时间内只会去远程仓库中查找一次;-->
                <!-- D .never是不会去远程仓库中查找(这种就和正式版本的行为同样了)。-->
                <updatePolicy>interval:5</updatePolicy>
            </snapshots>
        </repository>
    </repositories>

 

5.6.二、release版本

能够继续增长一个profile配置 

mvn clean deploy -P release -Dmaven.test.skip=true

注意修改nexus-staging-maven-plugin的configuration地址serverId

使用时候直接配置依赖便可

5.6.三、注意事项问题点

问题1、针对上述,出现问题:

  gpg: signing failed: Inappropriate ioctl for device

   Failed to execute goal org.apache.maven.plugins:maven-gpg-plugin:1.6:sign (sign-artifacts) on project xgen: Exit code: 2 -> [Help 1]  

解决方案:

尝试1、使用的版本是maven3.X,其执行maven-ant-plugin的行为和maven2.x有必定的不一样,须要引入pluginManagement

  <build>   <plugins>      <plugin></plugin>增长节点  <build>   <pluginManagement>   <plugins>      <plugin></plugin>

尝试2、增长跳过test处理

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>maven-gpg-plugin</artifactId>
    <configuration>
        <skip>true</skip>
    </configuration>
</plugin>

尝试3、在运行上述命令窗口:export GPG_TTY=$(tty),再次运行,能够弹出输入密码窗口

问题2、Return code is: 401, ReasonPhrase: Unauthorized. -> [Help 1]

  注意修改本机maven的指向setting ,能够经过mvn -X 查看 

  maven-help-plugin是一个小巧的辅助工具,最简单的help:system能够打印全部可用的环境变量和Java系统属性。help:effective-pom和help:effective-settings最为有用。

  help:effective-pom用于查看当前生效的POM内容,指合并了全部父POM(包括Super POM)后的XML,因此可用于检测POM中某个配置是否生效

  effective-settings可用于查看当前生效的settings.xml文件内容,因此可用于判断某个settings配置是否生效

因为release版本是必需要签名的,因此须要测试签名,

方法1、在maven的settings.xml中配置gpg的签名 :(须要先用gpg来生成 a 步骤)

    <!-- 环境变量预POM对应 -->
    <profile>
      <id>sonatype-nexus-snapshots</id>
      <activation>
        <activeByDefault>true</activeByDefault>
      </activation>
      <properties>
        <!-- 此处根据GPG版本填写,<2.0的填写GPG,2.0以上的填写gpg2 -->
        <gpg.executable>gpg2</gpg.executable>
        <gpg.passphrase>密码</gpg.passphrase>
      </properties>
    </profile>

方法2、增长编译参数  

mvn clean deploy -Prelease -Dmaven.test.skip=true -Dgpg.passphrase=<输入你的密码>

问题:zsh: no matches found: -Dgpg.passphrase=密码

  具体缘由:由于zsh缺省状况下始终本身解释这个 *.h,而不会传递给 find 来解释。

  解决办法:在~/.zshrc中加入:setopt no_nomatch, 而后进行source .zshrc命令

5.七、登陆https://oss.sonatype.org/#welcome,使用上述帐号便可,在 OSS 中发布构件

会在中间仓库中Staging Repositories 菜单中看到本身刚刚上传的jar 包,

若是没有错的话,选中它,close关闭后,而后点Release 就能够同步到中央仓库了,通常过十分钟左右就能在http://search.maven.org/上边搜到了。

若是在配置中有配置:<autoReleaseAfterClose>true</autoReleaseAfterClose>,那么会自动关闭,自动发布。可能就不须要上述操做了。而后也搜索不到的。

5.八、通知 Sonatype 构件已成功发布

一、 在以前的在Issue下面回复

在 Issue 下面回复一条“构件已成功发布”的评论,这是为了通知 Sonatype 的工做人员为须要发布的构件作审批,发布后会关闭该 Issue。

这个前面的 Sonatype 工做人员其实在审核 Issue 时,在 comment 中已经提示了.https://issues.sonatype.org

二、等待构件审批经过

当审批经过后,将会收到邮件通知。通常一天左右

三、从中央仓库中搜索构件

这时,就能够在 maven 的中央仓库中搜索到本身发布的构件了,之后能够直接在 pom.xml 中使用了!

中央仓库搜索网站:http://search.maven.org/

第一次成功发布以后,之后就不用这么麻烦了,能够直接使用 groupId 发布任何的构件,固然前提是 groupId 没有变。

四、后续其余jar的发布流程

a)构件完成后直接使用 maven 在命令行上传构建;固然groupId 没有变的状况下,若是变更就须要从新申请工单

b)在 https://oss.sonatype.org/ close 并 release 构件;

c) 等待同步好(大约2小时多)以后,就可使用了

相关文章
相关标签/搜索