【Maven实战】仓库介绍和Nexus的安装

在Maven中我们之前进行配置一个项目的依赖时,引用一下jar包,这些jar包第一次都会从一个地方进行下载,这个地方称为仓库,而对于仓库一般有本地仓库和中心仓库之分,但是我们一般在做项目时会在自己的服务器上配置一个私有仓库,那么我们下面就来创建一个私有仓库,这里我们使用的一个工具叫做Nexus。

1、首先到http://www.sonatype.org/nexus/下载对应的nexus安装包:nexus-latest-bundle.zip

2、解压缩得到的压缩包,然后将此压缩包中的bin目录(这里为:D:\docs\nexus-latest-bundle\nexus-2.5.1-01\bin)配置到path环境变量中:

3、接着打开cmd,输入nexus:

这边会让我们执行start、stop、install、uninstall等命令,我们首先执行install命令:

此时就会提示我们安装成功了,并且会在系统服务中生成nexus的服务:

此时我们再修改D:\docs\nexus-latest-bundle\nexus-2.5.1-01\bin\jsw\conf,下的wrapper.conf文件,如下:

1 # Set the JVM executable 
2 # (modify this to absolute path if you need a Java that is not on the OS path)
3 wrapper.java.command=java

 

其中的wrapper.java.command=java修改为本机安装的JDK路径:

1 # Set the JVM executable 
2 # (modify this to absolute path if you need a Java that is not on the OS path)
3 wrapper.java.command=C:\Program Files\Java\jdk1.7.0_17\bin\java

 

然后执行nexus start命令:

4、打开浏览器,输入:http://localhost:8081/nexus就能进行访问了:

 5、点击右上角的login进行登陆:用户名:admin,密码:admin123,登陆完成以后就会有很多的命令可以供我们进行操作了

 

 这里我们注意观察type属性中的内容:

  • hosted:表示是内部项目发布仓库

  • proxy仓库:从远程中央仓库中寻找数据的仓库,代理仓库。

  • group仓库:组仓库,方便开发人员用来访问的仓库,这里面包含了其他的所有仓库,这样我们引用仓库时就非常方便了

6、仓库建立完成之后,我们就可以设置项目引用的仓库了,这里我们在user-core项目中引用我们的group仓库:

1 <repositories>
2      <repository>
3         <id>group</id>
4         <name>group repository</name>
5         <url>http://localhost:8081/nexus/content/groups/public/</url>    //这里的url我们可以到nexus的控制面板中找到
6         <releases><enabled>true</enabled></releases>
7         <snapshots><enabled>true</enabled></snapshots>
8      </repository>
9   </repositories>

 

配置完成以后,我们user-aggregation项目中随便的加入一个jar包的依赖,这里加入的是commons-io:

1 <dependency>
2             <groupId>commons-io</groupId>
3             <artifactId>commons-io</artifactId>
4             <version>2.4</version>
5         </dependency>

 

这样我们可以看到默认的就会到我们自己的本地仓库进行下载了:

有的时候可能不会出现以上的这种情况,程序可能还会自动的从中央仓库进行下载,这时我们如果配置其不让从中央仓库下载的话,我们可以找到maven中的settings文件,然后找到其中的mirrors节点,为我们的中央仓库配置一下镜像:

 1  <mirrors>
 2     <!-- mirror
 3      | Specifies a repository mirror site to use instead of a given repository. The repository that
 4      | this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
 5      | for inheritance and direct lookup purposes, and must be unique across the set of mirrors.
 6      |
 7     <mirror>
 8       <id>mirrorId</id>
 9       <mirrorOf>repositoryId</mirrorOf>
10       <name>Human Readable Name for this Mirror.</name>
11       <url>http://my.repository.com/repo/path</url>
12     </mirror>
13      -->
14      <mirror>
15       <id>central</id>
16       <mirrorOf>central</mirrorOf>
17       <name>Human Readable Name for this Mirror.</name>
18       <url>http://localhost:8081/nexus/content/groups/public/</url>   //为中央仓库设置镜像url,这样当访问中央仓库时不是访问中央仓库的地址,而是我们配置的镜像地址,也就是我们自己服务器的地址
19     </mirror>
20   </mirrors>

 

这样当程序试图访问中央仓库时会自动的转到我们自己的仓库了。

posted @ 2013-09-21 21:30  悟空65  阅读(1071)  评论(0编辑  收藏  举报