使用Gitee或GitHub托管Maven仓库JAR包的便捷方法

打印 上一主题 下一主题

主题 584|帖子 584|积分 1752

原文地址:https://ntopic.cn/p/2023062201/
我开源的JAR包的Gitee和GitHub托管的Maven仓库:
背景说明

在上一篇博客中,我们介绍了开源通用高性能分布式id序列组件https://ntopic.cn/p/2023062101/)的设计思路,并把源代码托管在了Gitee(https://gitee.com/obullxl/sequence-jdbc)和GitHub(https://github.com/obullxl/sequence-jdbc)。
我们希望能让更多人便捷的使用本组件,那么把JAR包放到到Maven官方的中心仓库(https://mvnrepository.com)当然是最好的选择。
然而要把JAR包上传到Maven官方中心仓库,步骤比较繁琐,包括注册、申请、发布配置等一系列操作。其实我们的本意只是想把自己的开源项目打包让大家方便使用,能否有更快捷的方式呢?当然是有的,我们可以使用Gitee或者GitHub作为Maven托管仓库,把我们的组件JAR包存储到托管仓库中。
Gitee/GitHub仓库设置

由于Gitee和GitHub原理完全一致,下面截图说明以Gitee为主(GitHub是我们的主仓库,Gitee只是同步GitHub仓库,但这不妨碍我们的配置)。
建议在Gitee中单独申请一个仓库,专门用于存放JAR包,比如我的仓库叫maven-repositoryhttps://gitee.com/obullxl/maven-repository
同时,便于后续多个组件的JAR包能共用一个托管仓库,JAR包统一放到仓库的repository目录中:

特别注意:仓库请请设置为开源,否则其他人使用Maven托管仓库可能无法访问,从而无法下载组件JAR包:

打包发布JAR包到仓库

Gitee托管仓库设置好之后,开始设置我们打包并发布JAR包了。为便于后面设置打包命令,我们把托管Maven仓库的目录maven-repository和id序列组件仓库的目录sequence-jdbc放在同一个父目录中:
  1. OXL-MacBook:CodeSpace obullxl$ ll
  2. drwxr-xr-x   7 obullxl  staff    224  6 24 10:30 maven-repository
  3. drwxr-xr-x  13 obullxl  staff    416  6 24 17:42 sequence-jdbc
复制代码
组件pom.xml打包配置

完整的配置可直接参考分布式id序列的设置:https://gitee.com/obullxl/sequence-jdbc/blob/master/pom.xml

  • pom.xml文件,一定需要定义groupId/artifactId/version这Maven依赖坐标三要素:
  1. <groupId>cn.ntopic</groupId>
  2. <artifactId>sequence-jdbc</artifactId>
  3. <version>1.0.2</version>
  4. <packaging>jar</packaging>
复制代码

  • pom.xml文件,配置build节点,指定JAR打包、Deploy发布的配置(发布到Maven仓库的目录:../maven-repository/repository),即以下配置的altDeploymentRepository内容:
  1. <build>
  2.     <plugins>
  3.         <plugin>
  4.             <groupId>org.apache.maven.plugins</groupId>
  5.             <artifactId>maven-compiler-plugin</artifactId>
  6.             <version>2.3.2</version>
  7.             <configuration>
  8.                 <source>1.8</source>
  9.                 <target>1.8</target>
  10.                 <encoding>UTF-8</encoding>
  11.             </configuration>
  12.         </plugin>
  13.         <plugin>
  14.             <groupId>org.apache.maven.plugins</groupId>
  15.             <artifactId>maven-jar-plugin</artifactId>
  16.             <version>2.5</version>
  17.         </plugin>
  18.         <plugin>
  19.             <groupId>org.apache.maven.plugins</groupId>
  20.             <artifactId>maven-source-plugin</artifactId>
  21.             <version>2.1.2</version>
  22.             <executions>
  23.                 <execution>
  24.                     <phase>package</phase>
  25.                     <goals>
  26.                         <goal>jar</goal>
  27.                     </goals>
  28.                 </execution>
  29.             </executions>
  30.         </plugin>
  31.         <plugin>
  32.             <groupId>org.codehaus.mojo</groupId>
  33.             <artifactId>build-helper-maven-plugin</artifactId>
  34.             <version>1.9.1</version>
  35.             <executions>
  36.                 <execution>
  37.                     <id>timestamp-property</id>
  38.                     <goals>
  39.                         <goal>timestamp-property</goal>
  40.                     </goals>
  41.                 </execution>
  42.             </executions>
  43.             <configuration>
  44.                 <name>BuildTime</name>
  45.                 <pattern>yyyy-MM-dd HH:mm:ss.SSS</pattern>
  46.                 <timeZone>GMT+8</timeZone>
  47.                 <regex/>
  48.                 <source/>
  49.                 <value/>
  50.             </configuration>
  51.         </plugin>
  52.         <plugin>
  53.             <groupId>org.apache.maven.plugins</groupId>
  54.             <artifactId>maven-antrun-plugin</artifactId>
  55.             <version>1.3</version>
  56.             <executions>
  57.                 <execution>
  58.                     <id>generate-release</id>
  59.                     <phase>compile</phase>
  60.                     <goals>
  61.                         <goal>run</goal>
  62.                     </goals>
  63.                     <configuration>
  64.                         <tasks>
  65.                            
  66.                             <echo file="${project.basedir}/target/classes/NTopic.Release" message="Version=${project.version}${line.separator}BuildTime=${BuildTime}" />
  67.                         </tasks>
  68.                     </configuration>
  69.                 </execution>
  70.             </executions>
  71.         </plugin>
  72.         <plugin>
  73.             <groupId>org.apache.maven.plugins</groupId>
  74.             <artifactId>maven-deploy-plugin</artifactId>
  75.             <version>2.7</version>
  76.             <configuration>
  77.                
  78.                 <altDeploymentRepository>internal.repo::default::file://${project.basedir}/../maven-repository/repository</altDeploymentRepository>
  79.             </configuration>
  80.         </plugin>
  81.     </plugins>
  82. </build>
复制代码
打包并上传到仓库


  • 打包并发布到本地目录命令:
  1. mvn clean
  2. mvn deploy -Dmaven.test.skip=true
复制代码

  • 上传到远程仓库命令:
  1. cd ./../maven-repository
  2. git add --all
  3. git commit -m 'Deploy sequence-jdbc JAR: https://github.com/obullxl/sequence-jdbc'
  4. git push origin master
复制代码
完整的打包命令,请参考分布式id序列源仓库代码:https://gitee.com/obullxl/sequence-jdbc/blob/master/deploy.sh
  1. #!/bin/bash# 本地打包mvn clean && mvn deploy -Dmaven.test.skip=true# 上传仓库cd ./../maven-repository
  2. git add --all
  3. git commit -m 'Deploy sequence-jdbc JAR: https://github.com/obullxl/sequence-jdbc'
  4. git push origin master# 返回项目cd ../sequence-jdbc# Gitee刷新:人工刷新仓库,从GitHub同步过来open -a '/Applications/Microsoft Edge.app' https://gitee.com/obullxl/maven-repository
复制代码
多个版本完整的Maven托管仓库内容:

其他项目使用JAR包方法

和Maven官方的中心仓库相比,Gitee托管仓库没有本质区别,只需要在pom.xml中配置Gitee的托管仓库即可,让Maven知道从哪儿去下载JAR包。
pom.xml中增加仓库

pom.xml中增加Gitee托管仓库地址:
  1. <repositories>
  2.   <repository>
  3.     <id>Gitee-obullxl</id>
  4.     <url>https://gitee.com/obullxl/maven-repository/raw/master/repository</url>
  5.   </repository>
  6. </repositories>
复制代码
或者增加GitHub托管仓库地址:
  1. <repositories>
  2.    <repository>
  3.       <id>GitHub-obullxl</id>
  4.       <url>https://raw.githubusercontent.com/obullxl/maven-repository/master/repository</url>
  5.    </repository>
  6. </repositories>
复制代码
Maven配置依赖

和其他JAR包一样,pom.xml中增加依赖坐标:
  1. <dependency>
  2.     <groupId>cn.ntopic</groupId>
  3.     <artifactId>sequence-jdbc</artifactId>
  4.     <version>1.0.2</version>
  5. </dependency>
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

正序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

风雨同行

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表