说明

使用maven搭建maven仓库,找了网上的一些方法,踩了写坑,鼓捣了大半天终于弄好了,特此记录备忘。
网上基本上有两种方法,但本质上都是一样的,一种是将工程编译到本地,再将本地编译好的jar等文件上传到GitHub。另一种是在pom文件中配置插件,将编译上传使用插件完成。

创建GitHub仓库

在GitHub上创建一个仓库作为个人的maven仓库,以后自己编译后的jar都可以发布到该仓库中,我建立的仓库是maven-repo。

修改settings.xml

servers节点下增加

1
2
3
4
5
<server>
<id>github</id>
<username>github的用户名</username>
<password>github的密码</password>
</server>

修改pom.xml

添加一个属性

1
2
3
<properties>
<github.global.server>github</github.global.server>
</properties>

增加一个plugin,把编译后的jar放在target目录下的mvn-repo目录

1
2
3
4
5
6
7
<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>

继续增加一个plugin

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<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>
<outputDirectory>${project.build.directory}/mvn-repo</outputDirectory>
<branch>refs/heads/master</branch>
<includes>
<include>**/*</include>
</includes>
<repositoryName>maven-repo</repositoryName>
<!-- 对应github上创建的仓库名称 name -->
<repositoryOwner>你的用户名</repositoryOwner>
<!-- github 仓库所有者 -->
</configuration>
<executions>
<execution>
<goals>
<goal>site</goal>
</goals>
<phase>deploy</phase>
</execution>
</executions>
</plugin>

运行maven的clean deploy命令就可以把jar包上传到GitHub的maven-repo仓库中了。

这种方式需要对pom文件做一些修改,也可以自己先把jar编译到本地,再将本地文件夹上传到GitHub,两种方式的原理其实是一样的。下面说一下编译到本地的方式。

修改pom.xml

在GitHub上创建好仓库后,clone到本地并初始化,如我的本地目录是D:\wjj\github\maven-repo。修改pom增加如下配置

1
2
3
4
5
6
<distributionManagement>
<repository>
<id>wjj-maven-repo</id>
<url>file:D:/wjj/github/maven-repo</url>
</repository>
</distributionManagement>

上面配置是指定maven将jar包编译到本地,编译成功后,在将本地目录maven-repo提交到GitHub就行了。

可以参考我的示例 https://github.com/zhanyingf15/maven-repo

使用

在新的maven工程pom.xml中添加配置,指定GitHub上的仓库

1
2
3
4
5
6
<repositories>
<repository>
<id>wjj-maven-repo</id>
<url>https://raw.github.com/zhanyingf15/maven-repo/master</url>
</repository>
</repositories>

指定仓库后就可以添加依赖了,如下

1
2
3
4
5
<dependency>
<groupId>com.wjj</groupId>
<artifactId>elasticsearch-jdbc</artifactId>
<version>2.0.0</version>
</dependency>

在测试的时候发现包并不能下载下来,原因是我使用了阿里的maven仓库镜像,在mirror配置中mirrorOf配置为*,这样会覆盖我们在pom中自定义的GitHub仓库,修改
<mirrorOf>*</mirrorOf><mirrorOf>*,!wjj-maven-repo</mirrorOf>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<mirror>
<!--This sends everything else to /public -->
<id>nexus</id>
<mirrorOf>*,!wjj-maven-repo</mirrorOf>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
</mirror>
<mirror>
<!--This is used to direct the public snapshots repo in the
profile below over to a different nexus group -->
<id>nexus-public-snapshots</id>
<mirrorOf>public-snapshots</mirrorOf>
<url>http://maven.aliyun.com/nexus/content/repositories/snapshots/</url>
</mirror>
</mirrors>

关于mirrorOf的配置参考官方文档

部分内容参考自下面的博客

参考1:http://blog.csdn.net/hwangfantasy/article/details/69389766

参考2:http://www.importnew.com/19178.html