解决方案:如何将自定义工具项目创建为可依赖项
将Spring Boot + Maven项目部署到GitHub包注册表并使用依赖
把项目上传到GitHub官方包注册表后,即可在其他项目中便捷引用其依赖。以下是针对Spring Boot + Maven项目的完整操作流程:
部署步骤
配置pom.xml
向项目的pom.xml中添加以下配置,指定GitHub包注册表的发布地址:<distributionManagement> <repository> <id>github</id> <url>https://maven.pkg.github.com/{username}/{repository-name}</url> </repository> </distributionManagement>注意:将
{username}替换为你的GitHub用户名,{repository-name}替换为目标仓库名称。配置身份验证
创建个人访问令牌后,在本地.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/{username}/{repository-name}</url> <snapshots> <enabled>true</enabled> </snapshots> </repository> </repositories> </profile> </profiles> <servers> <server> <id>github</id> <username>{username}</username> <password>{your-personal-token}</password> </server> </servers> </settings>替换配置中的
{username}、{repository-name}和{your-personal-token}为实际信息。上传项目代码
在GitHub创建对应名称的仓库,将本地项目代码推送至该仓库。执行部署命令
在项目根目录下执行以下Maven命令,完成包的发布:mvn clean deploy
引用已部署的依赖
部署完成后,可在GitHub项目的packages页面获取依赖引用示例,将以下仓库配置添加到你的项目pom.xml中,即可像使用其他Maven依赖一样引用该包:
<repositories> <repository> <id>{artifactId}</id> <url>https://maven.pkg.github.com/{username}/{repository-name}</url> </repository> </repositories>
替换占位符为实际项目信息。
重要提示
- 每个版本仅能创建一个依赖实例,若需为同一版本更新依赖,需先删除GitHub上对应的旧依赖。
- 操作过程中若遇到问题或有其他解决方案,欢迎分享交流。
内容的提问来源于stack exchange,提问作者agitrubard
相关产品推荐
相关产品推荐

