概述
用maven已經一段時間,也搭建了公司内部的maven環境。然而有一些通用的可以開源的代碼想放到公網的倉庫中,以便可以随時使用(公司網絡因為經常切換,導緻maven庫常有無法導入的情況)。
注冊Sonatype OSSRH
關于如何注冊,可以看這篇文章:向maven中央倉庫送出jar
裡面介紹了如何注冊OSSRH賬号。我補充幾點:
- 項目groupId怎麼寫? 如果你是個人的名義,然後代碼是放到github上面的(比如是https://www.github.com/username/projectName的格式),那麼推薦groupId寫成:com.github.username, 不然可能會被拒絕(我就是一開始沒有寫對,被拒絕了。。。)
- 當project的status為
時才算是ok,如下圖所示RESOLVED
[Maven實戰]釋出maven項目到中央倉庫(Central Repository)概述注冊Sonatype OSSRH修改pom.xml修改maven的setting.xml執行mvn deploy釋出項目資源
修改pom.xml
賬号注冊完成,同時也建立了project後,就可以修改pom.xml了。
首先加入name,description,scm,developers等資訊
<name>${project.groupId}:${project.artifactId}</name>
<url>https://github.com/0604hx/nerve-tools</url>
<description></description>
<licenses>
<license>
<name>The Apache License, Version 2.0</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
</license>
</licenses>
<developers>
<developer>
<name>0604hx</name>
<email>[email protected]</email>
<roles>
<role>developer</role>
</roles>
<timezone>+8</timezone>
</developer>
</developers>
<scm>
<connection>scm:git:https://github.com/0604hx/nerve-tools.git</connection>
<developerConnection>scm:git:https://github.com/0604hx/nerve-tools.git</developerConnection>
<url>https://github.com/0604hx/nerve-tools</url>
<tag>v${project.version}</tag>
</scm>
然後添加
distributionManagement
<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>
接着添加plugins
<plugins>
<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId>
<version>1.6.3</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>2.10.3</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</goal>
</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>
修改maven的setting.xml
增加你的帳密資訊
然後設定gpg的profile
此時要先安裝gpg,詳細的說明請看:Working with PGP Signatures
執行mvn deploy
執行以下指令
mvn deploy -Dmaven.test.skip=true -e
等待結果即可。來個截圖哈
如果deploy時出現以下錯誤
gpg: no default secret key: No secret key
則表示你沒有建立secret key,此時建立key即可。
如果出現這樣的錯誤:
No public key:Key with id
就說明你沒有上傳keys到公共伺服器,上傳就行了:
釋出項目資源
上一步完成後,其實這是将我們的資源上傳到oss.sonatype而已,我們還有将其真正的釋出出去。步驟如下:
- 登入到oss.sonatype.com
- 點選 Staging Repositories,可以看到我們剛剛上傳的資源,如下圖所示
[Maven實戰]釋出maven項目到中央倉庫(Central Repository)概述注冊Sonatype OSSRH修改pom.xml修改maven的setting.xml執行mvn deploy釋出項目資源 - 選中相應的資源(處于open狀态),然後點選上方的“release”按鈕,會彈出确認框,直接确認即可。
- 上一步成功後,就表示釋出完成。此時去https://issues.sonatype.org(就是你一開始建立的issue)中留言,告訴管理者你已經release了。等稽核通過後,就可以在中央倉庫中搜尋出你的項目(是不是很激動,很有成就感^_^)。