根据上一章节,我们知道了pom.xml文件中维护着项目对jar的依赖,知道了GAV坐标可以唯一标识一个jar。
比如项目中需要junit.jar,那么我们去哪里找这些jar,这些jar对应的GAV呢?
答案很明确,我们应该去Maven的中央仓库,即http://mvnrepository.com/。这里存放着大量的开放的jar以供我们依赖到项目中。
如果项目需要依赖的jar是我们自己编译的,不在中央仓库中,那要怎么办呢?
答案依然很明确,我们应该使用Nexus,使用它来帮着我们管理中央仓库和第三方的jar。
Nexus
nexus即私服,个人或者公司管理项目以及jar包的一种私有服务器;安装过程非常简单;
首先,在http://www.sonatype.org/nexus/下载免费版的文件,可以是zip包,可以是gz包;以zip包举例:
1、解压开nexus,进入到bin\jsw\conf,打开wrapper.conf;修改wrapper.java.command
将其指向本机中java.exe所在的位置,如wrapper.java.command=C:\Program Files\Java\jdk1.8.0_101\bin\java.exe
2、将${nexus}\bin加入到环境变量中
以管理员打开cmd,输入nexus,有如下提示,即配置成功
3、为了方便于使用,这里将nexus加入到系统服务中,命令如下:
nexus install———->将nexus加入到服务中
nexus start ———–>启动nexus
判断nexus启动成功的标识:
在浏览器上键入localhost:8081/nexus(以本机为例,nexus默认端口号8081)
以上,nexus配置完成
Nexus与Maven的联合使用
一般,常将nexus作为maven的仓库,将项目中要用到的jar上传到nexus,交给nexus管理;而不是每次maven依赖jar的时候,都去中央仓库下载。
一般的jar的依赖过程是先去nexus中去找,若查找不到再去中央仓库中去寻找
常用的仓库类型:
一般常用的是hosted、proxy。hosted宿主仓库,用来发布一些第三方不允许的组件,如自己封装的jar;proxy,即代理仓库,常指代理远程中央仓库。group组仓库,可以包括多种类型的仓库,常用于开发使用。如上图所示Public Repositories
这里,组仓库就配置了如下的四个仓库:
上图的3rd party,常指我们发布自己jar的地方
为pom.xml文件指定仓库:
<respositories>
<respository>
<release>
<enabled>true</enabled>----------------->允许下载release类型的jar
</release>
<snapshots>
<enabled>true</enabled>
</snapshots>
<id>user respository</id>----->仓库id
<name>Public Repositories</name>
<url>http://http://localhost:8081/nexus/content/groups/public/</url>------>仓库地址
</respository>
</respositories>
在settings.xml中配置指定的仓库:
<profile>
<id>nexusProfile</id>
<repositories>
<repository>
<id>nexusProfile</id>
<name>Nexus Responsitory</name>
<url>http://localhost:8081/nexus/content/groups/public/</url>
<releases><enabeld>true</enabeld></releases>
<snapshots><enabled>true</enabled></snapshots>
</repository>
</repositories>
</profile>
配置了仓库之后,要激活仓库:
<activeProfiles>
<activeProfile>nexusProfile</activeProfile>---------------->与仓库的id一致
</activeProfiles>
若需上传第三方的jar到nexus中,选中3rd respository:
输入GroupId、artifactId、version
添加本地jar,select之后,点击Add Artifact,随后Upload Artifact
发布项目
将项目发布到nexus中,常是这样对应的:snapshots对应snapshots respository仓库,release对应release respository仓库
需要在pom.xml文件中配置:
<!-- 发布项目到nexus -->
<distributionManagement>
<repository>
<id>user release</id>
<name>User Projetc Release</name> <url>http://localhost:8081/nexus/content/repositories/releases/</url>
</repository>
<snapshotRepository>
<id>user snapshots</id>
<name>User Project SNAPSHOTS</name> <url>http://localhost:8081/nexus/content/repositories/snapshots/</url>
</snapshotRepository>
</distributionManagement>
随后,运行clean deploy成功之后,即可在snapshots下看到该项目(本项目是snapshots版本的)
为了能够正常的发布,常需要配置发布版本的角色:
nexus中默认的发布版本的用户是deployment,密码deployment123
因此在settings.xml中配置如下:
<server>
<id>user release</id>
<username>deployment</username>
<password>deployment123</password>
</server>
<server>
<id>user snapshots</id>
<username>deployment</username>
<password>deployment123</password>
</server>
注意ID是与pom.xml中发布的id相互对应的