Make github as your personal maven repository

前言:

开始用maven管理java项目后,突然发现自己写了一些通用的项目想要被别的项目依赖是件很麻烦的事。公司里项目依赖可以直接有maven仓库,但个人项目呢?

github 再次显示其威力了,example:https://github.com/Lhfcws/mvn-repo/ 

网上找了很多博客教程,都不全面,决定自己搭建成功了就分享一个。


  

假设我有个项目名叫 pyara ,我的个人repo名叫 mvn-repo 。(其实就是真的,不用假设。。。) 


Repo:

建立mvn-repo的文件结构

1 mkdir -p mvn-repo/releases
2 mkdir -p mvn-repo/snapshots

在github上也同样建立一个remote的项目 mvn-repo。(github不会用的请先学习github)

repo这边基本就完成了


Local Settings:

打开或新建(如果没有的话) ~/.m2/settings.xml ,~/.m2/ 为本机maven下载依赖的路径,里面存放着dependency的jar包。

添加一下内容:

1 <settings xmlns=" http://maven.apache.org/POM/4.0.0 " xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance " xsi:schemaLocation=" http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd ">
2     <servers>
3         <server>
4             <id>github</id>
5             <username>USERNAME</username>
6             <password>PASSWORD</password>
7         </server>
8     </servers>
9 </settings>

USERNAME 和 PASSWORD 即你的github的账户密码。保存后将settings.xml设置权限,防止你的密码泄漏。

1 sudo chmod 700 ~/.m2/settings.xml

Project pom.xml:

这个是最重要的一步,此处假设你已有一个可以 mvn package 成功的pom.xml 。

github设置,可以帮你自动commit。

1  <properties>
2         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
3         <!-- github server corresponds to entry in ~/.m2/settings.xml -->
4         <github.global.server>github</github.global.server>
5  </properties>
 1 <plugin>
 2                 <groupId>com.github.github</groupId>
 3                 <artifactId>site-maven-plugin</artifactId>
 4                 <version>0.9</version>
 5                 <configuration>
 6                     <message>Maven artifacts for ${project.version}</message>  <!-- git commit message -->
 7                     <noJekyll>true</noJekyll>                                  <!-- disable webpage processing -->
 8                     <outputDirectory>/home/lhfcws/coding/workspace/mvn-repo</outputDirectory> <!-- matches distribution management repository url above -->
 9                     <branch>refs/heads/mvn-repo</branch>                       <!-- remote branch name -->
10                     <includes>
11                         <include>
12                             **/*
13                         </include>
14                     </includes>
15                     <repositoryName>pyara</repositoryName>      <!-- github repo name -->
16                     <repositoryOwner>lhfcws</repositoryOwner>    <!-- github username  -->
17                 </configuration>
18                 <executions>
19                     <!-- run site-maven-plugin's 'site' target as part of the build's normal 'deploy' phase -->
20                     <execution>
21                         <goals>
22                             <goal>site</goal>
23                         </goals>
24                         <phase>deploy</phase>
25                     </execution>
26                 </executions>
27 </plugin>

repo设置:

distributionManagement 指定 deploy 的 destination ,切记url里需要指定协议,否则Wagon无法支持。

repositories 指定查找dependencies 的地方,repositories可以根据自己需求改变路径。

 1    <distributionManagement>
 2         <repository>
 3             <id>releases</id>
 4             <!--            <url>https://github.com/Lhfcws/mvn-repo/raw/master/releases</url> -->
 5             <url>file://${project.basedir}/../mvn-repo/releases</url>
 6         </repository>
 7         <snapshotRepository>
 8             <id>snapshots</id>
 9             <url>file://${project.basedir}/../mvn-repo/snapshots</url>
10         </snapshotRepository>
11     </distributionManagement>
12     <repositories>
13         <repository>
14             <id>lhfcws-mvn-repo</id>
15             <url>https://raw.github.com/lhfcws/mvn-repo/master/</url>
16             <releases>
17                 <enabled>true</enabled>
18             </releases>
19             <snapshots>
20                 <enabled>true</enabled>
21             </snapshots>
22         </repository>
23     </repositories>

添加 Wagon 支持:

Wagon 插件可以帮助deploy时的文件传输(http, scp, scm, file, ftp等)。

1 <extensions> 
2             <extension> 
3                 <artifactId>wagon-webdav-jackrabbit</artifactId> 
4                 <groupId>org.apache.maven.wagon</groupId> 
5                 <version>2.2</version> 
6             </extension> 
7 </extensions> 

 如果deploy时抛错类似下面:

Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.7:deploy (default-deploy) on project pyara: Failed to deploy artifacts/metadata: No connector available to access repository snapshots (/home/lhfcws/coding/workspace/pyara/../mvn-repo/snapshots) of type default using the available factories WagonRepositoryConnectorFactory

基本不是没有添加Wagon extension,就是没有指定协议或协议不支持。

SCM 支持:

可选,应该是可以不加的,除非指定要下载代码。

1 <scm>
2         <connection>scm:git:git://github.com/Lhfcws/pyara.git</connection>
3         <url>scm:git:git://github.com/Lhfcws/pyara.git</url>
4         <developerConnection>scm:git:git://github.com/Lhfcws/pyara.git</developerConnection>
5 </scm>


至此,尝试一下mvn deploy,见证奇迹的时刻。

最后deploy成功后还需要把本地仓库的mvn-repo提交到github,over。

原文地址:https://www.cnblogs.com/lhfcws/p/3503707.html