我們開源了組件的源代碼,希望更多人能更便捷的使用開源組件,最好的辦法當然是把組件的JAR包上傳到Maven中央倉庫,這樣可直接通過Maven/Gradle等方式快速引用和使用。但是要把JAR包上傳到Maven中央倉庫的門檻比較高,本文介紹一種非常簡單的使用GitHub/Gitee作為Maven倉庫的... ...
原文地址:https://ntopic.cn/p/2023062201/
我開源的JAR包的Gitee和GitHub托管的Maven倉庫:
- Gitee托管倉庫:https://gitee.com/obullxl/maven-repository
- GitHub托管倉庫:https://github.com/obullxl/maven-repository
背景說明
在上一篇博客中,我們介紹了開源通用高性能分散式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-repository:https://gitee.com/obullxl/maven-repository
同時,便於後續多個組件的JAR包能共用一個托管倉庫,JAR包統一放到倉庫的repository
目錄中:
特別註意:
倉庫請請設置為開源,否則其他人使用Maven托管倉庫可能無法訪問,從而無法下載組件JAR包:
打包發佈JAR包到倉庫
Gitee托管倉庫設置好之後,開始設置我們打包併發布JAR包了。為便於後面設置打包命令,我們把托管Maven倉庫的目錄maven-repository
和id序列組件倉庫的目錄sequence-jdbc
放在同一個父目錄中:
OXL-MacBook:CodeSpace obullxl$ ll
drwxr-xr-x 7 obullxl staff 224 6 24 10:30 maven-repository
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依賴坐標三要素:
<groupId>cn.ntopic</groupId>
<artifactId>sequence-jdbc</artifactId>
<version>1.0.2</version>
<packaging>jar</packaging>
- pom.xml文件,配置build節點,指定JAR打包、Deploy發佈的配置(發佈到Maven倉庫的目錄:
../maven-repository/repository
),即以下配置的altDeploymentRepository內容:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.5</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.1.2</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.9.1</version>
<executions>
<execution>
<id>timestamp-property</id>
<goals>
<goal>timestamp-property</goal>
</goals>
</execution>
</executions>
<configuration>
<name>BuildTime</name>
<pattern>yyyy-MM-dd HH:mm:ss.SSS</pattern>
<timeZone>GMT+8</timeZone>
<regex/>
<source/>
<value/>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.3</version>
<executions>
<execution>
<id>generate-release</id>
<phase>compile</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<!--suppress UnresolvedMavenProperty -->
<echo file="${project.basedir}/target/classes/NTopic.Release" message="Version=${project.version}${line.separator}BuildTime=${BuildTime}" />
</tasks>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.7</version>
<configuration>
<!-- 特別註意的地方:指定打包的目錄 -->
<altDeploymentRepository>internal.repo::default::file://${project.basedir}/../maven-repository/repository</altDeploymentRepository>
</configuration>
</plugin>
</plugins>
</build>
打包並上傳到倉庫
- 打包併發布到本地目錄命令:
mvn clean
mvn deploy -Dmaven.test.skip=true
- 上傳到遠程倉庫命令:
cd ./../maven-repository
git add --all
git commit -m 'Deploy sequence-jdbc JAR: https://github.com/obullxl/sequence-jdbc'
git push origin master
完整的打包命令,請參考分散式id序列源倉庫代碼:https://gitee.com/obullxl/sequence-jdbc/blob/master/deploy.sh:
#!/bin/bash
# 本地打包
mvn clean && mvn deploy -Dmaven.test.skip=true
# 上傳倉庫
cd ./../maven-repository
git add --all
git commit -m 'Deploy sequence-jdbc JAR: https://github.com/obullxl/sequence-jdbc'
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托管倉庫地址:
<repositories>
<repository>
<id>Gitee-obullxl</id>
<url>https://gitee.com/obullxl/maven-repository/raw/master/repository</url>
</repository>
</repositories>
或者增加GitHub托管倉庫地址:
<repositories>
<repository>
<id>GitHub-obullxl</id>
<url>https://raw.githubusercontent.com/obullxl/maven-repository/master/repository</url>
</repository>
</repositories>
Maven配置依賴
和其他JAR包一樣,pom.xml
中增加依賴坐標:
<dependency>
<groupId>cn.ntopic</groupId>
<artifactId>sequence-jdbc</artifactId>
<version>1.0.2</version>
</dependency>
本文作者:奔跑的蝸牛,轉載請註明原文鏈接:https://ntopic.cn