把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),然后勾选repouser,最后点击 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包仓库地址

遇到的问题

  • Error creating blob: Git Repository is empty. (409)

解决方案: 在仓库里随便创建一个文件不要为空就行。

参考

posted on 2022-04-23 15:52  愤怒的苹果ext  阅读(576)  评论(0编辑  收藏  举报

导航