JAVA開源軟件開發必備技能-發佈構建到maven中央倉庫

JAVA開源軟件開發必備技能

發佈構建到maven中央倉庫的方法和詳細步驟

  • sonatype介紹
  • 向sonatype提交申請
  • 配置maven
  • GPG簽名
  • 發佈版本
  • 通知sonatype

sonatype介紹

Maven項目託管在Apache上的,但是中央倉庫不是Apache的資源,中央倉庫是由Sonatype出資維護的。目前來說,http://repo1.maven.org/maven2/是真正的Maven中央倉庫的地址。Nexus倉庫就是Sonatype開發的,搭建maven私服普遍採用Nexus。

向sonatype提交申請

  • 首先需要註冊sonatype,地址:https://issues.sonatype.org/browse/OSSRH-19497?filter=-2
  • 創建issue,點擊Create,選擇New Project 這裏寫圖片描述
  • 提交申請,主要填寫項目簡介,groupId,projectUrl,scmUrl。這裏的groupId必須是自己擁有的域名,或者採用com.github.{user} 這裏寫圖片描述這裏寫圖片描述
  • 提交申請後,sonatype的工作人員會進行審覈,通過後issue的狀態變爲RESOLVED 這裏寫圖片描述

配置maven

  • 項目基本信息配置,主要包括名稱,描述,github地址,開發者信息等
  • <name>Gecco</name>
    <description>Easy to use lightweight web crawler</description>
    <url>https://github.com/xtuhcy/gecco</url>
    <scm>
    	<connection>scm:git:https://github.com/xtuhcy/gecco.git</connection>
    	<developerConnection>scm:git:https://github.com/xtuhcy/gecco.git</developerConnection>
    	<url>https://github.com/xtuhcy/gecco</url>
    	<tag>v1.0.0</tag>
    </scm>
    <developers>
    	<developer>
    		<name>xtuhcy</name>
    		<email>[email protected]</email>
    		<organization>geccocrawler</organization>
    		<organizationUrl>http://www.geccocrawler.com</organizationUrl>
    	</developer>
    </developers>
    <licenses>
    	<license>
    		<name>The MIT License (MIT)</name>
    		<url>https://raw.githubusercontent.com/xtuhcy/gecco/master/LICENSE</url>
    	</license>
    </licenses>
     
  • 配置source、javadoc打包和gpg簽名。上傳maven中央庫的構建必須包含源碼、javadoc,並且對構件jar、源碼jar、javadoc的jar進行gpg簽名
  • <profiles>
    	<profile>
    		<id>release</id>
    		<distributionManagement>
    			<snapshotRepository>
    				<id>ossrh</id>
    				<url>https://oss.sonatype.org/content/repositories/snapshots</url>
    			</snapshotRepository>
    			<repository>
    				<id>ossrh</id>
    				<url>https://oss.sonatype.org/service/local/staging/deploy/maven2</url>
    			</repository>
    		</distributionManagement>
    		<build>
    			<plugins>
    				<plugin>
    					<groupId>org.sonatype.plugins</groupId>
    					<artifactId>nexus-staging-maven-plugin</artifactId>
    					<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-release-plugin</artifactId>
    					<configuration>
    						<autoVersionSubmodules>true</autoVersionSubmodules>
    						<useReleaseProfile>false</useReleaseProfile>
    						<releaseProfiles>release</releaseProfiles>
    						<goals>deploy</goals>
    					</configuration>
    				</plugin>
    				<plugin>
    					<groupId>org.apache.maven.plugins</groupId>
    					<artifactId>maven-compiler-plugin</artifactId>
    					<configuration>
    						<source>1.6</source>
    						<target>1.6</target>
    						<encoding>UTF-8</encoding>
    					</configuration>
    				</plugin>
    				<plugin>
    					<groupId>org.apache.maven.plugins</groupId>
    					<artifactId>maven-gpg-plugin</artifactId>
    					<executions>
    						<execution>
    							<id>sign-artifacts</id>
    							<phase>verify</phase>
    							<goals>
    								<goal>sign</goal>
    							</goals>
    						</execution>
    					</executions>
    				</plugin>
    				<plugin>
    					<groupId>org.apache.maven.plugins</groupId>
    					<artifactId>maven-source-plugin</artifactId>
    					<configuration>
    						<encoding>UTF-8</encoding>
    					</configuration>
    					<executions>
    						<execution>
    							<id>attach-sources</id>
    							<goals>
    								<goal>jar-no-fork</goal>
    							</goals>
    						</execution>
    					</executions>
    				</plugin>
    				<plugin>
    					<groupId>org.apache.maven.plugins</groupId>
    					<artifactId>maven-javadoc-plugin</artifactId>
    					<configuration>
    						<encoding>UTF-8</encoding>
    					</configuration>
    					<executions>
    						<execution>
    							<id>attach-javadocs</id>
    							<goals>
    								<goal>jar</goal>
    							</goals>
    						</execution>
    					</executions>
    				</plugin>
    			</plugins>
    		</build>
    	</profile>
    </profiles>
     
  • 打包時只需要添加-P release即可執行上述構件jar、源碼jar、javadoc的jar以及簽名等一系列操作。如mvn deploy -P release
  • 配置上傳賬戶,修改maven的setting.xml,添加剛纔註冊的sonatype的賬號和密碼
  • <servers>
    	<server>
    		<id>ossrh</id>
    		<username>memory</username>
    		<password>your password</password>
    	</server>
    </servers>
     
  • 上述配置完成後還不能成功上傳中央庫,需要配置gpg簽名。生成密鑰。

GPG簽名

  • gpg簽名的主要目的就是生成公鑰、私鑰,並將公鑰上傳到gpg的密鑰服務器上,之後上傳maven服務器的文件都可以通過該私鑰進行數字簽名,以保證是您自己上傳到maven服務器上的。
  • 第一步,安裝gng window下載地址:https://www.gpg4win.org/download.html
  • 第二步,生成密鑰 gpg --gen-key 除了用戶名和郵箱已經密碼,其他均可以默認 生成密鑰生成密鑰後,可以通過gpg --list-keys查看 密鑰查看
  • 第三步,上傳公鑰到gpg服務器 gpg --keyserver hkp://pool.sks-keyservers.net --send-k eys 3E8E57E4 有時不成功,多嘗試幾次。

發佈版本

一切準備就緒,發佈版本,mvn deploy -P release。編譯後在簽名是需要提供剛纔生成gpg密鑰時輸入的密碼 輸入gpg密碼 到此,一個構件已經發布到中央庫,但是能正式下載還需要sonatype的工作人員審覈。

最後一步通知sonatype

回到sonatype網站的issue,回覆issue,等待審覈。審覈通過的結果如下: 這裏寫圖片描述 等待2個小時,您就可以在中央庫下載自己的jar了。

這裏說明一下自己使用的軟件環境:maven3.2.2,eclipse luna

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章