1、nexus下载并安装
从nexus官方网站http://www.sonatype.org/nexus/go下载nexus软件,比如nexus-2.7.2-03。
下载后解压,把%NEXUS_HOME%/bin路径加入到path环境变量中,然后以管理员身份运行nexus install命令安装nexus,安装成功之后,运行nexus start启动nexus。启动之后,在浏览器中输入http://localhost:8081/nexus/即可访问nexus。
2、nexus配置
以管理员身份登陆nexus,用户名和密码默认为admin/admin123。
登陆之后,点击左侧的Repositories,可以看到已经建好的仓库。
由于新搭建的仓库都是空的,首先对仓库进行远程同步。比如,选择central仓库,在Configuration中配置Download Remote Indexes为true。
3、Maven配置
搭建私服,自然希望工程在下载jar包的时候能够首先从私服下载,这样就还需要对Maven进行配置。
打开Maven的配置文件settings.xml,在在profiles下添加如下配置。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
< profile >
< id >nexus</ id >
< repositories >
< repository >
< id >local</ id >
< url >http://localhost:8081/nexus/content/groups/public/</ url >
< releases >< enabled >true</ enabled ></ releases >
< snapshots >< enabled >true</ enabled ></ snapshots >
</ repository >
</ repositories >
< pluginRepositories >
< pluginRepository >
< id >local</ id >
< url >http://localhost:8081/nexus/content/groups/public/</ url >
< releases >< enabled >true</ enabled ></ releases >
< snapshots >< enabled >true</ enabled ></ snapshots >
</ pluginRepository >
</ pluginRepositories >
</ profile >
|
要使这段配置信息生效,不要忘记添加如下配置。
1
2
3
|
< activeProfiles >
< activeProfile >nexus</ activeProfile >
</ activeProfiles >
|
4、检查配置是否成功
新建一个Maven工程,添加Maven依赖如下。
1
2
3
4
5
6
7
|
< dependencies >
< dependency >
< groupId >org.apache.commons</ groupId >
< artifactId >commons-lang3</ artifactId >
< version >3.2.1</ version >
</ dependency >
</ dependencies >
|
在工程路径下,运行maven命令mvn clean package。Maven首先会从nexus私服中下载,但是新建的nexus私服中并没有相应的jar包,于是nexus会从远程仓库中下载相应的jar包,Maven工程再从nexus私服中下载。如果再有新的工程需要下载这个jar包,就可以直接从私服中下载了。而且还可以在nexus中搜索出相应的jar包了。如此可见相关配置已经可以正常地工作了。