地址:http://blog.csdn.NET/tounaobun/article/details/8959682html
咱们知道,Maven仓库包括本地仓库和远程仓库,而远程仓库又包括中央仓库,私服,Jboss仓库和Java.net仓库等。java
私服,即私有的仓库服务器,它不只能够下降中央仓库负荷,节省外网带宽,还能够加速Maven构建,本身部署构件等,从而高效地使用Maven。对于私服,咱们可使用仓库管理软件来建立本身的私有仓库服务器。目前网上流行的仓库管理软件有Apache的Achiva,JFrog的Artifactory和Sonatype的Nexus。因为当前Nexus是最流行的仓库管理软件,所以,咱们这里采用Nexus。windows
Nexus的安装很简单,只需从http://nexus.sonatype.org/downloads下载最新的版本便可。须要注意的是官网向咱们提供了两种安装方式,一种是自带Web容器(Jetty)的Bundle包,另一种是war包。(能够直接部署到Tomcat等Web容器中)浏览器
假设下载的是Bundle包,解压到指定目录后(笔者的目录是E:\sonatype-nexus),进入目录E:\sonatype-nexus\nexus-2.4.0-09-bundle\nexus-2.4.0-09\bin\jsw\windows-x86-32\,找到脚本文件console-nexus.bat,打开并运行。若是没有出现错误,打开浏览器输入http://localhost:8081/nexus/index.html#welcome,能够看到nexus的欢迎界面:安全
默认状况下是匿名登陆该网站,只能享受浏览,搜索等功能。若是想拥有更多控制能力,能够点击右上角的Login in进行登陆,Nexus内置了一个管理员帐户,用户名是admin,密码是admin123。服务器
下面开始建立本身的宿主仓库,目的是为了将本身编写的构件部署到该仓库中,供组织其它成员下载。app
用管理员帐户登陆以后,在左边导航栏框中找到Repositories,点击后,在右框中选择Add按钮,弹出的菜单中选择Hosted Repository,表示添加宿主仓库。maven
在新建宿主仓库表中,填写仓库的ID,Name以及仓库策略(release表示发布版构件,snapshot表示快照版构件),好比:网站
点击save按钮,完成策略为Release的宿主仓库建立。ui
同理,建立策略为Snapshot的宿主仓库。
至此,咱们的宿主仓库(包括发布版和快照版)就完成了。
接下来,须要添加权限(Privileges)
在左边导航栏的Security中找到Privileges,点击后,在右框中点击Add按钮,选择Repository Target Privilleges
按如图所示添加发布版和快照版。
接下来建立具备相应权限的角色(Role)
最后建立用户。
这里须要注意的是,User ID就是远程仓库的username,password就是远程仓库的password。
至此,宿主仓库就算完成了。
由于远程仓库须要安全认证,因此须要在settings.xml中进行配置。
打开settings.xml,在settings根标签下添加以下内容:
[html] view plaincopy
<server> <id>pro-snapshot</id> <username>foo</username> <password>123456</password> </server> <server> <id>pro-release</id> <username>foo</username> <password>123456</password> </server>
这里的用户名和密码对应以前建立的user的userId和password。即foo和123456。另外,id元素对应pom.xml中distributionManagement里的id元素。以后会给出相应的代码。
安全认证配置好后,接下来打开你要部署的maven项目的pom.xml,在project元素下添加以下内容:
[html] view plaincopy
<distributionManagement> <repository> <id>pro-release</id> <name>Proj Release Repository</name> <url>http://localhost:8081/nexus/content/repositories/RestBus-Releases</url> </repository> <snapshotRepository> <id>pro-snapshot</id> <name>Proj Snapshot Repository</name> <url>http://localhost:8081/nexus/content/repositories/RestBus-Snapshots</url> </snapshotRepository> </distributionManagement>
这里在distributionManagement元素下添加了发布版仓库和快照版仓库,它们的id对应以前在settings.xml中server的id。url为仓库地址,该url能够在nexus的repositories中找到:
最后一步,打开命令提示符窗口,定位到maven根目录下(和pom.xml同目录),执行maven命令:
mvn clean deploy
若是配置没有错误,会出现如下相似打印信息:
[plain] view plaincopy
[INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building bus-location 1.0 [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-clean-plugin:2.4.1:clean (default-clean) @ bus-location --- [INFO] Deleting G:\bus-location\target [INFO] [INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ bus-location --- [debug] execute contextualize [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] skip non existing resourceDirectory G:\bus-location\src\main\resources [INFO] [INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ bus-location --- [INFO] Compiling 1 source file to G:\bus-location\target\classes [INFO] [INFO] --- maven-resources-plugin:2.5:testResources (default-testResources) @ bus-location --- [debug] execute contextualize [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] skip non existing resourceDirectory G:\bus-location\src\test\resources [INFO] [INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ bus-location --- [INFO] Compiling 1 source file to G:\bus-location\target\test-classes [INFO] [INFO] --- maven-surefire-plugin:2.10:test (default-test) @ bus-location --- [INFO] Surefire report directory: G:\bus-location\target\surefire-reports ------------------------------------------------------- T E S T S ------------------------------------------------------- Running zjut.edu.soft.bus.AppTest Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.016 sec Results : Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] --- maven-jar-plugin:2.3.2:jar (default-jar) @ bus-location --- [INFO] Building jar: G:\bus-location\target\bus-location-1.0.jar [INFO] [INFO] --- maven-install-plugin:2.3.1:install (default-install) @ bus-location --- [INFO] Installing G:\bus-location\target\bus-location-1.0.jar to C:\Users\tsw\.m2\repository\zjut\edu\soft\bus\bus-location\1.0\bus-location-1.0.jar [INFO] Installing G:\bus-location\pom.xml to C:\Users\tsw\.m2\repository\zjut\edu\soft\bus\bus-location\1.0\bus-location-1.0.pom [INFO] [INFO] --- maven-deploy-plugin:2.7:deploy (default-deploy) @ bus-location --- Uploading: http://localhost:8081/nexus/content/repositories/RestBus-Releases/zjut/edu/soft/bus/bus-location/1.0/bus-location-1.0.jar 2/3 KB 3/3 KB Uploaded: http://localhost:8081/nexus/content/repositories/RestBus-Releases/zjut/edu/soft/bus/bus-location/1.0/bus-location-1.0.jar (3 KB at 7.3 KB/sec) Uploading: http://localhost:8081/nexus/content/repositories/RestBus-Releases/zjut/edu/soft/bus/bus-location/1.0/bus-location-1.0.pom 2/2 KB Uploaded: http://localhost:8081/nexus/content/repositories/RestBus-Releases/zjut/edu/soft/bus/bus-location/1.0/bus-location-1.0.pom (2 KB at 10.1 KB/sec) Downloading: http://localhost:8081/nexus/content/repositories/RestBus-Releases/zjut/edu/soft/bus/bus-location/maven-metadata.xml Uploading: http://localhost:8081/nexus/content/repositories/RestBus-Releases/zjut/edu/soft/bus/bus-location/maven-metadata.xml 305/305 B Uploaded: http://localhost:8081/nexus/content/repositories/RestBus-Releases/zjut/edu/soft/bus/bus-location/maven-metadata.xml (305 B at 3.1 KB/sec) [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 4.762s [INFO] Finished at: Wed May 22 13:23:16 CST 2013 [INFO] Final Memory: 14M/33M [INFO] ------------------------------------------------------------------------
固然,也能够在网页上进行手工部署。
注:此处我遇到一个错误,就是nexus一直启动不了,wrapper.log报错:Error in WrapperListener.start callback. java.lang.StackOverflowError
主要缘由是个人D:\maven\nexus-2.6.4-02\bin\jsw\conf\warpper.conf必须指定1.7版本的jdk,1.8的会出这个错误
wrapper.java.command=C:\Program Files\Java\jdk1.7.0_17\bin\java.exe。
同时记得配置nexus的环境变量PATH=D:\maven\nexus-2.6.4-02\bin;
验证能够。