[toc]java
使用site-maven-plugin在github上搭建公有仓库git
Maven是咱们在开发java程序中常常使用的构建工具,在团队合做开发过程当中,若是咱们想要将本身写好的jar包共享给别人使用,一般须要本身搭建maven仓库,而后将写好的jar包上传到maven仓库中,以供其余用户使用。程序员
搭建maven仓库须要服务器和域名,对公司而言域名和服务器多的是,可是若是是咱们我的或者小团队想共享一些很是有用的jar包给别人使用就太麻烦了。github
最近Github好消息频出,先是对我的用户取消了repositories和协做用户的个数限制,后面对于企业用户也进行了升级和降价处理。若是仓库不大的话,彻底能够把仓库搬到github上面去。web
更多精彩内容且看:apache
更多内容请访问 www.flydean.com
要在github上面搭建maven仓库,咱们须要使用到maven的插件:site-maven-plugin。由于要连到github上面,因此须要设置github的oauth权限。直接用用户名密码也能够,可是这样作不安全,咱们并不推荐。安全
如上图所示,在Settings->Developer settings->Personal access tokens中建立一个access tokens,所需权限以下:服务器
注意,用户这里的权限必定要选,不然后面会报异常。
有了权限,接下来咱们再建立一个github-maven-repository,用来做为mvn仓库存储数据。maven
假如生成的地址是:https://github.com/flydean/gi...ide
这一步咱们须要编辑setting.xml文件,通常来讲这个文件是在~/.m2/settings.xml。
咱们须要添加一个Server,若是直接使用github的用户名密码,则像下面这样:
<server> <id>github</id> <username>YOUR_USERNAME</username> <password>YOUR_PASSWORD</password> </server>
前面咱们讲到了直接使用用户名是不安全的,咱们可使用上面建立的oauth key:
<server> <id>github</id> <password>OAUTH2TOKEN</password> </server>
这个id会在后面的pom.xml文件配置中用到,这里咱们先记下来。
咱们的目标是生成包含jar包的maven依赖。在将jar包上传到远程仓库以前,咱们须要在本地先生成。
先配置一个本地的repository:
<distributionManagement> <repository> <id>maven.repo</id> <name>Local Staging Repository</name> <url>file://${project.build.directory}/mvn-repo</url> </repository> </distributionManagement>
上面咱们指定了在项目的build目录下面建立了mvn-repo用来存储本地打好的package。
接下来,咱们须要使用maven-deploy-plugin指定将打好的包部署到刚刚咱们指定的local仓库中。
<plugin> <artifactId>maven-deploy-plugin</artifactId> <version>2.8.2</version> <configuration> <altDeploymentRepository>maven.repo::default::file://${project.build.directory}/mvn-repo</altDeploymentRepository> </configuration> </plugin>
如今咱们就可使用site-maven-plugin了:
<plugin> <!-- Deploy the web site --> <groupId>com.github.github</groupId> <artifactId>site-maven-plugin</artifactId> <version>0.12</version> <executions> <execution> <goals> <goal>site</goal> </goals> <!-- select the Maven phase in which the plugin will be executed --> <phase>deploy</phase> <configuration> <!-- Plugin configuration goes here --> <server>github</server> <!-- The commit message --> <message>init git maven repository</message> <!-- The location where the site is uploaded --> <repositoryName>github-maven-repository</repositoryName> <!-- github repo name --> <repositoryOwner>flydean</repositoryOwner> <!-- organization or user name --> <!-- Use merge or override the content --> <merge>true</merge> <outputDirectory>${project.build.directory}/mvn-repo</outputDirectory> <branch>refs/heads/mvn-repo</branch> <!-- <includes>--> <!-- <include>**/*</include>--> <!-- </includes>--> </configuration> </execution> </executions> </plugin>
使用中要注意下面几点:
<properties> <github.global.server>github</github.global.server> </properties>
好了,一切都配置完了,咱们能够运行了mvn deploy:
从上图能够看到,github上面已经有了一个可共享的项目了。
使用起来很简单,只须要在pom.xml文件中添加相应的依赖和repository便可:
<dependency> <groupId>YOUR.PROJECT.GROUPID</groupId> <artifactId>ARTIFACT-ID</artifactId> <version>VERSION</version> </dependency> <repository> <id>ARTIFACT-ID</id> <url>https://raw.github.com/flydean/github-maven-repository/mvn-repo/</url> </repository>
Github带给咱们的福利,赶忙用起来吧。
本文的例子[https://github.com/ddean2009/
learn-java-base-9-to-20](https://github.com/ddean2009/...
本文做者:flydean程序那些事本文连接:http://www.flydean.com/apache-maven-git-repository/
本文来源:flydean的博客
欢迎关注个人公众号:程序那些事,更多精彩等着您!