如何在本地搭建maven私服

来源:互联网 发布:车辆数据 编辑:程序博客网 时间:2024/06/14 04:00

1、安装前的准备。

 配置maven环境之前要确保已经安装了JDK,在命令行提示框里输入:java -version,如果出现以下界面,则表示JDK环境已经安装成功。

 

2、安装maven

下载maven,下载地址为:https://maven.apache.org/download.cgi

 

下载成功后,解压到指定的目录,如C:\,注意不要解压到中文文件夹,文件夹名最好不要有空格。解压成功后,配置maven环境变量,在windows高级选项里打开环境变量设置,新建系统变量M2_HOME,变量值为maven安装位置。

 

然后在path变量里添加%M2_HOME%\bin; 

 

配置完成后,测试maven是否配置成功,打开命令行提示框,输入mvn -v,如果出现下图信息,则表示maven已经配置成功。

 

3、在eclipse里配置maven

首先将maven安装目录conf文件夹下的settings.xml复制到C:\Users\wangdaoliang\.m2 (wangdaoliang是你的计算机名称,每个人不一样)里

 

打开eclipse,选择window-->preferences-->Maven,展开,设置User Settings和Installocations

 

 

至此,eclipse maven配置完成,这时如果直接导入maven工程,就会从settings.xml里指定的maven仓库地址下载jar包,默认是国外的maven地址,在国内经常出现下载不成功的情况,遇到这种情况,最好在本地搭建maven私服(强烈推荐),并把remote地址指向国内速度快的maven库,如开源中国的maven库。

4、使用Nexus搭建Maven私服

首先下载nexus(建议使用绿色版),下载地址:http://www.sonatype.org/nexus/go,我使用的nexus-2.10.0-02,然后解压到任意目录(非中文目录),打开目录nexus-2.10.0-02-bundle\nexus-2.10.0-02\bin\jsw, 这个目录下面你会发现有很多系统版本的nexus环境,根据个人电脑系统选择对应的版本。我的电脑是win10(X64),所以我选择的是windows-x86-64这个版本。

 

打开window-x86-64你会看到:

 

以管理员身份运行start-nexus.bat,就可以启动nexus服务了。我一般都是将nexus安装成windows服务,所以点击install-nexus.bat这个,访问http://localhost:8081/nexus/ 启动后如下页面,在右上角有个Log in 的超链接,点击登录。默认的用户名是 admin 密码是 admin123

 

进入后,选择Repositories,你会看到

 

再选择configuration,设置远程仓库地址为http://maven.oschina.net/content/groups/public/

 

选择sava。

然后再在C:\Users\wangdaoliang\.m2的settings.xml里选择nexus服务,配置相关属性,相关配置请自己查阅相关资料。下面是我的相关配置部分截图

 

也可以直接复制我的配置,只需修改你想存放的本地仓库地址就可以了。详细配置如下:

<?xml version="1.0" encoding="UTF-8"?><settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd"><localRepository>D:/Maven/repository</localRepository>  <pluginGroups>  </pluginGroups>  <proxies>  </proxies>  <servers>  <server> <id>releases</id><username>admin</username><password>admin123</password></server><server><id>snapshots</id><username>admin</username><password>admin123</password> </server>  </servers>  <mirrors>    <mirror> <!--此处配置所有的构建均从私有仓库中下载 *代表所有--> <id>nexus</id> <mirrorOf>*</mirrorOf> <url>http://localhost:8081/nexus/content/groups/public</url> </mirror>  </mirrors>  <profiles>    <profile> <id>nexus</id> <repositories> <repository> <id>central</id> <url>http://central</url> <releases><enabled>true</enabled></releases>  <snapshots><enabled>true</enabled></snapshots> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>central</id> <url>http://central</url> <releases><enabled>true</enabled></releases> <snapshots><enabled>true</enabled></snapshots> </pluginRepository> </pluginRepositories> </profile>  </profiles>  <activeProfiles> <activeProfile>nexus</activeProfile>  </activeProfiles></settings>

至此,maven私服搭建结束,这时maven工程就会自动从私服下载对应的jar包了,同时也会下载到本地仓库目录里,以后每次导入或创建maven工程,就会先从本地仓库里拉jar包,如果本地没有的话,就会从私服里拉。

现在我们服务器里已经建了maven私服,可以直接在settings.xml里配置url为私服仓库地址,本质上是一样的。不过我还是建议直接在本地搭建一个私服,这样回宿舍,自己创建maven工程时,也可以从公网里拉jar包,开源中国的maven库速度非常快。

0 0
原创粉丝点击