GitHub上建立自己的Maven倉庫並引用
1、首先在Github倉庫中建立一個自己的倉庫,倉庫名稱為:mvn-repo,如下圖所示:
2、然後在mvn工具的配置檔案settings.xml中(在window中配置檔案會在Maven的安裝目錄下的conf資料夾下),找到servers標籤,新增一個server,如:
<server> <id>github</id> <username>guihub登入的使用者名稱</username> <password>guihub登入的使用者密碼</password> </server>
如下圖所示:
3、在maven專案的pom.xml中新增入下程式碼,將本地的jar釋出到本地倉庫中。
<build> <plugins> <plugin> <artifactId>maven-deploy-plugin</artifactId> <version>2.8.1</version> <configuration> <altDeploymentRepository>internal.repo::default::file://${project.build.directory}/mvn-repo</altDeploymentRepository> </configuration> </plugin> </plugins> </build>
如下圖所示:
4、然後輸入: mvn clean deploy命令,如下圖所示bulid成功即可將jar釋出到了本地殘酷中了:
5、將本地倉庫中釋出到遠端的github指定的倉庫中,新增修改外掛,如下程式碼:
<plugin> <groupId>com.github.github</groupId> <artifactId>site-maven-plugin</artifactId> <version >0.12</version> <configuration> <message >Maven artifacts for ${project.version}</message> <noJekyll>true</noJekyll> <outputDirectory>${project.build.directory}/mvn-repo</outputDirectory><!--本地jar地址--> <branch>refs/heads/master</branch><!--分支的名稱--> <merge>true</merge> <includes> <include>**/*</include> </includes> <repositoryName>mvn-repo</repositoryName><!--對應github上建立的倉庫名稱 name--> <repositoryOwner>sxjlinux</repositoryOwner><!--github 倉庫所有者即登入使用者名稱--> </configuration> <executions> <execution> <goals> <goal>site</goal> </goals> <phase>deploy</phase> </execution> </executions> </plugin>
注意:branch必須是refs/heads/開頭的,後邊跟分支名稱,我們在網頁上檢視到mvn-repo下有一個master的分支,如下圖所示:
6、然後加入如下程式碼,配置遠端的github服務:
<properties>
<github.global.server>github</github.global.server>
</properties>
如下圖所示:
7、整個配置pom.xml檔案程式碼如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>pubmodel</artifactId>
<groupId>com.wincom.publicmodel</groupId>
<version>2.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<version>2.0</version>
<artifactId>path</artifactId>
<build>
<plugins>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.1</version>
<configuration>
<altDeploymentRepository>internal.repo::default::file://${project.build.directory}/mvn-repo</altDeploymentRepository>
</configuration>
</plugin>
<plugin>
<groupId>com.github.github</groupId>
<artifactId>site-maven-plugin</artifactId>
<version >0.12</version>
<configuration>
<message >Maven artifacts for ${project.version}</message>
<noJekyll>true</noJekyll>
<outputDirectory>${project.build.directory}/mvn-repo</outputDirectory><!--本地jar地址-->
<branch>refs/heads/master</branch><!--分支的名稱-->
<merge>true</merge>
<includes>
<include>**/*</include>
</includes>
<repositoryName>mvn-repo</repositoryName><!--對應github上建立的倉庫名稱 name-->
<repositoryOwner>sxjlinux</repositoryOwner><!--github 倉庫所有者即登入使用者名稱-->
</configuration>
<executions>
<execution>
<goals>
<goal>site</goal>
</goals>
<phase>deploy</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>
<properties>
<github.global.server>github</github.global.server>
</properties>
</project>
8、然後輸入: mvn clean deploy命令會提示錯誤,此錯誤主要是因為在github中沒有設定自己的姓名
[INFO]
[INFO] --- site-maven-plugin:0.12:site (default) @ path ---
[INFO] Creating 9 blobs
[INFO] Creating tree with 10 blob entries
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 38.520 s
[INFO] Finished at: 2018-12-28T20:53:57+08:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal com.github.github:site-maven-plugin:0.12:site (default) on project path: Error creating commit: Invalid request.
[ERROR]
[ERROR] For 'properties/name', nil is not a string.
[ERROR] For 'properties/name', nil is not a string. (422)
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
如下圖所示:
9、登入到github中,然後點選Settings,如下圖所示:
10、在下圖中輸入自己的姓名
11、然後點選update,如下圖所示:
12、在次使用mvn clean deploy命令,此時就會上傳成功,如下圖所示:
13、然後重新整理github網頁檢視,如下圖所示:
14、以上都是在子模組中進行的,如果有多個子模組,可以將上邊用到的build統一放在專案的根目錄中的pom.xml中,而子模組不需要放置build,直接在根目錄執行mvn clean deploy命令,mvn會自動將所有子模組打包上傳到github中的。
15、當上傳成功後,需要在專案中使用釋出到github上的jar包,只需要在專案中的pom.xml中新增github倉庫,如下程式碼表示
<repositories>
<repository>
<id>mvn-repo</id>
<url>https://raw.github.com/sxjlinux/mvn-repo/master</url>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
</snapshots>
</repository>
</repositories>
如下圖所示:
注意:最需要注意的是上傳的分支路徑,下面的截圖我是將分支更改為了master,所以使用的地址是https://raw.github.com/sxjlinux/mvn-repo/master/,如果在mvn-repo有一個test的分支,則網址則寫為:https://raw.github.com/sxjlinux/mvn-repo/test/即可。
16、新建一個工程,然後在沒加入依賴以前,程式碼中是顯示沒有改包的,如下圖所示:
17、在pom.xml加入依賴,如下程式碼所示:
<repositories>
<repository>
<id>mvn-repo</id>
<url>https://raw.github.com/sxjlinux/mvn-repo/master</url>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
</snapshots>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.wincom.publicmodel</groupId>
<artifactId>path</artifactId>
<version>2.0</version>
</dependency>
</dependencies>
如下圖所示:
此處的groupId、artifactId、version是上傳git倉庫專案中pom.xml中的id和版本號,如:
18、此時程式碼中即可引用成功,並有提示,如下圖所示:
19、此處執行會打印出當前路徑,如下圖所示:
20、說明可以成功被引用。
具體的程式碼請看:https://github.com/sxjlinux/mvn-repo-src
以下是我多次上傳中採的坑出現的錯誤:
1、執行mvn clean deploy命令出現錯誤,意思是github的倉庫分支必須是以refs/開頭的,如下程式碼所示:
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 40.043 s
[INFO] Finished at: 2018-12-28T20:56:50+08:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal com.github.github:site-maven-plugin:0.12:site (default) on project path: Error creating reference: Reference name must start with 'refs/'. (422) -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
如下圖所示:
或者:
[INFO]
[INFO] --- site-maven-plugin:0.12:site (default) @ path ---
[INFO] Creating 12 blobs
[INFO] Creating tree with 13 blob entries
[INFO] Creating commit with SHA-1: d0eca79926ec6a3a77754fe44936455cfe885c7e
[INFO] Creating reference mvn-repo starting at commit d0eca79926ec6a3a77754fe44936455cfe885c7e
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 50.346 s
[INFO] Finished at: 2018-12-29T22:13:05+08:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal com.github.github:site-maven-plugin:0.12:site (default) on project path: Error creating reference: mvn-repo is not a valid ref name. (422) -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
如下圖所示:
2、只需要在branch標籤的mvn-repo的前邊加上refs/即可,如下圖所示:
3、再次執行mvn clean deploy命令即可編譯成功並上傳,如下圖所示:
4、此時發現倉庫中是空的,如下圖所示:
5、此時還是需要更改配置路徑將refs/mvn-repo更改為refs/heads/mvn-repo,如下圖所示:
6、再次執行mvn clean deploy命令即可編譯成功並上傳,如下圖所示:
7、然後登陸到github即可檢視上傳的jar專案,如下圖所示: