我们知道,Maven仓库包括本地仓库和远程仓库,而远程仓库又包括中央仓库,私服,Jboss仓库和Java.net仓库等。
私服,即私有的仓库服务器,它不仅可以降低中央仓库负荷,节省外网带 宽,还可以加速Maven构建,自己部署构件等,从而高效地使用Maven。对于私服,我们可以使用仓库管理软件来创建自己的私有仓库服务器。目前网上流 行的仓库管理软件有Apache的Achiva,JFrog的Artifactory和Sonatype的Nexus。由于当前Nexus是最流行的仓库 管理软件,因此,我们这里采用Nexus。
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。
下面开始创建自己的宿主仓库,目的是为了将自己编写的构件部署到该仓库中,供组织其它成员下载。
用管理员账户登录之后,在左边导航栏框中找到Repositories,点击后,在右框中选择Add按钮,弹出的菜单中选择Hosted Repository,表示添加宿主仓库。
在新建宿主仓库表中,填写仓库的ID,Name以及仓库策略(release表示发布版构件,snapshot表示快照版构件),比如:
点击save按钮,完成策略为Release的宿主仓库创建。
同理,创建策略为Snapshot的宿主仓库。
至此,我们的宿主仓库(包括发布版和快照版)就完成了。
接下来,需要添加权限(Privileges)
在左边导航栏的Security中找到Privileges,点击后,在右框中点击Add按钮,选择Repository Target Privilleges
按如图所示添加发布版和快照版。
接下来创建具有相应权限的角色(Role)
最后创建用户。
这里需要注意的是,User ID就是远程仓库的username,password就是远程仓库的password。
至此,宿主仓库就算完成了。
因为远程仓库需要安全认证,所以需要在settings.xml中进行配置。
打开settings.xml,在settings根标签下添加如下内容:
1. <server>
2. <id>pro-snapshot</id>
3. <username>foo</username>
4. <password>123456</password>
5. </server>
6.
7. <server>
8. <id>pro-release</id>
9. <username>foo</username>
10. <password>123456</password>
11. </server>
这里的用户名和密码对应之前创建的user的userId和password。即foo和123456。另外,id元素对应pom.xml中distributionManagement里的id元素。之后会给出相应的代码。
安全认证配置好后,接下来打开你要部署的maven项目的pom.xml,在project元素下添加如下内容:
1. <distributionManagement>
2. <repository>
3. <id>pro-release</id>
4. <name>Proj Release Repository</name>
5. <url>http://localhost:8081/nexus/content/repositories/RestBus-Releases</url>
6. </repository>
7.
8. <snapshotRepository>
9. <id>pro-snapshot</id>
10. <name>Proj Snapshot Repository</name>
11. <url>http://localhost:8081/nexus/content/repositories/RestBus-Snapshots</url>
12. </snapshotRepository>
13. </distributionManagement>
这里在distributionManagement元素下添加了发布版仓库和快照版仓库,它们的id对应之前在settings.xml中server的id。url为仓库地址,该url可以在nexus的repositories中找到:
最后一步,打开命令提示符窗口,定位到maven根目录下(和pom.xml同目录),执行maven命令:
mvn clean deploy
如果配置没有错误,会出现以下类似打印信息:
1. [INFO] Scanning for projects...
2. [INFO]
3. [INFO] ------------------------------------------------------------------------
4. [INFO] Building bus-location 1.0
5. [INFO] ------------------------------------------------------------------------
6. [INFO]
7. [INFO] --- maven-clean-plugin:2.4.1:clean (default-clean) @ bus-location ---
8. [INFO] Deleting G:\bus-location\target
9. [INFO]
10. [INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ bus-location ---
11. [debug] execute contextualize
12. [INFO] Using 'UTF-8' encoding to copy filtered resources.
13. [INFO] skip non existing resourceDirectory G:\bus-location\src\main\resources
14. [INFO]
15. [INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ bus-location ---
16. [INFO] Compiling 1 source file to G:\bus-location\target\classes
17. [INFO]
18. [INFO] --- maven-resources-plugin:2.5:testResources (default-testResources) @ bus-location ---
19. [debug] execute contextualize
20. [INFO] Using 'UTF-8' encoding to copy filtered resources.
21. [INFO] skip non existing resourceDirectory G:\bus-location\src\test\resources
22. [INFO]
23. [INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ bus-location ---
24. [INFO] Compiling 1 source file to G:\bus-location\target\test-classes
25. [INFO]
26. [INFO] --- maven-surefire-plugin:2.10:test (default-test) @ bus-location ---
27. [INFO] Surefire report directory: G:\bus-location\target\surefire-reports
28.
29. -------------------------------------------------------
30. T E S T S
31. -------------------------------------------------------
32. Running zjut.edu.soft.bus.AppTest
33. Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.016 sec
34.
35. Results :
36.
37. Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
38.
39. [INFO]
40. [INFO] --- maven-jar-plugin:2.3.2:jar (default-jar) @ bus-location ---
41. [INFO] Building jar: G:\bus-location\target\bus-location-1.0.jar
42. [INFO]
43. [INFO] --- maven-install-plugin:2.3.1:install (default-install) @ bus-location ---
44. [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
45. [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
46. [INFO]
47. [INFO] --- maven-deploy-plugin:2.7:deploy (default-deploy) @ bus-location ---
48. Uploading: http://localhost:8081/nexus/content/repositories/RestBus-Releases/zjut/edu/soft/bus/bus-location/1.0/bus-location-1.0.jar
49. 2/3 KB
50. 3/3 KB
51.
52. 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)
53. Uploading: http://localhost:8081/nexus/content/repositories/RestBus-Releases/zjut/edu/soft/bus/bus-location/1.0/bus-location-1.0.pom
54. 2/2 KB
55.
56. 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)
57. Downloading: http://localhost:8081/nexus/content/repositories/RestBus-Releases/zjut/edu/soft/bus/bus-location/maven-metadata.xml
58.
59. Uploading: http://localhost:8081/nexus/content/repositories/RestBus-Releases/zjut/edu/soft/bus/bus-location/maven-metadata.xml
60. 305/305 B
61.
62. 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)
63. [INFO] ------------------------------------------------------------------------
64. [INFO] BUILD SUCCESS
65. [INFO] ------------------------------------------------------------------------
66. [INFO] Total time: 4.762s
67. [INFO] Finished at: Wed May 22 13:23:16 CST 2013
68. [INFO] Final Memory: 14M/33M
69. [INFO] ------------------------------------------------------------------------
进入http://localhost:8081/nexus网页,在RestBus-Releases(因为在pom.xml中,version是1.0,不是快照版)中,可以找到刚刚部署上去的构件bus-location-1.0.jar
当然,也可以在网页上进行手工部署。
标签:INFO,---,仓库,bus,Maven,nexus,location,构件,远程 From: https://blog.51cto.com/u_2650279/6424060