【Github上創建倉庫】
首先,在GitHub上創建自己的倉庫(mvn-repo):
【配置本地setting文件】
找到本地的maven settings文件,配置server:
有兩種選擇,可以選擇配置username和password,或者選擇配置Personal access tokens<OAUTH2TOKEN>(也是填充到password字段)。
優先使用Personal access tokens,因為有些公司內部可能會對用戶名和密碼做限制(我也不知道為什么)。
前者即是GitHub的用戶名以及密碼,后者需要在GitHub上進行申請,步驟如下:
選擇對應的權限,並標注Note:
然后點擊查看:
上圖紅框即是你的personal access token。
注:此token會不斷發生變化,每一次查看都會更新,更新后之前的不可用,所以要妥善保存。
【增加本地臨時存儲庫】
在pom文件中增加
<build>
<plugins>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
<configuration>
<!-- altDeploymentRepository :指定替代方案應該部署項目工件的存儲庫(除了指定的工件)。 -->
<altDeploymentRepository>internal.repo::default::file://${project.build.directory}/mvn-repo </altDeploymentRepository>
</configuration>
</plugin>
</plugins>
</build>
然后執行mvn clean deploy。
如下圖,版本已經正確地發布到本地指定的存儲庫中了。
【配置遠程的github服務】
在pom文件中增加以下幾行:
<properties>
<github.global.server>github</github.global.server>
</properties>
【發布到遠程的github指定的倉庫】
在pom文件中配置以下幾行:
<!--源碼-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
<!--github上傳插件,用於修改后的發布,執行 mvn clean deploy 自動打包上傳到github-->
<plugin>
<groupId>com.github.github</groupId>
<artifactId>site-maven-plugin</artifactId>
<version>0.12</version>
<configuration>
<message>Creating site for ${project.artifactId} ${project.version}</message>
<noJekyll>true</noJekyll>
<!--本地jar地址, 對應上面的altDeploymentRepository-->
<outputDirectory>${project.build.directory}/mvn-repo</outputDirectory>
<!--分支-->
<branch>refs/heads/master</branch>
<merge>true</merge>
<includes>
<include>**/*</include>
</includes>
<!--對應github上創建的倉庫名稱 name-->
<repositoryName>mvn-repo</repositoryName>
<!--github登錄賬號 對應的密碼存在maven的setting.xml文件中-->
<!--由github組織擁有,則該值將是組織名稱,如果由用戶擁有,則該值將是用戶名-->
<repositoryOwner>liufarui</repositoryOwner>
</configuration>
<executions>
<execution>
<goals>
<goal>site</goal>
</goals>
<phase>deploy</phase>
</execution>
</executions>
</plugin>
再次執行mvn clean deploy命令即可發布到GitHub的maven倉庫中。
如上圖即為成功;
我們可以查看我們的mvn-repo項目,發現內容已經發生了變化:
【使用依賴包】
在新項目的pom文件中增加以下行:
<repositories>
<repository>
<id>mvn-repo</id>
<!-- https://raw.github.com/用戶名/倉庫名/分支名 -->
<url>https://raw.github.com/liufarui/mvn-repo/master</url>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
</snapshots>
</repository>
</repositories>
在新項目的pom文件中增加依賴:
<dependencies>
<dependency>
<groupId>com.github.liufarui</groupId>
<artifactId>demo-maven-github-repo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
由於我們之前開發的這個是個maven插件工程,所以增加以下行進行使用:
<build>
<plugins>
<plugin>
<groupId>com.github.liufarui</groupId>
<artifactId>demo-maven-github-repo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</plugin>
</plugins>
</build>
我們現在可以在右側Maven中看到我們的插件
執行看結果:
【demo地址】
以上,即是整個Github創建maven倉庫的內容,為了方便大家查看學習,我把demo項目放到了我的github上,大家可以自行查看,有問題也可以在評論區隨時討論:
https://github.com/liufarui/code-demo
【問題】
[ERROR] Failed to execute goal com.github.github:site-maven-plugin:0.12:site (default) on project path: Error creating commit: Invalid request. |
遇到以上問題是因為沒有設置姓名,在setting中進行設置:
Error creating blob: cannot retry due to server authentication, in streaming mode |
很多人都遇到了此問題,此問題一般是權限問題,有可能是用戶名密碼不對,也有可能是公司網絡做了特殊的限制,還有一些奇怪的原因,一般可以通過配置Personal access tokens去解決,附上網上的討論鏈接:
https://github.com/github/maven-plugins/issues/36
以上,是如何搭建私有的maven倉庫,這個倉庫必須是有個人賬戶認證才可以使用的,無法確保大家可以一起用,之后,會再寫一篇如何搭建public倉庫的博客。