maven学习(中)- 私服nexus搭建

上回继续,相信你们对maven每次都联网从国外站点下载依赖项的龟速网络已经不坎忍受了,今天先来看看如何搭建"仓库私服",目前nexus是使用比较普遍的私服软件之一,下面将介绍基本用法:html

 

1、到nexus官网下载最新版java

1.1 下载地址:http://www.sonatype.org/downloads/nexus-latest-bundle.zip (目前最新版本是2.7.2-03)apache

1.2 下载完后,解压到指定目录,好比:c:\java\nexus\windows

1.3 启动nexus缓存

%nexus_home%\bin\nexus.bat install (安装成windows服务)服务器

%nexus_home%\bin\nexus.bat start (启动)网络

tips : 直接nexus.bat不带参数,会显示完整的参数列表eclipse

参考下图,若是启用不起来,检查jdk版本,nexus要求1.7版本(若是版本不对,请调整环境变量JAVA_HOME指向jdk1.7的目录)maven

若是换成1.7仍是启用不了,建议用nexus.bat console 控制台方式启用,这样能够看到详细启动过程url

启动成功后,能够用http://localhost:8081/nexus 进入控制界面

点击右上角的Log In,默认用户名/密码是admin/admin123

 

2、添加jboss资源库地址

2.1 先了解下nexus的资源库类型:

Hosted Repository - nexus本机的资源库(至关于nexus所在服务器硬盘上已经存在的jar、pom文件库)

Proxy Repository - 代理库,不少开源官方组织都在互联网上公布了Repository,供全世界的开发者使用,nexus中设置该类型的资源库后,其它使用nexus的开发者,表面上是在请求nexus服务器上的jar包,但nexus实质是背后是在请求这些互联网资源,首次会把服务器上没有的资源,从网上download到nexus服务器(以文件形式保存到硬盘上),而后再返回给开发者,下次再有一样的pom/jar请求时,直接从服务器硬盘上返回,再也不请求互联网

Repository Group - 资源组,好比咱们有不少资源库:资源库A、资源库B...,能够把它们再成组合一个特殊的资源C,而后其它开发人员,在maven里配置资源C地址,这样maven在向资源C请求jar包里,实质会搜索资源A、资源B

 

2.2 添加jboss 代理资源库

maven默认的官方资源库http://repo1.maven.org/maven2/,不少第三方的jar包没有,因此我再添加一个jboss的资源库

点击左侧的Repositories,而后再点击右侧的Add,会弹出下拉菜单,选择Proxy Repository

接下来,参考下图填写:

Respository ID这里填写:jboss-public-repository-group (能够自已更名字)

Respository Name这里填写:JBoss Public Maven Repository Group (能够自已更名字)

其它项保持默认

Remote Storage Location这里填写:https://repository.jboss.org/nexus/content/groups/public-jboss/ (这是jboss公布的公开资源库)

 

2.3 下载jboss eap 6.x的离线资源包

首次使用nexus时,私服服务器上实质上没有任何jar包的缓存,因此第一次使用时,其实跟直接到互联网上下载没啥区别,只有后面再请求相同的jar包,服务器上才会有jar文件缓存,为了加快速度,咱们能够直接上jboss官网下载离线资源包

下载地址: htp://www.jboss.org/jbossas/downloads

而后解压到指定目录,建议放在 %nexus_home%\sonatype-work\nexus\storage\ 下

而后add一个hosted repository,参考下图:

 

2.4 把刚才添加的二个respoitory添加到Public Repositories组

刚才咱们添加了二个新的资源库,检查一下是否包括在默认的Public Repositories组里

若是没包括在组里,参考上图处理一下,而后保存。最后建议更新一下索引,参考下图:

 

3、修改本机当前用户下.m2目录下的settings.xml

<?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">

  <mirrors>
    <mirror>
      <id>nexus</id>
      <mirrorOf>*</mirrorOf>     
      <url>http://192.168.0.110:8081/nexus/content/groups/public/</url>
    </mirror>     
  </mirrors>  
  
  <profiles>
    <profile>
      <id>nexus</id>
      <repositories>
        <repository>
          <id>nexus</id>
          <name>Nexus</name>
          <url>http://192.168.0.110:8081/nexus/content/groups/public/</url>
          <releases><enabled>true</enabled></releases>
          <snapshots><enabled>true</enabled></snapshots>
        </repository>
      </repositories>
      <pluginRepositories>
        <pluginRepository>
          <id>nexus</id>
            <name>Nexus</name>
            <url>http://192.168.0.110:8081/nexus/content/groups/public/</url>
            <releases><enabled>true</enabled></releases>
            <snapshots><enabled>true</enabled></snapshots>
        </pluginRepository>
      </pluginRepositories>
    </profile>
  </profiles>
  <activeProfiles>
    <activeProfile>nexus</activeProfile>
  </activeProfiles>

 </settings>

注:你们自行把这段xml中的地址换成nexus服务器的ip。

而后随便建立一个maven项目,执行mvn clean package 之类,观察下输出:

从上图能够看出,如今依赖项都是从nexus服务器下载的

 

4、eclipse中添加资源xml

最后再补充一个小技巧,这跟私服其实关系不大。每次咱们在eclipse里建立maven project时,都会弹出一个界面,让咱们选择“项目模板“,这个过程须要联网获取模板列表,并且每次都次重复获取,很不合理,以下图:

能够点击Confiure,会弹出如下界面,点击”Add Local Catalog“

先在浏览里访问 https://repository.jboss.org/nexus/content/groups/public-jboss/archetype-catalog.xml 这个地址,而后保存为xml文件,接下面的界面里 Catalog File里,选择这个xml文件,而后Description填写一个名字

而后在建立Maven项目时,就能够直接选择这个了,选择后,将直接读取本机xml的内容,比联网获取要快得多

相关文章
相关标签/搜索