说明:将github的某个仓库作为公共的maven仓库,可以将自己写的项目发布到github上的公共的maven仓库里,然后再以pom引入依赖的方式使用。

1.上传时,可以选择不同版本上传的不同的分支上,此处时master分支,例,可以创建snapshot,release等分支。

2.将项目打包发布到github公共maven仓库几种方式

  • maven项目通过配置pom.xml实现(不推荐,配置繁琐)
  • 命令行方式(推荐,命令行操作)
  • 脚本方式(极力推荐操作简单)

一、maven项目通过配置pom.xml实现

配置项目deploy(发布)到github步骤

1、完善github用户名配置
登录到github中,然后点击Settings–>修改用户名(英文)
点击”Update profile” 保存修改。

修改用户名目的:防止上传jar报未知异常,例如” For ‘properties/name’, nil is not a string”异常。

2、配置maven工具的settings.xml文件,找到servers标签,添加一个server

<server>
    <id>github</id>
    <username>guihub登录的用户名</username>
    <password>guihub登录的用户密码</password>
</server>

3、将jar deploy(发布)到本地存储库中
在maven项目的pom.xml中添加入下代码

<!--1.作用:将jar deploy(发布)到本地储存库位置(altDeploymentRepository)-->
    <plugin>
        <artifactId>maven-deploy-plugin</artifactId>
        <version>2.8.1</version>
        <configuration>
            <altDeploymentRepository>internal.repo::default::file://${project.build.directory}/mvn-repo</altDeploymentRepository>
        </configuration>
    </plugin>

运行命令: 将本地的jar发布到本地仓库中

mvn clean deploy

即可在对应项目中的target/mvn-repo目录(存储库)下找到本地的jar.

4、将本地存储库位置的jar文件发布到github上
在maven项目的pom.xml中添加入下代码

<properties>
        <github.global.server>github</github.global.server>
    </properties>
    
    <!--2.作用:将本地存储库位置的jar文件发布到github上-->
    <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>
            <!--本地jar相关文件地址,与上方配置储存库位置(altDeploymentRepository)保持一致-->
            <outputDirectory>${project.build.directory}/mvn-repo</outputDirectory>
            <!--配置上传到github哪个分支,此处配置格式必须以refs/heads/+分支名称-->
            <branch>refs/heads/master</branch>
            <merge>true</merge>
            <includes>
                <include>**/*</include>
            </includes>
            <!--对应github上创建的仓库名称 name-->
            <repositoryName>mvn-repo</repositoryName>
            <!--github 仓库所有者即登录用户名-->
            <repositoryOwner>zhengjiaao</repositoryOwner>
        </configuration>
        <executions>
            <execution>
                <goals>
                    <goal>site</goal>
                </goals>
                <phase>deploy</phase>
            </execution>
        </executions>
    </plugin>

再次执行命令:发布到github上

mvn clean deploy

此时打开github查看并刷新仓库mvn-repo,已经存在上传的jar。

上传后效果图:

5、使用已上传github存储库的jar并测试是否可用。
使用上传到github上的jar依赖
随便找个maven项目,在pom.xml添加配置

<repositories>
        <repository>
            <id>mvn-repo</id>
            <!--将"zhengjiaao"改为 你的github用户名-->
            <url>https://raw.github.com/zhengjiaao/mvn-repo/master</url>
            <snapshots>
                <enabled>true</enabled>
                <updatePolicy>always</updatePolicy>
            </snapshots>
        </repository>
    </repositories>
    
    <dependency>
        <groupId>com.zja</groupId>
        <artifactId>github-util</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>

测试jar包里的实体类是否可用:

//没有报错,可以使用github-util jar中的类
AbcEntity abcEntity = new AbcEntity();

项目的pom.xml完整deploy配置

<properties>
        <github.global.server>github</github.global.server>
    </properties>

    <build>
        <plugins>
            <!--1.作用:将jar deploy(发布)到本地储存库位置(altDeploymentRepository)-->
            <plugin>
                <artifactId>maven-deploy-plugin</artifactId>
                <version>2.8.1</version>
                <configuration>
                    <altDeploymentRepository>internal.repo::default::file://${project.build.directory}/mvn-repo</altDeploymentRepository>
                </configuration>
            </plugin>

            <!--2.作用:将本地存储库位置的jar项目文件发布到github上-->
            <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>
                    <!--本地jar相关文件地址,与上方配置储存库位置(altDeploymentRepository)保持一致-->
                    <outputDirectory>${project.build.directory}/mvn-repo</outputDirectory>
                    <!--配置上传到github哪个分支,此处配置格式必须以refs/heads/+分支名称-->
                    <branch>refs/heads/master</branch>
                    <merge>true</merge>
                    <includes>
                        <include>**/*</include>
                    </includes>
                    <!--对应github上创建的仓库名称 name-->
                    <repositoryName>mvn-repo</repositoryName>
                    <!--github 仓库所有者即登录用户名-->
                    <repositoryOwner>zhengjiaao</repositoryOwner>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>site</goal>
                        </goals>
                        <phase>deploy</phase>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

二、命令行方式

新建本地存储库位置:D:/GitHub/maven-repository
执行命令:将项目发布到本地存储库

## deploy项目到本地仓库
mvn clean deploy -Dmaven.test.skip  -DaltDeploymentRepository=self-mvn-repo::default::file:D:/GitHub/maven-repository

github新建远程仓库,仓库名称:maven-repository
执行命令:将项目存储库里的jar上传到github 的maven-repository仓库上

#1.进入项目到本地仓库
$ cd D:/GitHub/maven-repository
#添加md文档
$ echo "# maven-repository" >> README.md
#2.git初始化
$ git init
#3.将本地仓库内容添加到暂存区
$ git add .
#4.将暂存区内容提交
$ git commit -m "提交存储库"
#5.绑定远程github仓库
$ git remote add origin https://github.com/zhengjiaao/maven-repository.git
#6.将本地仓库推送至远程github的maven-repository仓库上
$ git push -u origin master

使用上传的jar
随便找个maven项目,在pom.xml添加配置

<repositories>
        <repository>
            <id>maven-repository</id>
            <!--将"zhengjiaao"改为 你的github用户名-->
            <url>https://raw.github.com/zhengjiaao/maven-repository/master</url>
            <snapshots>
                <enabled>true</enabled>
                <updatePolicy>always</updatePolicy>
            </snapshots>
        </repository>
    </repositories>
    
    <dependency>
        <groupId>com.zja</groupId>
        <artifactId>github-util</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>

测试jar包里的实体类是否可用:

//没有报错,可以使用github-util jar中的类
AbcEntity abcEntity = new AbcEntity();

三、脚本方式

1.将git安装位置下的cmd目录配置到环境变量中

例如:D:\Git\cmd

2.新建github远程仓库,仓库名称:maven-repository

远程仓库地址:https://github.com/zhengjiaao/maven-repository.git

relation.bat脚本作用:(脚本可放在任何电脑位置,“双击执行脚本”)

1.创建本地仓库,并与远程仓库关联
2.新建master、snapshot、release等分支

relation.bat:直接双击执行脚本

chcp 65001
::此脚本仅执行一次,不可多次执行
::【需要修改1】:github本地仓库,非maven存储库
set DEPLOY_PATH=D:\GitHub\maven-repository

D:
::创建本地仓库
md %DEPLOY_PATH%
cd %DEPLOY_PATH%

::将本地仓库与远程仓库关联
echo "# maven-repository" >> README.md
git init
git add README.md
git commit -m "first commit"
git remote add origin https://github.com/zhengjiaao/maven-repository.git
git push -u origin master

::新建分支snapshot,并将新建的分支推送到远程仓库
git switch -c snapshot
git push --set-upstream origin snapshot

git switch -c release
git push --set-upstream origin release

pause

分支作用:
master分支管理所有的版本
snapshot分支只管理项目中的snapshot版本
release分支只管理项目中的release版本

4.新建脚本放到项目根路径下

执行脚本前提:远程仓库已有master、snapshot、release分支

github.bat:传参执行脚本 例如:./github.bat r

chcp 65001
echo "注:1、将此bat脚本文件放到项目跟路径下"
echo "    2、启动CMD 参传方式: github.bat r/s"
echo "    3、参数说明:r/s r是release正式版本,s是snapshot快照版本"

:: deploy参数,snapshot 表示快照包,简写为s, release表示正式包,简写为r
set arg=%1

::【需要修改1】:github本地存储库,非maven存储库
set DEPLOY_PATH=D:/GitHub/maven-repository/
::分支
set "branch="

:: 快照包发布 snapshot分支
if "s"=="%arg%" (
    set "branch=snapshot"
)

:: 正式包发布 release分支
if "r"=="%arg%" (
    set "branch=release"
)

D:
cd %DEPLOY_PATH%
git pull

echo 切换对应分支%branch%
git checkout %branch%

::【需要修改2】:项目的磁盘
J:
::回到项目当前根目录
cd %~dp0
echo 开始deploy,将项目发布到本地存储库%DEPLOY_PATH%
call mvn clean deploy -Dmaven.test.skip  -DaltDeploymentRepository=self-mvn-repo::default::file:%DEPLOY_PATH%

D:
cd %DEPLOY_PATH%
echo 本地存储库的发送到github仓库%branch%分支上
git add .
git commit -m "提交新的版本"
git pull
git push origin %branch%

echo 将%branch%分支合并到master分支
git checkout master
git add .
git git commit -m 'master'
git merge %branch%
git commit -m 'master merge'
git push origin master

::git push origin master

pause