需求

  • 在用Maven的开发过程中,我们经常会遇到一些自有包的依赖,但又不能都在一个项目中(可能多个团队在维护),也有可能是我们用到了第三方的包,例如前段时间所有的阿里的包一样,我们把项目交给Maven管理后自已单独引进来的包在项目打包时是没办法打入包里面的,这时我们有两个解决办法一是前几天所说到的建立本地仓,这个方法简单但不易维护,接下来讲另一种方法就是建立基于Nexus的私服

安装Nexus

  • 我用的是Nexus2.9版本,3.3版本没有2.9方便可能是我没有弄明白吧。
  • 如果有需要的请网上搜,找不到的也可以发邮件给我,我提供给大家网盘共享
  • 下载下来后我们解压到任一目录,我是放在了D:\soft\nexus目录
  • 我们需要为Nexus建立环境变量,就是把BIN目录加入Path就可以
  • 做完上面几步后,我们可以在DOS窗口试一下命令 nexus 只是不是不认这个命令就是成功了
  • 安装服务,以管理员模式启动DOS后输入 nexus install 提示安装成功 再 nexus start 启动服务,只有第一次这样以后开机自启的
  • 默认端口号为 8081 如果修改,请修改 D:\Soft\nexus\nexus-2.9.0-04\conf\nexus.properties 中的application-port=9002 修改后记得重启服务
  • 运行 loaclhost:9002 可以看到如下图界面

  • 安装成功了,管理账号 admin 默认密码 admin123

Maven 设置

  • 在Pom文件中增加
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
27
28
29
30
31
32
33
34
35
36
<repositories>
<repository>
<id>nexus_public</id>
<url>http://nexus.hantianwei.com:9002/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>nexus_public</id>
<url>http://nexus.hantianwei.com:9002/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
<distributionManagement>
<repository>
<id>nexus_public</id>
<name>Nexus Release Repository</name>
<url>http://nexus.hantianwei.com:9002/nexus/content/repositories/releases/</url>
</repository>
<snapshotRepository>
<id>nexus_public</id>
<name>Nexus Snapshot Repository</name>
<url>http://nexus.hantianwei.com:9002/nexus/content/repositories/snapshots/</url>
</snapshotRepository>
</distributionManagement>
  • 在Maven的Setting中增加

    1
    2
    3
    4
    5
    6
    7
    <servers>
    <server>
    <id>nexus_public</id>
    <username>admin</username>
    <password>admin123</password>
    </server>
    </servers>
  • 到此设置完毕,这样是对每个项目单独的设置,也可以全在Setting中设置为全局性

    验证

  • 在项目中执行 Deploy 提示成功后,可以到Nexus中搜一下包名,就可以看到如下

  • 这样就和其它包一样填加引用就可
1
2
3
4
5
<dependency>
<groupId>com.hantianwei</groupId>
<artifactId>jgenerator</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
  • 是不是很方便,还可以为远程设置 开源中国 或 阿里云 的镜像