背景
自己構思一個好的項目到github,然後發佈到maven,供別人通過maven進行依賴使用。然後寫點文檔什麼的,就可以到github騙小星星了。
實際操作
- 到網站https://issues.sonatype.org/,註冊賬號,該網是外網,需要自備小梯子。。註冊時需要設置密碼,看別人的博客說密碼要求挺高,我一般都是用chrome瀏覽器自動生成的,然後用小本子記下來,看着是挺複雜的,強烈建議密碼用小本子記下來。
- 註冊完以後,登陸網站。
- 點擊網站導航上的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
創建完後還可以再修改的。 - 創建完後,具體如下:
- 創建過程中會與工作人員有交流,比如確保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你的項目了。
-
準備pgp的公私鑰。
- 安裝gpg。gpg(GunPG)是一款用於生成祕鑰的加密軟件。下載地址https://www.gnupg.org/download/,可以根據自己的系統選擇對應的版本下載。我是用的是window,所以我選擇Gpg4win。
- 下載完成後進行安裝。安裝完成後進入到cmd,運行
gpg --version
校驗是否安裝成功。 - 運行
gpg --gen-key
生成密鑰對,生成時會讓你提示輸入用戶名密碼郵箱,輸入的密碼需要記住,後續Release時會用到,建議使用註冊https://issues.sonatype.org/網站用戶時的密碼,並用小本子記錄好。 - 運行
gpg --list-keys
查看公鑰。 gpg --keyserver hkp://keyserver.ubuntu.com:11371 --send-keys <公鑰key>
將公鑰信息發送到ubuntu.com服務器,後續推送maven倉庫會做校驗。gpg --keyserver hkp://keyserver.ubuntu.com:11371 --recv-keys <公鑰key>
查詢公鑰是否推送成功。
- 安裝gpg。gpg(GunPG)是一款用於生成祕鑰的加密軟件。下載地址https://www.gnupg.org/download/,可以根據自己的系統選擇對應的版本下載。我是用的是window,所以我選擇Gpg4win。
-
準備要發佈的項目。下面以我自己的項目爲例。
-
項目結構如下:
我的項目結構是多module的,其中spark-hbase爲scala項目。 -
parent pom配置如下:
<?xml version="1.0" encoding="UTF-8"?> <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.0</modelVersion> <groupId>io.github.thirdparty-core</groupId> <artifactId>kernel</artifactId> <packaging>pom</packaging> <version>1.0.1-pre</version> <name>kernel</name> <description>Some core third-party implementations</description> <url>https://github.com/thirdparty-core/kernel</url> <modules> <module>spark-hbase</module> </modules> <!--pom example https://central.sonatype.org/pages/requirements.html#a-complete-example-pom--> <distributionManagement> <snapshotRepository> <id>sonatype-nexus-snapshots</id> <name>Sonatype Nexus Snapshots</name> <url>https://oss.sonatype.org/content/repositories/snapshots</url> </snapshotRepository> <repository> <id>sonatype-nexus-staging</id> <name>Nexus Release Repository</name> <url>https://oss.sonatype.org/service/local/staging/deploy/maven2</url> </repository> </distributionManagement> <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> <scm> <url>https://github.com/thirdparty-core/kernel.git</url> <connection>scm:git:git://github.com/thirdparty-core/kernel.git</connection> <developerConnection>scm:git:ssh://github.com/thirdparty-core</developerConnection> </scm> <developers> <developer> <name>bugboy</name> <email>[email protected]</email> <url>https://github.com/thirdparty-core</url> </developer> </developers> <build> <plugins> <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> <!--https://oss.sonatype.org/#stagingRepositories--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-release-plugin</artifactId> <version>2.5.3</version> <configuration> <autoVersionSubmodules>true</autoVersionSubmodules> <useReleaseProfile>false</useReleaseProfile> <releaseProfiles>release</releaseProfiles> <goals>deploy</goals> </configuration> </plugin> </plugins> </build> </project>
-
spark-hbase的pom如下:
<?xml version="1.0" encoding="UTF-8"?> <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>kernel</artifactId> <groupId>io.github.thirdparty-core</groupId> <version>1.0.1-pre</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>spark-hbase</artifactId> <version>1.0.1-pre</version> <packaging>jar</packaging> <name>spark-hbase</name> <properties> <spark.version>2.4.4</spark.version> <hadoop.version>2.8.5</hadoop.version> <hbase.version>2.2.2</hbase.version> <scala.version>2.11.12</scala.version> <scala.lib.version>2.11</scala.lib.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <scope>provided</scope> <!--<scope>compile</scope>--> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.glassfish</groupId> <artifactId>javax.el</artifactId> <version>3.0.1-b08</version> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.scala-lang</groupId> <artifactId>scala-library</artifactId> <version>${scala.version}</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.apache.spark</groupId> <artifactId>spark-core_${scala.lib.version}</artifactId> <version>${spark.version}</version> <scope>${scope}</scope> <exclusions> <exclusion> <artifactId>org.scala-lang</artifactId> <groupId>scala-library</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.apache.spark</groupId> <artifactId>spark-sql_${scala.lib.version}</artifactId> <version>${spark.version}</version> <scope>${scope}</scope> <exclusions> <exclusion> <artifactId>org.scala-lang</artifactId> <groupId>scala-library</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-client</artifactId> <version>${hadoop.version}</version> <scope>${scope}</scope> </dependency> <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-client</artifactId> <version>${hbase.version}</version> <scope>${scope}</scope> </dependency> <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-server</artifactId> <version>${hbase.version}</version> <scope>${scope}</scope> </dependency> <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-mapreduce</artifactId> <version>${hbase.version}</version> <scope>${scope}</scope> </dependency> <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-common</artifactId> <version>${hbase.version}</version> <scope>${scope}</scope> </dependency> <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-testing-util</artifactId> <version>${hbase.version}</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <!-- scala編譯,d打scala源碼包等的插件 scala doc: 1. mvn scala:doc create scala doc in into target/site 2. create dir target/apidocs 3. mv target/site/scaladocs to target/apidocs 4. mvn install deploy --> <plugin> <groupId>net.alchim31.maven</groupId> <artifactId>scala-maven-plugin</artifactId> <version>3.2.0</version> <configuration> <charset>${project.build.sourceEncoding}</charset> <scalaVersion>${scala.version}</scalaVersion> <args> <arg>-feature</arg> <!--The target.jvm variable gets set above by the groovy snippet in the gmaven-plugin.--> <arg>-target:jvm-1.8</arg> </args> <source>1.8</source> <target>1.8</target> </configuration> <executions> <execution> <id>scala-compile-first</id> <phase>process-resources</phase> <goals> <goal>add-source</goal> <goal>compile</goal> </goals> </execution> <execution> <id>scala-test-compile</id> <phase>process-test-resources</phase> <goals> <goal>testCompile</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> <!--生成java源碼包插件(僅對java有用,對scala不管用) source--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-source-plugin</artifactId> <version>3.0.1</version> <executions> <execution> <id>attach-sources</id> <goals> <goal>jar-no-fork</goal> </goals> </execution> </executions> </plugin> <!--源碼編譯插件 Compiler--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.6.0</version> <configuration> <encoding>utf-8</encoding> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project>
注意,進行發佈的時候,必須要有發佈包,源碼包,文檔包。
-
配置項目maven,如下:
-
修改maven的setting.xml文件,看到我maven在我自己目錄下的.m2/setting.xml,該文件我沒有找到,我是下載的maven安裝包中,將安裝包中的conf下的setting.xml拷貝過來放這的。編輯該文件,在services節點下添加:
<servers> <!--添加--> <server> <id>sonatype-nexus-snapshots</id> <username>bugboy</username> <password>上文中網站註冊時的密碼</password> </server> <server> <id>sonatype-nexus-staging</id> <username>bugboy</username> <password>上文中網站註冊時的密碼</password> </server> </servers>
因爲要用到好多密碼,所以爲了方便不混淆,整個過程都用註冊時的密碼,所以用小本子記錄下來。
-
-
編譯項目,打包。
- 命令行進入到root pom所在的目錄,運行
mvn install
- 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目錄下,注意目錄名不更更改。 - 再root pom所在的目錄下運行一遍
mvn install
,結束後會在spark-hbase的target目錄下看到*-javadoc.jar,*-sources.jar,*.jar
等文件,此時要發佈的文件都已經準備好了。
- 命令行進入到root pom所在的目錄,運行
-
發佈。運行
mvn deploy
,會看到發佈過程中會進行相關文件的上傳。外網,有點慢。 -
上傳結束後,訪問https://oss.sonatype.org/#stagingRepositories查看發佈好的構件,點擊左側的Staging Repositories,會看到你發佈的構建,其根據group-id-1001來命名的,並且每成功上傳一次,後面的編號自增一次。選擇最大的編號即爲最新發布的。選擇要發佈的構建,此時狀態爲Open
-
選中構件,並點擊上方的 Close–>Confirm 在下邊的Activity選項卡中查看狀態。注意,Close時會分步驟檢查你發佈的構建是否都包含它要求需要的東西,其中root pom必須要有
<name>kernel</name> <description>Some core third-party implementations</description> <url>https://github.com/thirdparty-core/kernel</url>
否則檢查不通過,會報沒有相關信息的錯誤。對於如果module發佈的是jar,則必須有發佈包,源碼包,文檔包
-
Close結束後,可查看頁面底部的檢查信息,如下表示檢查通過,
-
當狀態變成closed後,執行 Release–>Confirm 並在下邊的Activity選項卡中查看狀態。成功後構件自動刪除,一小段時間(約1-2個小時)後即可同步到maven的中央倉庫。屆時會有郵件通知。
-
之後到Issue增加Comment,留言致謝並表示發佈已經完成,請工作人員關閉Issue。
-
搜索自己發佈的包:
https://search.maven.org/
或者新建一個maven項目,依賴測試以下。
參考文章:https://blog.csdn.net/sinat_23290725/article/details/85018092
致謝!