使用site-maven-plugin在github上搭建公有倉庫

簡介

Maven是我們在開發java程序中經常使用的構建工具,在團隊合作開發過程中,如果我們想要將自己寫好的jar包共享給別人使用,通常需要自己搭建maven倉庫,然後將寫好的jar包上傳到maven倉庫中,以供其他用戶使用。

搭建maven倉庫需要服務器和域名,對公司而言域名和服務器多的是,但是如果是我們個人或者小團隊想共享一些非常有用的jar包給別人使用就太麻煩了。

最近Github好消息頻出,先是對個人用戶取消了repositories和協作用戶的個數限制,後面對於企業用戶也進行了升級和降價處理。如果倉庫不大的話,完全可以把倉庫搬到github上面去。

更多精彩內容且看:

更多內容請訪問www.flydean.com

前期準備

要在github上面搭建maven倉庫,我們需要使用到maven的插件:site-maven-plugin。因爲要連到github上面,所以需要設置github的oauth權限。直接用用戶名密碼也可以,但是這樣做不安全,我們並不推薦。

如上圖所示,在Settings->Developer settings->Personal access tokens中創建一個access tokens,所需權限如下:

注意,用戶這裏的權限一定要選,否則後面會報異常。

有了權限,接下來我們再創建一個github-maven-repository,用來作爲mvn倉庫存儲數據。

假如生成的地址是:https://github.com/flydean/github-maven-repository

在maven中配置GitHub權限

這一步我們需要編輯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文件配置中用到,這裏我們先記下來。

配置deploy-plugin

我們的目標是生成包含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

現在我們就可以使用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>

使用中要注意下面幾點:

  1. site-maven-plugin的goals是site,它需要跟maven的deploy phase相關聯,從而在我們執行mvn deploy的時候自動運行site-maven-plugin。

  2. github的權限配置,我們可以在configuration中設置server=github,也可以配置下面的全局變量:

    <properties>
        <github.global.server>github</github.global.server>
    </properties>
  1. 需要指定repositoryName和repositoryOwner,否則會報錯。

  2. message表示的是提交到github的消息。

  3. 默認情況下的提交到github中的branch是refs/heads/gh-pages,這裏我們自定義了一個。

好了,一切都配置完了,我們可以運行了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

本文作者:flydean程序那些事

本文鏈接:http://www.flydean.com/apache-maven-git-repository/

本文來源:flydean的博客

歡迎關注我的公衆號:程序那些事,更多精彩等着您!

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