本文最后更新于731 天前,其中的信息可能已经过时,如有错误请发送邮件到mapleleaf2333@gmail.com
近期由于项目需要,得在Maven项目中引入GitHub上的工具包,但是该包如果直接在Maven中添加,会出现401报错,说是没有认证权限。
了解到需要在GitHub上创建个人的访问远程仓库的Token,然后利用Token完成认证,官方步骤可以参考:
Working with the Apache Maven registry – GitHub Docs
下面简单说一下具体的步骤:
创建个人Token
首先进入GitHub,点击Setting
在左侧栏找到开发者设置
选择经典Token,并新建一个token
创建时勾选如下几项,最重要的是框框内的选项,如果用不了,可以直接按照下图的内容勾起。勾起后点击创建,注意刚创建时一定要把提供的token复制下来,因为之后这个token便不可视了,除非重新生成。
配置maven
接下来需要在本地电脑的用户路径下(我的电脑是在C:\Users\你的用户名\.m2),编辑settings.xml文件,如果没有则新建一个即可,配置内容如下:
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
http://maven.apache.org/xsd/settings-1.0.0.xsd">
<activeProfiles>
<activeProfile>github</activeProfile>
</activeProfiles>
<profiles>
<profile>
<id>github</id>
<repositories>
<repository>
<id>central</id>
<url>https://repo1.maven.org/maven2</url>
</repository>
<repository>
<id>github</id>
<url>https://maven.pkg.github.com/OWNER/REPOSITORY</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
</profile>
</profiles>
<servers>
<server>
<id>github</id>
<username>USERNAME</username>
<password>TOKEN</password>
</server>
</servers>
</settings>
其中repository标签中,将你需要引入的工具包的仓库地址填进去,然后在下面的server中填入自己GitHub账号的用户名和Token,然后保存即可。(如何发布包到GitHub上可以参考上面的官方文档,这里因为我用的是别人发布的包,所以也没试过)
然后直接在maven的pom.xml中添加依赖即可,先添加仓库如下所示:
然后在dependencies中添加依赖即可:(这里由于我直接用的项目代码,没有自己试过引入其他的项目,所以有不清楚的建议找其他资料)
<dependencies>
<dependency>
<groupId>{自己用的组ID}</groupId>
<artifactId>{引入的包ID}</artifactId>
<version>{引入的版本}/version>
</dependency>
</dependencies>