
說明:將github的某個(gè)倉庫作為公共的maven倉庫,可以將自己寫的項(xiàng)目發(fā)布到github上的公共的maven倉庫里,然后再以pom引入依賴的方式使用。
1.上傳時(shí),可以選擇不同版本上傳的不同的分支上,此處時(shí)master分支,例,可以創(chuàng)建snapshot,release等分支。
2.將項(xiàng)目打包發(fā)布到github公共maven倉庫幾種方式
- maven項(xiàng)目通過配置pom.xml實(shí)現(xiàn)(不推薦,配置繁瑣)
- 命令行方式(推薦,命令行操作)
- 腳本方式(極力推薦操作簡單)
一、maven項(xiàng)目通過配置pom.xml實(shí)現(xiàn)
配置項(xiàng)目deploy(發(fā)布)到github步驟
1、完善github用戶名配置
登錄到github中,然后點(diǎn)擊Settings-->修改用戶名(英文)
點(diǎn)擊"Update profile" 保存修改。
修改用戶名目的:防止上傳jar報(bào)未知異常,例如" For 'properties/name', nil is not a string"異常。
2、配置maven工具的settings.xml文件,找到servers標(biāo)簽,添加一個(gè)server
<server>
<id>github</id>
<username>guihub登錄的用戶名</username>
<password>guihub登錄的用戶密碼</password>
</server>
3、將jar deploy(發(fā)布)到本地存儲(chǔ)庫中
在maven項(xiàng)目的pom.xml中添加入下代碼
<!--1.作用:將jar deploy(發(fā)布)到本地儲(chǔ)存庫位置(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>
運(yùn)行命令: 將本地的jar發(fā)布到本地倉庫中
mvn clean deploy
即可在對(duì)應(yīng)項(xiàng)目中的target/mvn-repo目錄(存儲(chǔ)庫)下找到本地的jar.
4、將本地存儲(chǔ)庫位置的jar文件發(fā)布到github上
在maven項(xiàng)目的pom.xml中添加入下代碼
<properties>
<github.global.server>github</github.global.server>
</properties>
<!--2.作用:將本地存儲(chǔ)庫位置的jar文件發(fā)布到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相關(guān)文件地址,與上方配置儲(chǔ)存庫位置(altDeploymentRepository)保持一致-->
<outputDirectory>${project.build.directory}/mvn-repo</outputDirectory>
<!--配置上傳到github哪個(gè)分支,此處配置格式必須以refs/heads/+分支名稱-->
<branch>refs/heads/master</branch>
<merge>true</merge>
<includes>
<include>**/*</include>
</includes>
<!--對(duì)應(yīng)github上創(chuàng)建的倉庫名稱 name-->
<repositoryName>mvn-repo</repositoryName>
<!--github 倉庫所有者即登錄用戶名-->
<repositoryOwner>zhengjiaao</repositoryOwner>
</configuration>
<executions>
<execution>
<goals>
<goal>site</goal>
</goals>
<phase>deploy</phase>
</execution>
</executions>
</plugin>
再次執(zhí)行命令:發(fā)布到github上
mvn clean deploy
此時(shí)打開github查看并刷新倉庫mvn-repo,已經(jīng)存在上傳的jar。
上傳后效果圖:

5、使用已上傳github存儲(chǔ)庫的jar并測(cè)試是否可用。
使用上傳到github上的jar依賴
隨便找個(gè)maven項(xiàng)目,在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>
測(cè)試jar包里的實(shí)體類是否可用:
//沒有報(bào)錯(cuò),可以使用github-util jar中的類
AbcEntity abcEntity = new AbcEntity();
項(xiàng)目的pom.xml完整deploy配置
<properties>
<github.global.server>github</github.global.server>
</properties>
<build>
<plugins>
<!--1.作用:將jar deploy(發(fā)布)到本地儲(chǔ)存庫位置(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.作用:將本地存儲(chǔ)庫位置的jar項(xiàng)目文件發(fā)布到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相關(guān)文件地址,與上方配置儲(chǔ)存庫位置(altDeploymentRepository)保持一致-->
<outputDirectory>${project.build.directory}/mvn-repo</outputDirectory>
<!--配置上傳到github哪個(gè)分支,此處配置格式必須以refs/heads/+分支名稱-->
<branch>refs/heads/master</branch>
<merge>true</merge>
<includes>
<include>**/*</include>
</includes>
<!--對(duì)應(yīng)github上創(chuàng)建的倉庫名稱 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>
二、命令行方式
新建本地存儲(chǔ)庫位置:D:/GitHub/maven-repository
執(zhí)行命令:將項(xiàng)目發(fā)布到本地存儲(chǔ)庫
## deploy項(xiàng)目到本地倉庫
mvn clean deploy -Dmaven.test.skip -DaltDeploymentRepository=self-mvn-repo::default::file:D:/GitHub/maven-repository
github新建遠(yuǎn)程倉庫,倉庫名稱:maven-repository
執(zhí)行命令:將項(xiàng)目存儲(chǔ)庫里的jar上傳到github 的maven-repository倉庫上
#1.進(jìn)入項(xiàng)目到本地倉庫
$ cd D:/GitHub/maven-repository
#添加md文檔
$ echo "# maven-repository" >> README.md
#2.git初始化
$ git init
#3.將本地倉庫內(nèi)容添加到暫存區(qū)
$ git add .
#4.將暫存區(qū)內(nèi)容提交
$ git commit -m "提交存儲(chǔ)庫"
#5.綁定遠(yuǎn)程github倉庫
$ git remote add origin https://github.com/zhengjiaao/maven-repository.git
#6.將本地倉庫推送至遠(yuǎn)程github的maven-repository倉庫上
$ git push -u origin master
使用上傳的jar
隨便找個(gè)maven項(xiàng)目,在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>
測(cè)試jar包里的實(shí)體類是否可用:
//沒有報(bào)錯(cuò),可以使用github-util jar中的類
AbcEntity abcEntity = new AbcEntity();
三、腳本方式
-
1.將git安裝位置下的cmd目錄配置到環(huán)境變量中
例如:D:\Git\cmd
-
2.新建github遠(yuǎn)程倉庫,倉庫名稱:maven-repository
遠(yuǎn)程倉庫地址:https://github.com/zhengjiaao/maven-repository.git
relation.bat腳本作用:(腳本可放在任何電腦位置,“雙擊執(zhí)行腳本”)
1.創(chuàng)建本地倉庫,并與遠(yuǎn)程倉庫關(guān)聯(lián)
2.新建master、snapshot、release等分支relation.bat:直接雙擊執(zhí)行腳本
chcp 65001 ::此腳本僅執(zhí)行一次,不可多次執(zhí)行 ::【需要修改1】:github本地倉庫,非maven存儲(chǔ)庫 set DEPLOY_PATH=D:\GitHub\maven-repository D: ::創(chuàng)建本地倉庫 md %DEPLOY_PATH% cd %DEPLOY_PATH% ::將本地倉庫與遠(yuǎn)程倉庫關(guān)聯(lián) 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,并將新建的分支推送到遠(yuǎn)程倉庫 git switch -c snapshot git push --set-upstream origin snapshot git switch -c release git push --set-upstream origin release pause分支作用:
master分支管理所有的版本
snapshot分支只管理項(xiàng)目中的snapshot版本
release分支只管理項(xiàng)目中的release版本 -
4.新建腳本放到項(xiàng)目根路徑下
執(zhí)行腳本前提:遠(yuǎn)程倉庫已有master、snapshot、release分支
github.bat:傳參執(zhí)行腳本 例如:./github.bat r
chcp 65001
echo "注:1、將此bat腳本文件放到項(xiàng)目跟路徑下"
echo " 2、啟動(dòng)CMD 參傳方式: github.bat r/s"
echo " 3、參數(shù)說明:r/s r是release正式版本,s是snapshot快照版本"
:: deploy參數(shù),snapshot 表示快照包,簡寫為s, release表示正式包,簡寫為r
set arg=%1
::【需要修改1】:github本地存儲(chǔ)庫,非maven存儲(chǔ)庫
set DEPLOY_PATH=D:/GitHub/maven-repository/
::分支
set "branch="
:: 快照包發(fā)布 snapshot分支
if "s"=="%arg%" (
set "branch=snapshot"
)
:: 正式包發(fā)布 release分支
if "r"=="%arg%" (
set "branch=release"
)
D:
cd %DEPLOY_PATH%
git pull
echo 切換對(duì)應(yīng)分支%branch%
git checkout %branch%
::【需要修改2】:項(xiàng)目的磁盤
J:
::回到項(xiàng)目當(dāng)前根目錄
cd %~dp0
echo 開始deploy,將項(xiàng)目發(fā)布到本地存儲(chǔ)庫%DEPLOY_PATH%
call mvn clean deploy -Dmaven.test.skip -DaltDeploymentRepository=self-mvn-repo::default::file:%DEPLOY_PATH%
D:
cd %DEPLOY_PATH%
echo 本地存儲(chǔ)庫的發(fā)送到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