使用site-maven-plugin在github上搭建公有仓库
文章目錄
- 簡介
- 前期準(zhǔn)備
- 在maven中配置GitHub權(quán)限
- 配置deploy-plugin
- 配置site-maven-plugin
- 怎么使用這個共享的項目
- 總結(jié)
簡介
Maven是我們在開發(fā)java程序中經(jīng)常使用的構(gòu)建工具,在團隊合作開發(fā)過程中,如果我們想要將自己寫好的jar包共享給別人使用,通常需要自己搭建maven倉庫,然后將寫好的jar包上傳到maven倉庫中,以供其他用戶使用。
搭建maven倉庫需要服務(wù)器和域名,對公司而言域名和服務(wù)器多的是,但是如果是我們個人或者小團隊想共享一些非常有用的jar包給別人使用就太麻煩了。
最近Github好消息頻出,先是對個人用戶取消了repositories和協(xié)作用戶的個數(shù)限制,后面對于企業(yè)用戶也進行了升級和降價處理。如果倉庫不大的話,完全可以把倉庫搬到github上面去。
更多精彩內(nèi)容且看:
- 區(qū)塊鏈從入門到放棄系列教程-涵蓋密碼學(xué),超級賬本,以太坊,Libra,比特幣等持續(xù)更新
- Spring Boot 2.X系列教程:七天從無到有掌握Spring Boot-持續(xù)更新
- Spring 5.X系列教程:滿足你對Spring5的一切想象-持續(xù)更新
- java程序員從小工到專家成神之路(2020版)-持續(xù)更新中,附詳細(xì)文章教程
更多內(nèi)容請訪問www.flydean.com
前期準(zhǔn)備
要在github上面搭建maven倉庫,我們需要使用到maven的插件:site-maven-plugin。因為要連到github上面,所以需要設(shè)置github的oauth權(quán)限。直接用用戶名密碼也可以,但是這樣做不安全,我們并不推薦。
如上圖所示,在Settings->Developer settings->Personal access tokens中創(chuàng)建一個access tokens,所需權(quán)限如下:
注意,用戶這里的權(quán)限一定要選,否則后面會報異常。
有了權(quán)限,接下來我們再創(chuàng)建一個github-maven-repository,用來作為mvn倉庫存儲數(shù)據(jù)。
假如生成的地址是:https://github.com/flydean/github-maven-repository
在maven中配置GitHub權(quán)限
這一步我們需要編輯setting.xml文件,一般來說這個文件是在~/.m2/settings.xml。
我們需要添加一個Server,如果直接使用github的用戶名密碼,則像下面這樣:
<server><id>github</id><username>YOUR_USERNAME</username><password>YOUR_PASSWORD</password> </server>前面我們講到了直接使用用戶名是不安全的,我們可以使用上面創(chuàng)建的oauth key:
<server><id>github</id><password>OAUTH2TOKEN</password> </server>這個id會在后面的pom.xml文件配置中用到,這里我們先記下來。
配置deploy-plugin
我們的目標(biāo)是生成包含jar包的maven依賴。在將jar包上傳到遠(yuǎn)程倉庫之前,我們需要在本地先生成。
先配置一個本地的repository:
<distributionManagement><repository><id>maven.repo</id><name>Local Staging Repository</name><url>file://${project.build.directory}/mvn-repo</url></repository></distributionManagement>上面我們指定了在項目的build目錄下面創(chuàng)建了mvn-repo用來存儲本地打好的package。
接下來,我們需要使用maven-deploy-plugin指定將打好的包部署到剛剛我們指定的local倉庫中。
<plugin><artifactId>maven-deploy-plugin</artifactId><version>2.8.2</version><configuration><altDeploymentRepository>maven.repo::default::file://${project.build.directory}/mvn-repo</altDeploymentRepository></configuration></plugin>配置site-maven-plugin
現(xiàn)在我們就可以使用site-maven-plugin了:
<plugin><!-- Deploy the web site --><groupId>com.github.github</groupId><artifactId>site-maven-plugin</artifactId><version>0.12</version><executions><execution><goals><goal>site</goal></goals><!-- select the Maven phase in which the plugin will be executed --><phase>deploy</phase><configuration><!-- Plugin configuration goes here --><server>github</server><!-- The commit message --><message>init git maven repository</message><!-- The location where the site is uploaded --><repositoryName>github-maven-repository</repositoryName> <!-- github repo name --><repositoryOwner>flydean</repositoryOwner> <!-- organization or user name --><!-- Use merge or override the content --><merge>true</merge><outputDirectory>${project.build.directory}/mvn-repo</outputDirectory><branch>refs/heads/mvn-repo</branch> <!-- <includes>--> <!-- <include>**/*</include>--> <!-- </includes>--></configuration></execution></executions></plugin>使用中要注意下面幾點:
site-maven-plugin的goals是site,它需要跟maven的deploy phase相關(guān)聯(lián),從而在我們執(zhí)行mvn deploy的時候自動運行site-maven-plugin。
github的權(quán)限配置,我們可以在configuration中設(shè)置server=github,也可以配置下面的全局變量:
需要指定repositoryName和repositoryOwner,否則會報錯。
message表示的是提交到github的消息。
默認(rèn)情況下的提交到github中的branch是refs/heads/gh-pages,這里我們自定義了一個。
好了,一切都配置完了,我們可以運行了mvn deploy:
從上圖可以看到,github上面已經(jīng)有了一個可共享的項目了。
怎么使用這個共享的項目
使用起來很簡單,只需要在pom.xml文件中添加相應(yīng)的依賴和repository即可:
<dependency><groupId>YOUR.PROJECT.GROUPID</groupId><artifactId>ARTIFACT-ID</artifactId><version>VERSION</version> </dependency><repository><id>ARTIFACT-ID</id><url>https://raw.github.com/flydean/github-maven-repository/mvn-repo/</url> </repository>總結(jié)
Github帶給我們的福利,趕緊用起來吧。
本文的例子https://github.com/ddean2009/
learn-java-base-9-to-20
本文作者:flydean程序那些事
本文鏈接:http://www.flydean.com/apache-maven-git-repository/
本文來源:flydean的博客
歡迎關(guān)注我的公眾號:程序那些事,更多精彩等著您!
總結(jié)
以上是生活随笔為你收集整理的使用site-maven-plugin在github上搭建公有仓库的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java中的类型推断和lambda表达式
- 下一篇: 12个月大厂主机免费领AWS Azure