发布开源项目至maven中央仓库,内附打scala源码包,scala doc 包的教程。

背景

自己构思一个好的项目到github,然后发布到maven,供别人通过maven进行依赖使用。然后写点文档什么的,就可以到github骗小星星了。

实际操作

  1. 到网站https://issues.sonatype.org/,注册账号,该网是外网,需要自备小梯子。。注册时需要设置密码,看别人的博客说密码要求挺高,我一般都是用chrome浏览器自动生成的,然后用小本子记下来,看着是挺复杂的,强烈建议密码用小本子记下来
  2. 注册完以后,登陆网站。
  3. 点击网站导航上的Create 创建一个Issue。
    Project: Community Support - Open Source Project Repository Hosting (OSSRH)
    Issue TypeRequired: New Project
    Summary: 概述
    Group Id:io.github.thirdparty-core
    Project URL:项目站点,如:https://github.com/thirdparty-core/kernel
    SCM url:项目源码仓库,如:https://github.com/thirdparty-core/kernel.git
    
    发布开源项目至maven中央仓库,内附打scala源码包,scala doc 包的教程。_第1张图片
    发布开源项目至maven中央仓库,内附打scala源码包,scala doc 包的教程。_第2张图片
    创建完后还可以再修改的。
  4. 创建完后,具体如下:
    发布开源项目至maven中央仓库,内附打scala源码包,scala doc 包的教程。_第3张图片
  5. 创建过程中会与工作人员有交流,比如确保Group Id是你所有,如果不是你所有,他也会给你提供解决方案,直到他回复你:
    io.github.thirdparty-core has been prepared, now user(s) bugboy can:
    
    Deploy snapshot artifacts into repository https://oss.sonatype.org/content/repositories/snapshots
    Deploy release artifacts into the staging repository https://oss.sonatype.org/service/local/staging/deploy/maven2
    Release staged artifacts into repository 'Releases'
    please comment on this ticket when you promoted your first release, thanks
    

到这,说明你可以进行release你的项目了。

  1. 准备pgp的公私钥。

    1. 安装gpg。gpg(GunPG)是一款用于生成秘钥的加密软件。下载地址https://www.gnupg.org/download/,可以根据自己的系统选择对应的版本下载。我是用的是window,所以我选择Gpg4win。
      发布开源项目至maven中央仓库,内附打scala源码包,scala doc 包的教程。_第4张图片
    2. 下载完成后进行安装。安装完成后进入到cmd,运行gpg --version校验是否安装成功。
    3. 运行gpg --gen-key 生成密钥对,生成时会让你提示输入用户名密码邮箱,输入的密码需要记住,后续Release时会用到,建议使用注册https://issues.sonatype.org/网站用户时的密码,并用小本子记录好。
    4. 运行gpg --list-keys 查看公钥。
    5. gpg --keyserver hkp://keyserver.ubuntu.com:11371 --send-keys <公钥key>将公钥信息发送到ubuntu.com服务器,后续推送maven仓库会做校验。
    6. gpg --keyserver hkp://keyserver.ubuntu.com:11371 --recv-keys <公钥key>查询公钥是否推送成功。
  2. 准备要发布的项目。下面以我自己的项目为例。

    1. 项目结构如下:
      发布开源项目至maven中央仓库,内附打scala源码包,scala doc 包的教程。_第5张图片
      我的项目结构是多module的,其中spark-hbase为scala项目。

    2. parent pom配置如下:

      
      <project xmlns="http://maven.apache.org/POM/4.0.0"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
          <modelVersion>4.0.0modelVersion>
      
          <groupId>io.github.thirdparty-coregroupId>
          <artifactId>kernelartifactId>
          <packaging>pompackaging>
          <version>1.0.1-preversion>
          <name>kernelname>
          <description>Some core third-party implementationsdescription>
          <url>https://github.com/thirdparty-core/kernelurl>
          <modules>
              <module>spark-hbasemodule>
          modules>
      
          
          <distributionManagement>
              <snapshotRepository>
                  <id>sonatype-nexus-snapshotsid>
                  <name>Sonatype Nexus Snapshotsname>
                  <url>https://oss.sonatype.org/content/repositories/snapshotsurl>
              snapshotRepository>
              <repository>
                  <id>sonatype-nexus-stagingid>
                  <name>Nexus Release Repositoryname>
                  <url>https://oss.sonatype.org/service/local/staging/deploy/maven2url>
              repository>
          distributionManagement>
      
          <licenses>
              <license>
                  <name>The Apache Software License, Version 2.0name>
                  <url>http://www.apache.org/licenses/LICENSE-2.0.txturl>
                  <distribution>repodistribution>
              license>
          licenses>
          <scm>
              <url>https://github.com/thirdparty-core/kernel.giturl>
              <connection>scm:git:git://github.com/thirdparty-core/kernel.gitconnection>
              <developerConnection>scm:git:ssh://github.com/thirdparty-coredeveloperConnection>
          scm>
      
          <developers>
              <developer>
                  <name>bugboyname>
                  <email>[email protected]email>
                  <url>https://github.com/thirdparty-coreurl>
              developer>
          developers>
      
          <build>
              <plugins>
                  <plugin>
                      <groupId>org.apache.maven.pluginsgroupId>
                      <artifactId>maven-gpg-pluginartifactId>
                      <version>1.6version>
                      <executions>
                          <execution>
                              <id>sign-artifactsid>
                              <phase>verifyphase>
                              <goals>
                                  <goal>signgoal>
                              goals>
                          execution>
                      executions>
                  plugin>
                  
                  <plugin>
                      <groupId>org.apache.maven.pluginsgroupId>
                      <artifactId>maven-release-pluginartifactId>
                      <version>2.5.3version>
                      <configuration>
                          <autoVersionSubmodules>trueautoVersionSubmodules>
                          <useReleaseProfile>falseuseReleaseProfile>
                          <releaseProfiles>releasereleaseProfiles>
                          <goals>deploygoals>
                      configuration>
                  plugin>
              plugins>
          build>
      project>           
      
    3. spark-hbase的pom如下:

      
      <project xmlns="http://maven.apache.org/POM/4.0.0"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
          <parent>
              <artifactId>kernelartifactId>
              <groupId>io.github.thirdparty-coregroupId>
              <version>1.0.1-preversion>
          parent>
          <modelVersion>4.0.0modelVersion>
      
          <artifactId>spark-hbaseartifactId>
          <version>1.0.1-preversion>
          <packaging>jarpackaging>
          <name>spark-hbasename>
          <properties>
              <spark.version>2.4.4spark.version>
              <hadoop.version>2.8.5hadoop.version>
              <hbase.version>2.2.2hbase.version>
              <scala.version>2.11.12scala.version>
              <scala.lib.version>2.11scala.lib.version>
              <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
              <scope>providedscope>
              
          properties>
      
          <dependencyManagement>
              <dependencies>
                  <dependency>
                      <groupId>org.glassfishgroupId>
                      <artifactId>javax.elartifactId>
                      <version>3.0.1-b08version>
                  dependency>
              dependencies>
          dependencyManagement>
          <dependencies>
              <dependency>
                  <groupId>org.scala-langgroupId>
                  <artifactId>scala-libraryartifactId>
                  <version>${scala.version}version>
                  <scope>providedscope>
              dependency>
      
              <dependency>
                  <groupId>org.apache.sparkgroupId>
                  <artifactId>spark-core_${scala.lib.version}artifactId>
                  <version>${spark.version}version>
                  <scope>${scope}scope>
                  <exclusions>
                      <exclusion>
                          <artifactId>org.scala-langartifactId>
                          <groupId>scala-librarygroupId>
                      exclusion>
                  exclusions>
              dependency>
      
              <dependency>
                  <groupId>org.apache.sparkgroupId>
                  <artifactId>spark-sql_${scala.lib.version}artifactId>
                  <version>${spark.version}version>
                  <scope>${scope}scope>
                  <exclusions>
                      <exclusion>
                          <artifactId>org.scala-langartifactId>
                          <groupId>scala-librarygroupId>
                      exclusion>
                  exclusions>
              dependency>
      
              <dependency>
                  <groupId>org.apache.hadoopgroupId>
                  <artifactId>hadoop-clientartifactId>
                  <version>${hadoop.version}version>
                  <scope>${scope}scope>
              dependency>
      
              <dependency>
                  <groupId>org.apache.hbasegroupId>
                  <artifactId>hbase-clientartifactId>
                  <version>${hbase.version}version>
                  <scope>${scope}scope>
              dependency>
      
              <dependency>
                  <groupId>org.apache.hbasegroupId>
                  <artifactId>hbase-serverartifactId>
                  <version>${hbase.version}version>
                  <scope>${scope}scope>
              dependency>
      
              <dependency>
                  <groupId>org.apache.hbasegroupId>
                  <artifactId>hbase-mapreduceartifactId>
                  <version>${hbase.version}version>
                  <scope>${scope}scope>
              dependency>
      
              <dependency>
                  <groupId>org.apache.hbasegroupId>
                  <artifactId>hbase-commonartifactId>
                  <version>${hbase.version}version>
                  <scope>${scope}scope>
              dependency>
      
              <dependency>
                  <groupId>org.apache.hbasegroupId>
                  <artifactId>hbase-testing-utilartifactId>
                  <version>${hbase.version}version>
                  <scope>testscope>
              dependency>
          dependencies>
          <build>
              <plugins>
                  
                  <plugin>
                      <groupId>net.alchim31.mavengroupId>
                      <artifactId>scala-maven-pluginartifactId>
                      <version>3.2.0version>
                      <configuration>
                          <charset>${project.build.sourceEncoding}charset>
                          <scalaVersion>${scala.version}scalaVersion>
                          <args>
                              <arg>-featurearg>
                              
                              <arg>-target:jvm-1.8arg>
                          args>
                          <source>1.8source>
                          <target>1.8target>
                      configuration>
                      <executions>
                          <execution>
                              <id>scala-compile-firstid>
                              <phase>process-resourcesphase>
                              <goals>
                                  <goal>add-sourcegoal>
                                  <goal>compilegoal>
                              goals>
                          execution>
                          <execution>
                              <id>scala-test-compileid>
                              <phase>process-test-resourcesphase>
                              <goals>
                                  <goal>testCompilegoal>
                              goals>
                          execution>
                      executions>
                  plugin>
                  
                  <plugin>
                      <groupId>org.apache.maven.pluginsgroupId>
                      <artifactId>maven-javadoc-pluginartifactId>
                      <version>2.9.1version>
                      <executions>
                          <execution>
                              <phase>packagephase>
                              <goals>
                                  <goal>jargoal>
                              goals>
                          execution>
                      executions>
                  plugin>
                  
                  <plugin>
                      <groupId>org.apache.maven.pluginsgroupId>
                      <artifactId>maven-source-pluginartifactId>
                      <version>3.0.1version>
                      <executions>
                          <execution>
                              <id>attach-sourcesid>
                              <goals>
                                  <goal>jar-no-forkgoal>
                              goals>
                          execution>
                      executions>
                  plugin>
                  
                  <plugin>
                      <groupId>org.apache.maven.pluginsgroupId>
                      <artifactId>maven-compiler-pluginartifactId>
                      <version>3.6.0version>
                      <configuration>
                          <encoding>utf-8encoding>
                          <source>1.8source>
                          <target>1.8target>
                      configuration>
                  plugin>
              plugins>
          build>
      project>
      

      注意,进行发布的时候,必须要有发布包,源码包,文档包。

    4. 配置项目maven,如下:
      发布开源项目至maven中央仓库,内附打scala源码包,scala doc 包的教程。_第6张图片

    5. 修改maven的setting.xml文件,看到我maven在我自己目录下的.m2/setting.xml,该文件我没有找到,我是下载的maven安装包中,将安装包中的conf下的setting.xml拷贝过来放这的。编辑该文件,在services节点下添加:

      <servers>
         
          <server>
            <id>sonatype-nexus-snapshotsid>
            <username>bugboyusername>
            <password>上文中网站注册时的密码password>
          server>
          <server>
            <id>sonatype-nexus-stagingid>
            <username>bugboyusername>
            <password>上文中网站注册时的密码password>
          server>
        servers>
      

      因为要用到好多密码,所以为了方便不混淆,整个过程都用注册时的密码,所以用小本子记录下来。

  3. 编译项目,打包。

    1. 命令行进入到root pom所在的目录,运行mvn install
    2. 1运行完后,会在root 的target临时目录下看到最后结果的pom。以及在spark-hbase的target下会看到相关包,包括要不发的包,源码包,但是我这是scala项目,所以并不会自动生成scala的文档包。解决方案是命令行进入到spark-hbase pom所在的目录,运行mvn scala:doc,之后会看到在spark-hbase的target目录下生成一个site目录,里面有一个scaladocs文件夹。此时,在target目录下新建一个名为apidocs的目录,并将site目录中的scaladocs拷贝到apidocs目录下,注意目录名不更更改。
    3. 再root pom所在的目录下运行一遍mvn install,结束后会在spark-hbase的target目录下看到*-javadoc.jar,*-sources.jar,*.jar等文件,此时要发布的文件都已经准备好了。
  4. 发布。运行mvn deploy,会看到发布过程中会进行相关文件的上传。外网,有点慢。

  5. 上传结束后,访问https://oss.sonatype.org/#stagingRepositories查看发布好的构件,点击左侧的Staging Repositories,会看到你发布的构建,其根据group-id-1001来命名的,并且每成功上传一次,后面的编号自增一次。选择最大的编号即为最新发布的。选择要发布的构建,此时状态为Open

  6. 选中构件,并点击上方的 Close–>Confirm 在下边的Activity选项卡中查看状态。注意,Close时会分步骤检查你发布的构建是否都包含它要求需要的东西,其中root pom必须要有

     kernel
     Some core third-party implementations
     https://github.com/thirdparty-core/kernel
    

    否则检查不通过,会报没有相关信息的错误。对于如果module发布的是jar,则必须有发布包,源码包,文档包
    发布开源项目至maven中央仓库,内附打scala源码包,scala doc 包的教程。_第7张图片

  7. Close结束后,可查看页面底部的检查信息,如下表示检查通过,
    发布开源项目至maven中央仓库,内附打scala源码包,scala doc 包的教程。_第8张图片

  8. 当状态变成closed后,执行 Release–>Confirm 并在下边的Activity选项卡中查看状态。成功后构件自动删除,一小段时间(约1-2个小时)后即可同步到maven的中央仓库。届时会有邮件通知。

  9. 之后到Issue增加Comment,留言致谢并表示发布已经完成,请工作人员关闭Issue。

  10. 搜索自己发布的包:

    https://search.maven.org/

    或者新建一个maven项目,依赖测试以下。

参考文章:https://blog.csdn.net/sinat_23290725/article/details/85018092
致谢!

你可能感兴趣的:(Spark)