把Jar包上传到GitHub仓库
前言
- 想要把自己的Jar作为公共依赖,要使用的时候引入就可以了。但是上传到中央仓库还要先完成审核。自己又没有私服。这个时候我选择上传到GitHub仓库,先满足我基本的需求。
事先创建保存Jar包的仓库
- 事先创建好保存Jar包的仓库 ,如
maven-repository
设置settings.xml文件
- 定位到
<servers>
标签下,增加如下代码。
<server>
<id>github</id>
<username>GitHub帐号名</username>
<password>GitHub token</password>
</server>
-
token
在此处申请https://github.com/settings/tokens
-
点击
Generate new token
,输入密码后进入。输入名称(Note),失效时间(Expiration),然后勾选repo
和user
,最后点击 Generate token。 -
然后就能得到
token
了, 注意保存下来,这个只显示一次。
修改项目的pom.xml
- 增加如下代码
下面代码中
1030907690
是我的帐户名,maven-repository
是我事先在GitHub创建好的仓库。
<properties>
<java.version>1.8</java.version>
<github.global.server>github</github.global.server>
</properties>
<build>
<plugins>
<!--该插件识别java代码 否则自己在Project Structure设置-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!-- 要将源码放上去,需要加入这个插件 -->
<plugin>
<artifactId>maven-source-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<attach>true</attach>
</configuration>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<!--参考 https://www.jianshu.com/p/98a141701cc7-->
<!--Error creating blob: Git Repository is empty. (409) 仓库里随便创建下文件-->
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.1</version>
<configuration>
<altDeploymentRepository>
internal.repo::default::file://${project.build.directory}/maven-repository
</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}/maven-repository</outputDirectory><!--本地jar地址-->
<branch>refs/heads/main</branch><!--分支-->
<merge>true</merge>
<includes>
<include>**/*</include>
</includes>
<repositoryName>maven-repository</repositoryName><!--对应github上创建的仓库名称 name-->
<repositoryOwner>1030907690</repositoryOwner><!--github 仓库所有者-->
</configuration>
<executions>
<execution>
<goals>
<goal>site</goal>
</goals>
<phase>deploy</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>
上传Jar
- 使用如下命令
mvn clean package deploy
- 如果要忽略测试
mvn clean package deploy -DskipTests
执行效果
- 上传完成后仓库里就有Jar包了,如下图所示。
其他项目引入Jar
注意下
settings.xml
有没有配置<mirrorOf>*</mirrorOf>
,如果有不会走自己的仓库,是拉不下来这种包的。
<repositories>
<repository>
<id>maven-repository-main</id>
<url>https://raw.github.com/帐户名/maven-repository/main/</url>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
</snapshots>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>groupId</groupId>
<artifactId>artifactId</artifactId>
<version>版本号</version>
</dependency>
</dependencies>
源码和Jar包仓库地址
- 源码地址:https://github.com/1030907690/spring-boot-kubernetes/tree/v1.0.1
- Jar包地址:https://github.com/1030907690/maven-repository
遇到的问题
- Error creating blob: Git Repository is empty. (409)
解决方案: 在仓库里随便创建一个文件不要为空就行。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战