搭建maven私有仓库

目前maven私有仓库都是通过nexus来搭建的,所以本文也以nexus这个工具来搭建maven私有仓库
maven下载组件包的过程示意:

一、maven私有仓库搭建

注:最新版本为3.x,不过大多数公司仍然使用2.x,所以这里以安装2.x为例

1、下载源码

下载地址:https://www.sonatype.com/download-oss-sonatype

  1. wget https://sonatype-download.global.ssl.fastly.net/nexus/oss/nexus-2.14.5-02-bundle.tar.gz
2、解压并启动服务

官方文档:https://help.sonatype.com/display/NXRM2

(1)解压
  1. tar -zxvf nexus-2.14.5-02-bundle.tar.gz

会出现两个文件夹

  • nexus-2.14.5-02
  • sonatype-work
(2)移动文件夹
  1. cd /usr/local/src
  2. mv nexus-2.14.5-02 /usr/local/nexus
  3. mv sonatype-work /usr/local/sonatype-work
  4. chown -R shixinke:shixinke /usr/local/nexus
  5. chown -R shixinke:shixinke /usr/local/sonatype-work
(3)修改nexus的配置
  1. cd /usr/local/nexus
  2. vim conf/nexus.properties
  1. application-port=8082
  • 注:这里可以修改nexus的访问端口,默认为8081
(3)启动nexus
  1. bin/nexus start

在控制台有Started Nexus OSS.等日志输出时,可以在浏览器中打开 http://ip:port/nexus访问,如http://192.168.0.100:8082/nexus/,如果成功访问,则表明启动成功(这个启动需要一定的时间,请耐心等候)

二、nexus常用操作

1、仓库类型
  • hosted 类型的仓库,内部项目的发布仓库。代表宿主仓库,用来发布一些第三方不允许的组件,比如oracle驱动、比如商业软件jar包
  • releases 内部的模块中release模块的发布仓库
  • snapshots 发布内部的SNAPSHOT模块的仓库
  • 3rd party 第三方依赖的仓库,这个数据通常是由内部人员自行下载之后发布上去
  • proxy 类型的仓库,从远程中央仓库中寻找数据的仓库。表代理远程的仓库,最典型的就是Maven官方中央仓库、JBoss仓库等等
  • group 类型的仓库,组仓库用来方便我们开发人员进行设置的仓库
2、登录

默认用户名有admin,密码为admin123,需要登录后修改

3、更新中央仓库的索引
(1)自动更新

将远程仓库修改为阿里云的仓库,默认的apache的仓库因为网络的问题更新起来比较慢
阿里云中央仓库地址:http://maven.aliyun.com/nexus/content/repositories/central/

(2)手动更新

上面的方式通过网络更新更新索引,比较慢,下面使用手动更新索引(推荐)

  1. /usr/local/nexus/bin/nexus stop
  2. cd /usr/local/src
  3. java -jar indexer-cli-6.0.0.jar -u nexus-maven-repository-index.gz -d indexer

注:
(a) 更新索引前请关闭nexus服务,另外将自动下载远程的索引的选项设置
(b)生成索引可能需要花费一定的时间(大约2分钟左右),请耐心等待

  • 第四步:将生成的索引入入nexus索引目录中
  1. rm -rf /usr/local/sonatype-work/nexus/indexer/central-ctx/*
  2. cp /usr/local/src/indexer/* /usr/local/sonatype-work/nexus/indexer/central-ctx/
  3. /usr/local/nexus/bin/nexus start
  • 第五步:浏览索引,即可查看已经有相关的索引的,搜索”spring”即可查看到有相关的包了

三、将本地代码发布到私有仓库中

1、修改maven配置文件,将私有仓库加入到配置文件中

maven配置文件分为全局配置文件${MAVEN_HOME}/conf/settings.xml和用户配置文件~/.m2/settings.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
  5. <pluginGroups>
  6. <!-- pluginGroup
  7. | Specifies a further group identifier to use for plugin lookup.
  8. <pluginGroup>com.your.plugins</pluginGroup>
  9. -->
  10. </pluginGroups>
  11. <proxies>
  12. <!-- proxy
  13. | Specification for one proxy, to be used in connecting to the network.
  14. |
  15. <proxy>
  16. <id>optional</id>
  17. <active>true</active>
  18. <protocol>http</protocol>
  19. <username>proxyuser</username>
  20. <password>proxypass</password>
  21. <host>proxy.host.net</host>
  22. <port>80</port>
  23. <nonProxyHosts>local.net|some.host.com</nonProxyHosts>
  24. </proxy>
  25. -->
  26. </proxies>
  27. <!-- 私服仓库用户名和密码 -->
  28. <servers>
  29. <server>
  30. <id>shixinke-releases</id>
  31. <username>admin</username>
  32. <password>admin123</password>
  33. </server>
  34. <server>
  35. <id>shixinke-snapshots</id>
  36. <username>admin</username>
  37. <password>admin123</password>
  38. </server>
  39. </servers>
  40. <mirrors>
  41. </mirrors>
  42. <profiles>
  43. <!--仓库地址配置,与上面的Server的id要对应-->
  44. <profile>
  45. <id>shixinke</id>
  46. <repositories>
  47. <repository>
  48. <id>shixinke-release</id>
  49. <name>local private nexus</name>
  50. <url>http://10.2.82.56:8091/nexus/content/groups/public</url>
  51. <releases>
  52. <enabled>true</enabled>
  53. </releases>
  54. <snapshots>
  55. <enabled>false</enabled>
  56. </snapshots>
  57. </repository>
  58. <repository>
  59. <id>shixinke-snapshots</id>
  60. <name>local private nexus</name>
  61. <url>http://10.2.82.56:8091/nexus/content/repositories/snapshots</url>
  62. <releases>
  63. <enabled>false</enabled>
  64. </releases>
  65. <snapshots>
  66. <enabled>true</enabled>
  67. </snapshots>
  68. </repository>
  69. </repositories>
  70. <pluginRepositories>
  71. <pluginRepository>
  72. <id>shixinke-release</id>
  73. <name>local private nexus</name>
  74. <url>http://10.2.82.56:8091/nexus/content/groups/public</url>
  75. <releases>
  76. <enabled>true</enabled>
  77. </releases>
  78. <snapshots>
  79. <enabled>false</enabled>
  80. </snapshots>
  81. </pluginRepository>
  82. <pluginRepository>
  83. <id>shixinke-snapshots</id>
  84. <name>local private nexus</name>
  85. <url>http://10.2.82.56:8091/nexus/content/repositories/snapshots</url>
  86. <releases>
  87. <enabled>false</enabled>
  88. </releases>
  89. <snapshots>
  90. <enabled>true</enabled>
  91. </snapshots>
  92. </pluginRepository>
  93. </pluginRepositories>
  94. </profile>
  95. </profiles>
  96. <activeProfiles>
  97. <!--make the profile active all the time -->
  98. <activeProfile>shixinke</activeProfile>
  99. </activeProfiles>
  100. </settings>
2、在发布jar项目的pom.xml中添加仓库配置

在project节点下面添加如下内容:

  1. <distributionManagement>
  2. <repository>
  3. <id>shixinke-releases</id>
  4. <name>Release Repository</name>
  5. <url>http://10.2.82.56:8091/nexus/content/repositories/releases/</url>
  6. </repository>
  7. <snapshotRepository>
  8. <id>shixinke-snapshots</id>
  9. <name>Snapshot Repository</name>
  10. <url>http://10.2.82.56:8091/nexus/content/repositories/snapshots/</url>
  11. </snapshotRepository>
  12. </distributionManagement>
3、编写jar包程序,如:
  1. package com.shixinke.farm.client.data;
  2. import java.io.Serializable;
  3. import java.util.Date;
  4. /**
  5. * @author shixinke [ishixinke@qq.com]
  6. * @date 17-12-13.
  7. */
  8. public class UserData implements Serializable {
  9. private static final long serialVersionUID = 4202390739709155471L;
  10. private int userId;
  11. private String account;
  12. private String password;
  13. private String salt;
  14. private String nickname;
  15. private String email;
  16. private String mobile;
  17. private Date lastLoginTime;
  18. private String lastLoginIp;
  19. private Short status;
  20. private Date createTime;
  21. private Date updateTime;
  22. public int getUserId() {
  23. return userId;
  24. }
  25. public String getAccount() {
  26. return account;
  27. }
  28. public String getPassword() {
  29. return password;
  30. }
  31. public String getSalt() {
  32. return salt;
  33. }
  34. public String getNickname() {
  35. return nickname;
  36. }
  37. public String getEmail() {
  38. return email;
  39. }
  40. public String getMobile() {
  41. return mobile;
  42. }
  43. public Date getLastLoginTime() {
  44. return lastLoginTime;
  45. }
  46. public String getLastLoginIp() {
  47. return lastLoginIp;
  48. }
  49. public Short getStatus() {
  50. return status;
  51. }
  52. public Date getCreateTime() {
  53. return createTime;
  54. }
  55. public Date getUpdateTime() {
  56. return updateTime;
  57. }
  58. public void setUserId(int userId) {
  59. this.userId = userId;
  60. }
  61. public void setAccount(String account) {
  62. this.account = account;
  63. }
  64. public void setPassword(String password) {
  65. this.password = password;
  66. }
  67. public void setSalt(String salt) {
  68. this.salt = salt;
  69. }
  70. public void setNickname(String nickname) {
  71. this.nickname = nickname;
  72. }
  73. public void setEmail(String email) {
  74. this.email = email;
  75. }
  76. public void setMobile(String mobile) {
  77. this.mobile = mobile;
  78. }
  79. public void setLastLoginTime(Date lastLoginTime) {
  80. this.lastLoginTime = lastLoginTime;
  81. }
  82. public void setLastLoginIp(String lastLoginIp) {
  83. this.lastLoginIp = lastLoginIp;
  84. }
  85. public void setStatus(Short status) {
  86. this.status = status;
  87. }
  88. public void setCreateTime(Date createTime) {
  89. this.createTime = createTime;
  90. }
  91. public void setUpdateTime(Date updateTime) {
  92. this.updateTime = updateTime;
  93. }
  94. }
4、发布jar包

在idea编辑器中依次打开 “View”->”Tool Windows”->”Maven Porjects”打开maven面板,或者通过maven的部署插件来做

在maven面板中点击deploy即可:

5、到私服仓库查看刚部署的jar包

6、在其他项目中就可以使用刚部署的包了
  1. <dependency>
  2. <groupId>com.shixinke.farm.client</groupId>
  3. <artifactId>farm-client</artifactId>
  4. <version>1.0-SNAPSHOT</version>
  5. </dependency>