[toc]java
使用site-maven-plugin在github上搭建公有倉庫git
Maven是咱們在開發java程序中常常使用的構建工具,在團隊合做開發過程當中,若是咱們想要將本身寫好的jar包共享給別人使用,一般須要本身搭建maven倉庫,而後將寫好的jar包上傳到maven倉庫中,以供其餘用戶使用。程序員
搭建maven倉庫須要服務器和域名,對公司而言域名和服務器多的是,可是若是是咱們我的或者小團隊想共享一些很是有用的jar包給別人使用就太麻煩了。github
最近Github好消息頻出,先是對我的用戶取消了repositories和協做用戶的個數限制,後面對於企業用戶也進行了升級和降價處理。若是倉庫不大的話,徹底能夠把倉庫搬到github上面去。web
更多精彩內容且看:apache
更多內容請訪問 www.flydean.com
要在github上面搭建maven倉庫,咱們須要使用到maven的插件:site-maven-plugin。由於要連到github上面,因此須要設置github的oauth權限。直接用用戶名密碼也能夠,可是這樣作不安全,咱們並不推薦。安全
如上圖所示,在Settings->Developer settings->Personal access tokens中建立一個access tokens,所需權限以下:服務器
注意,用戶這裏的權限必定要選,不然後面會報異常。
有了權限,接下來咱們再建立一個github-maven-repository,用來做爲mvn倉庫存儲數據。maven
假如生成的地址是:https://github.com/flydean/gi...ide
這一步咱們須要編輯setting.xml文件,通常來講這個文件是在~/.m2/settings.xml。
咱們須要添加一個Server,若是直接使用github的用戶名密碼,則像下面這樣:
<server> <id>github</id> <username>YOUR_USERNAME</username> <password>YOUR_PASSWORD</password> </server>
前面咱們講到了直接使用用戶名是不安全的,咱們可使用上面建立的oauth key:
<server> <id>github</id> <password>OAUTH2TOKEN</password> </server>
這個id會在後面的pom.xml文件配置中用到,這裏咱們先記下來。
咱們的目標是生成包含jar包的maven依賴。在將jar包上傳到遠程倉庫以前,咱們須要在本地先生成。
先配置一個本地的repository:
<distributionManagement> <repository> <id>maven.repo</id> <name>Local Staging Repository</name> <url>file://${project.build.directory}/mvn-repo</url> </repository> </distributionManagement>
上面咱們指定了在項目的build目錄下面建立了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了:
<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>
使用中要注意下面幾點:
<properties> <github.global.server>github</github.global.server> </properties>
好了,一切都配置完了,咱們能夠運行了mvn deploy:
從上圖能夠看到,github上面已經有了一個可共享的項目了。
使用起來很簡單,只須要在pom.xml文件中添加相應的依賴和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>
Github帶給咱們的福利,趕忙用起來吧。
本文的例子[https://github.com/ddean2009/
learn-java-base-9-to-20](https://github.com/ddean2009/...
本文做者:flydean程序那些事本文連接:http://www.flydean.com/apache-maven-git-repository/
本文來源:flydean的博客
歡迎關注個人公衆號:程序那些事,更多精彩等着您!