向蒋丞选手看齐 發表於 2023-7-1 14:38:00

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

<p>原文地址:https://ntopic.cn/p/2023062201/</p>
<p>我开源的JAR包的Gitee和GitHub托管的Maven仓库:</p>
<ul>
<li>Gitee托管仓库:https://gitee.com/obullxl/maven-repository</li>
<li>GitHub托管仓库:https://github.com/obullxl/maven-repository</li>
</ul>
<h2 id="背景说明">背景说明</h2>
<p>在上一篇博客中,我们介绍了<strong>开源通用高性能分布式id序列组件</strong>(https://ntopic.cn/p/2023062101/)的设计思路,并把源代码托管在了Gitee(https://gitee.com/obullxl/sequence-jdbc)和GitHub(https://github.com/obullxl/sequence-jdbc)。</p>
<p>我们希望能让更多人便捷的使用本组件,那么把JAR包放到到Maven官方的中心仓库(https://mvnrepository.com)当然是最好的选择。</p>
<p>然而要把JAR包上传到Maven官方中心仓库,步骤比较繁琐,包括注册、申请、发布配置等一系列操作。其实我们的本意只是想把自己的开源项目打包让大家方便使用,能否有更快捷的方式呢?当然是有的,我们可以使用Gitee或者GitHub作为Maven托管仓库,把我们的组件JAR包存储到托管仓库中。</p>
<h2 id="giteegithub仓库设置">Gitee/GitHub仓库设置</h2>
<p>由于Gitee和GitHub原理完全一致,下面截图说明以Gitee为主(GitHub是我们的主仓库,Gitee只是同步GitHub仓库,但这不妨碍我们的配置)。</p>
<p>建议在Gitee中单独申请一个仓库,专门用于存放JAR包,比如我的仓库叫<strong>maven-repository</strong>:https://gitee.com/obullxl/maven-repository</p>
<p>同时,便于后续多个组件的JAR包能共用一个托管仓库,JAR包统一放到仓库的<code>repository</code>目录中:</p>
<p><img src="https://img2023.cnblogs.com/blog/198133/202307/198133-20230701143056071-867729819.jpg"></p>
<p><code>特别注意:</code>仓库请请设置为<strong>开源</strong>,否则其他人使用Maven托管仓库可能无法访问,从而无法下载组件JAR包:</p>
<p><img src="https://img2023.cnblogs.com/blog/198133/202307/198133-20230701143107044-1418159490.jpg"></p>
<h2 id="打包发布jar包到仓库">打包发布JAR包到仓库</h2>
<p>Gitee托管仓库设置好之后,开始设置我们打包并发布JAR包了。为便于后面设置打包命令,我们把托管Maven仓库的目录<code>maven-repository</code>和id序列组件仓库的目录<code>sequence-jdbc</code>放在同一个父目录中:</p>
<pre><code class="language-shell">OXL-MacBook:CodeSpace obullxl$ ll
drwxr-xr-x   7 obullxlstaff    2246 24 10:30 maven-repository
drwxr-xr-x13 obullxlstaff    4166 24 17:42 sequence-jdbc
</code></pre>
<h3 id="组件pomxml打包配置">组件pom.xml打包配置</h3>
<p>完整的配置可直接参考分布式id序列的设置:https://gitee.com/obullxl/sequence-jdbc/blob/master/pom.xml</p>
<ul>
<li>pom.xml文件,一定需要定义<code>groupId</code>/<code>artifactId</code>/<code>version</code>这Maven依赖坐标三要素:</li>
</ul>
<pre><code class="language-xml">&lt;groupId&gt;cn.ntopic&lt;/groupId&gt;
&lt;artifactId&gt;sequence-jdbc&lt;/artifactId&gt;
&lt;version&gt;1.0.2&lt;/version&gt;
&lt;packaging&gt;jar&lt;/packaging&gt;
</code></pre>
<ul>
<li>pom.xml文件,配置build节点,指定JAR打包、Deploy发布的配置(发布到Maven仓库的目录:<code>../maven-repository/repository</code>),即以下配置的<strong>altDeploymentRepository</strong>内容:</li>
</ul>
<pre><code class="language-xml">&lt;build&gt;
    &lt;plugins&gt;
      &lt;plugin&gt;
            &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
            &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
            &lt;version&gt;2.3.2&lt;/version&gt;
            &lt;configuration&gt;
                &lt;source&gt;1.8&lt;/source&gt;
                &lt;target&gt;1.8&lt;/target&gt;
                &lt;encoding&gt;UTF-8&lt;/encoding&gt;
            &lt;/configuration&gt;
      &lt;/plugin&gt;
      &lt;plugin&gt;
            &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
            &lt;artifactId&gt;maven-jar-plugin&lt;/artifactId&gt;
            &lt;version&gt;2.5&lt;/version&gt;
      &lt;/plugin&gt;
      &lt;plugin&gt;
            &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
            &lt;artifactId&gt;maven-source-plugin&lt;/artifactId&gt;
            &lt;version&gt;2.1.2&lt;/version&gt;
            &lt;executions&gt;
                &lt;execution&gt;
                  &lt;phase&gt;package&lt;/phase&gt;
                  &lt;goals&gt;
                        &lt;goal&gt;jar&lt;/goal&gt;
                  &lt;/goals&gt;
                &lt;/execution&gt;
            &lt;/executions&gt;
      &lt;/plugin&gt;
      &lt;plugin&gt;
            &lt;groupId&gt;org.codehaus.mojo&lt;/groupId&gt;
            &lt;artifactId&gt;build-helper-maven-plugin&lt;/artifactId&gt;
            &lt;version&gt;1.9.1&lt;/version&gt;
            &lt;executions&gt;
                &lt;execution&gt;
                  &lt;id&gt;timestamp-property&lt;/id&gt;
                  &lt;goals&gt;
                        &lt;goal&gt;timestamp-property&lt;/goal&gt;
                  &lt;/goals&gt;
                &lt;/execution&gt;
            &lt;/executions&gt;
            &lt;configuration&gt;
                &lt;name&gt;BuildTime&lt;/name&gt;
                &lt;pattern&gt;yyyy-MM-dd HH:mm:ss.SSS&lt;/pattern&gt;
                &lt;timeZone&gt;GMT+8&lt;/timeZone&gt;
                &lt;regex/&gt;
                &lt;source/&gt;
                &lt;value/&gt;
            &lt;/configuration&gt;
      &lt;/plugin&gt;
      &lt;plugin&gt;
            &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
            &lt;artifactId&gt;maven-antrun-plugin&lt;/artifactId&gt;
            &lt;version&gt;1.3&lt;/version&gt;
            &lt;executions&gt;
                &lt;execution&gt;
                  &lt;id&gt;generate-release&lt;/id&gt;
                  &lt;phase&gt;compile&lt;/phase&gt;
                  &lt;goals&gt;
                        &lt;goal&gt;run&lt;/goal&gt;
                  &lt;/goals&gt;
                  &lt;configuration&gt;
                        &lt;tasks&gt;
                            &lt;!--suppress UnresolvedMavenProperty --&gt;
                            &lt;echo file="${project.basedir}/target/classes/NTopic.Release" message="Version=${project.version}${line.separator}BuildTime=${BuildTime}" /&gt;
                        &lt;/tasks&gt;
                  &lt;/configuration&gt;
                &lt;/execution&gt;
            &lt;/executions&gt;
      &lt;/plugin&gt;
      &lt;plugin&gt;
            &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
            &lt;artifactId&gt;maven-deploy-plugin&lt;/artifactId&gt;
            &lt;version&gt;2.7&lt;/version&gt;
            &lt;configuration&gt;
                &lt;!-- 特别注意的地方:指定打包的目录 --&gt;
                &lt;altDeploymentRepository&gt;internal.repo::default::file://${project.basedir}/../maven-repository/repository&lt;/altDeploymentRepository&gt;
            &lt;/configuration&gt;
      &lt;/plugin&gt;
    &lt;/plugins&gt;
&lt;/build&gt;
</code></pre>
<h3 id="打包并上传到仓库">打包并上传到仓库</h3>
<ul>
<li>打包并发布到本地目录命令:</li>
</ul>
<pre><code class="language-shell">mvn clean
mvn deploy -Dmaven.test.skip=true
</code></pre>
<ul>
<li>上传到远程仓库命令:</li>
</ul>
<pre><code class="language-shell">cd ./../maven-repository
git add --all
git commit -m 'Deploy sequence-jdbc JAR: https://github.com/obullxl/sequence-jdbc'
git push origin master
</code></pre>
<p>完整的打包命令,请参考分布式id序列源仓库代码:https://gitee.com/obullxl/sequence-jdbc/blob/master/deploy.sh:</p>
<pre><code class="language-shell">#!/bin/bash

# 本地打包
mvn clean &amp;&amp; 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
</code></pre>
<p><strong>多个版本</strong>完整的Maven托管仓库内容:</p>
<p><img src="https://img2023.cnblogs.com/blog/198133/202307/198133-20230701143127375-1311687821.jpg"></p>
<h2 id="其他项目使用jar包方法">其他项目使用JAR包方法</h2>
<p>和Maven官方的中心仓库相比,Gitee托管仓库没有本质区别,只需要在pom.xml中配置Gitee的托管仓库即可,让Maven知道从哪儿去下载JAR包。</p>
<h3 id="pomxml中增加仓库">pom.xml中增加仓库</h3>
<p><code>pom.xml</code>中增加Gitee托管仓库地址:</p>
<pre><code class="language-xml">&lt;repositories&gt;
&lt;repository&gt;
    &lt;id&gt;Gitee-obullxl&lt;/id&gt;
    &lt;url&gt;https://gitee.com/obullxl/maven-repository/raw/master/repository&lt;/url&gt;
&lt;/repository&gt;
&lt;/repositories&gt;
</code></pre>
<p>或者增加GitHub托管仓库地址:</p>
<pre><code class="language-xml">&lt;repositories&gt;
   &lt;repository&gt;
      &lt;id&gt;GitHub-obullxl&lt;/id&gt;
      &lt;url&gt;https://raw.githubusercontent.com/obullxl/maven-repository/master/repository&lt;/url&gt;
   &lt;/repository&gt;
&lt;/repositories&gt;
</code></pre>
<h3 id="maven配置依赖">Maven配置依赖</h3>
<p>和其他JAR包一样,<code>pom.xml</code>中增加依赖坐标:</p>
<pre><code class="language-xml">&lt;dependency&gt;
    &lt;groupId&gt;cn.ntopic&lt;/groupId&gt;
    &lt;artifactId&gt;sequence-jdbc&lt;/artifactId&gt;
    &lt;version&gt;1.0.2&lt;/version&gt;
&lt;/dependency&gt;
</code></pre>


</div>
<div id="MySignature" role="contentinfo">
    <p>本文作者:<b>奔跑的蜗牛</b>,转载请注明原文链接:https://ntopic.cn</p><br><br>
来源:https://www.cnblogs.com/obullxl/p/NTopic2023062201.html
頁: [1]
查看完整版本: 使用Gitee或GitHub托管Maven仓库JAR包的便捷方法