创建Nexus代理仓库
同样的Add操作选择Proxy Repository,就会看到如下配置界面:
对于代理仓库来说,最重要的是远程仓库的地址,即Remote Storage Location,用户必须在这里输入有效的值。例如中央仓库地址为:https://repo1.maven.org/maven2/。
Download Remote Indexes表示是否下载远程索引,Checksum Policy配置校验和出错时的策略,用户可以选择忽略,记录警告或者拒绝下载,默认选warn。
当远程仓库需要认证的时候,这里的Authentication配置就能派上用场了。
Artifact Max Age默认值为-1,表示构件缓存后就一直保存着,不再重新下载。对于快照版仓库来说,Artifact Max Age默认为1440分钟,表示每隔一天重新缓存。
创建Nexus仓库组
要创建一个仓库组,一样的Add,选择Repository Group,就会看到下图所示配置项:
此图就不用过多解释了,需要注意的是:仓库组所包含的顺序决定了仓库组遍历其所含仓库的次序,因此最好将常用的仓库放到前面,当用户从仓库下载构件的时候,就能尽快访问到包含构件的仓库。
Nexus的索引与搜索构件
Nexus代理了那么多远程仓库,就需要搜索,以Maven中央仓库为例,我们设置索引为true,它就开始下载索引了,下载情况k可以scheduled tasks中看到下载情况,其状态为RUNNING,因为中央仓库有十几万构件,下载需要花费一定的时间,有了索引我们就可以,在左边栏就行搜索了,如下图所示:
该面板除了显示构件的坐标,还包含了一段XML声明,用户可以复制粘贴到项目的POM中。除了简单的关键字搜索,Nexus还提供了GAV搜索、类名搜索和校验和搜索等功能,用户可以单击搜索页面左上角的Advanced Search选择高级搜索功能:
- GAV搜索(GAV Search):允许用户通过设置GroupId、ArtifactId和Version等信息来进行更有针对性的搜索。
- 类名搜索(Classname Search):允许用户搜索包含某个java类的构件。
- 校验和搜索(Checksum Search):允许用户直接使用构件的校验和搜索该构件。
除了下载使用远程仓库的索引,我们也能为宿主仓库和代理仓库建立索引。只需要在仓库上右击,从弹出的快捷菜单中选择Repair index即可,如下图所示:
待索引编纂任务完成后,就能搜索该仓库所包含的构件。
对于宿主仓库来说,Repair Index任务会扫描该仓库所包含的所有构件建立索引。对于代理仓库来说,Repair Index任务会扫描所有缓存的构件建立索引,如果远程仓库也有索引,则下载后与本地的索引合并。对于仓库组来说,Repair Index任务会合并其包含的所有仓库的索引。
配置Maven从Nexus下载构件
首先看下直接配置pom文件的方式,这种方式只能支持一个索引,方式如下:
<project>
...
<repositories>
<repository>
<id>nexus</id>
<name>Nexus</name>
<url>http://localhost: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://localhost:8081/nexus/content/groups/public/</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled><snapshots>
</pluginRepository>
</pluginRepositories>
...
</project>
Maven还提供了Profile机制,能让用户将仓库配置放到settings.xml中的Profile中,这样,所有的项目就都能访问了,如下所示:
<settings>
...
<profiles>
<profile>
<id>nexus</id>
<repositories>
<repository>
<id>nexus</id>
<name>Nexus</name>
<url>http://localhost: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://localhost: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>
该配置中使用了一个id为nexus的profile,这个profile包含了相关的仓库配置,同时配置中又使用activeProfile元素将nexus这个profile激活,这样当执行Maven构件的时候,激活的profile会将仓库配置应用到项目中去。
上述配置已经能让本机所有的Maven项目从Nexus私服下载构件。细心的读者会发现,Maven除了从Nexus下载构件外,还会不时地访问中央仓库central,我们希望的是所有Maven下载请求都仅仅通过Nexus,以全面发挥私服的作用。这是时候就需要借助于之前提到的镜像配置了。可以创建一个匹配任何仓库的镜像,镜像的地址为私服,这样,Maven对任何仓库的构件下载请求都会转到私服中。具体配置如下所示:
<settings>
...
<mirrors>
<mirror>
<id>nexus</id>
<mirrorOf>*</mirrorOf>
<url>http://localhost:8081/nexus/content/groups/public</url>
<mirror>
</mirrors>
<profiles>
<profile>
<id>nexus</id>
<repositories>
<repository>
<id>central</id>
<url>http://central</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
<repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>central</id>
<url>http://central</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
<activeProfiles>
<activeProfile>nexus</activeProfile>
</activeProfiles>
...
</settings>
这里解释下仓库及仓库插件配置,它们的id都为central,也就是说覆盖了超级POM中央仓库的配置,它们的url无关紧要,因为所有的请求都会通过镜像访问私服地址。配置仓库及插件仓库的主要目的是开启对
快照版本下载的支持,当Maven需要下载发布版或快照版的时候,它首先检查central,看该类型的构件是否支持,得到正面的回答之后,在根据镜像匹配规则转而访问私服仓库地址。
部署构件至Nexuskey
如果只为代理外部仓库,那么Nexus的代理仓库就已经能够完全满足需要了。对于另一类Nexus仓库——宿主仓库来说,他们的作用是存储组织内部的,或者一些无法从公共仓库获得的第三方构件,供大家下载使用。用户可以配置Maven自动部署构件至Nexus的宿主仓库,也可以通过界面手动上传构件。
使用Maven部署构件至Nexus
日常开发生成的快照版本可以直接部署到Nexus中策略为Snapshot的宿主仓库中,项目正式发布的构件则应该部署到Nexus中策略为Release的宿主仓库中。pom的配置方式如下:
<project>
...
<distributionManagement>
<repository>
<id>releases</id>
<name>Releases</name>
<url>http://localhost:8081/nexus/content/repositories/releases/</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<name>Snapshots</name>
<url>http://localhost:8081/nexus/content/repositories/snapshots/</url>
</snapshotRepository>
</distributionManagement>
...
</project>
Nexus的仓库对于匿名用户是只读的。为了能够部署构件,还需要在settings.xml中配置认证信息,如下所示:
<settings>
...
<servers>
<server>
<id>releases</id>
<username>admin</username>
<password>*****</password>
</server>
<server>
<id>snapshots</id>
<username>admin</username>
<password>*****</password>
</server>
<servers>
...
</settings>
手动部署第三方构件至Nexus
某些Java Jar文件(如Oracle)的JDBC驱动,由于许可证的因素,他们无法公开地放到公共仓库中。用户可以下载这些构件然后通过Nexus的界面上传到私服中。方法如下:
首先选择一个宿主仓库如:3rd party,然后在页面下方选择Artifact Upload选项卡。在上传构件的时候,,Nexus要求用户确定其Maven坐标,如果该构件是通过Maven构建的,那么可以在GAV Definition下拉列表选择From POM,否则就选GAV Parameters。用户需要为该构件定义一个Maven坐标。
然后单击Select Artifact(s) to Upload按钮从本机选择要上传的构件,然后单击Add Aritfac按钮将其加入到上传列表中。Nexus允许用户一次上传一个主构件和多个附属构件(即Classifier)。最后,单击页面最下方的Upload Artifact(s)按钮将构将上传到仓库中。
还有一些东西,再开第三篇文章写。