准备好Maven项目下的类
package com.mouday; public class Hello { public void sayHello(){ System.out.println("Hello"); } }
1、创建两个GITHUB_TOKEN
一个用于发布,一个用于使用
https://github.com/settings/tokens
用户设置 Settings -> Developer settings -> Personal access tokens -> New personal access token
2、设置账号密码
~/.m2/settings.xml
<servers> <server> <id>github</id> <username>mouday</username> <password>GITHUB_TOKEN</password> </server> </servers>
3、设置仓库地址
pom.xml
<distributionManagement> <repository> <id>github</id> <name>GitHub OWNER Apache Maven Packages</name> <url>https://maven.pkg.github.com/mouday/hello-package</url> </repository> </distributionManagement>
4、发布到Github私有仓库
mvn clean package deploy
查看
https://github.com/mouday/hello-package/packages/
5、下载使用
pom.xml
<project> <repositories> <!--仓库地址--> <repository> <id>github</id> <name>GitHub OWNER Apache Maven Packages</name> <url>https://maven.pkg.github.com/mouday/hello-package</url> </repository> </repositories> <dependencies> <!--引入依赖 --> <dependency> <groupId>com.mouday</groupId> <artifactId>hello-package</artifactId> <version>1.0</version> </dependency> </dependencies> </project>
引入Hello类使用
package com.mouday; public class demo { public static void main(String[] args) { Hello hello = new Hello(); hello.sayHello(); } }
参考