一.参数配置
如果你想配置 nexus 的应用在本地启动的 JVM参数,可以在 nexus.vmoptions:
如果你想改变 nexus 的 端口号,可以在 nexus-default.properties:
二.仓库配置
创建 Blob Strores
在创建 repository之前,还是需要先设定一个指定的文件存储目录,便于统一管理。在创建 repository之前,还是需要先设定一个指定的文件存储目录,便于统一管理。
前篇文章也提到,nexus3 使用了Blob Stores来保存仓库文件。因此创建仓库前,可以先创建 Blob Stores。当然你也可以不创建,那么将会使用默认的路径:./sonatype-work/nexus3/blobs
创建 Proxy Repository 代理仓库
配置 Proxy Repository代理仓库。创建页面出来之后,页面上有三个框是必填的:
注意:可以多建几个proxy repository,以便于可以加入更多的远程maven仓库到我们的 group 仓库中。唯一一点,就是看加入的proxy资源库使用的是国内的还是国外的远程仓库。加入group资源库的顺序应是:hosted仓库 > 国内proxy仓库 > 国外proxy仓库。
这里推荐几个远程仓库:
- jboss的maven中央仓库地址:http://repository.jboss.com/maven2/
- 阿里云的maven中央仓库地址:http://maven.aliyun.com/nexus/content/groups/public/
- apache的maven中央仓库地址:http://repo.maven.apache.org/maven2/
创建 hosted repository 宿主仓库
Hosted有三种方式:Releases、Snapshot、Mixed
- Releases: 一般是已经发布的Jar包
- Snapshot: 未发布的版本
- Mixed:混合的
创建 group repository 仓库组
为什么要注意顺序??
官方文档中建议:
It is recommended practice to place hosted repositories higher in the list than proxy repositories. For proxy repositories, the repository manager needs to check the remote repository which will incur more overhead than a hosted repository lookup.
希望将hosted repositories【宿主仓库】的顺序放在proxy repositories【代理仓库】之前,因为一个group【仓库组】中可以涵括这些宿主仓库和代理仓库。而一整个的group是作为一个public,一个接口给别人使用的。
所以当查找架包的时候,如果代理资源库在前面,那就是先从远程去查找jar,而不是先从宿主仓库(本地仓库)去查找是否有jar。
三.让 Maven 项目使用 Nexus 私服
下载Jar:设置 Nexus 为镜像地址
Maven 下的setting.xml文件和你自己项目中的pom.xml文件的关系:
- settting.xml 文件就是本地电脑上的全局变量;而pom.xml文件就是局部变量。
- pom.xml文件对于项目来说,是优先使用的。然而pom.xml文件中如果没有指定jar要从哪里下载的话,也就是说没有配置镜像地址的话,当然只能去按照settting.xml 中定义的地址去找了
拷贝的这个地址,就是下面即将在setting.xml文件中添加时使用的:
setting.xml:
<!--自定义maven本地仓库地址-->
<localRepository>D:\apps\repository</localRepository>
<!--nexus服务器-->
<servers>
<server>
<id>nexus</id>
<username>admin</username>
<password>admin123</password>
</server>
</servers>
<!--仓库组的url地址 id和name自定义,mirrorOf的值设置为central,写死的-->
<mirrors>
<mirror>
<id>nexus</id>
<name>nexus repository</name>
<url>http://localhost:8081/repository/myself_group/</url>
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>
部署上传Jar:配置distributionManagement
配置 distributionManagement 节点,执行 deploy 命令时,会将jar发布到指定地址。
注意:配置
pom.xml:
<project>
...
<distributionManagement>
<repository>
<id>nexus-releases</id>
<name>Nexus Release Repository</name>
<url>http://localhost:8081/nexus/content/repositories/releases/</url>
</repository>
<snapshotRepository>
<id>nexus-snapshots</id>
<name>Nexus Snapshot Repository</name>
<url>http://localhost:8081/nexus/content/repositories/snapshots/</url>
</snapshotRepository>
</distributionManagement>
...
</project>
settings.xml:
<settings>
...
<servers>
<server>
<id>nexus-releases</id>
<username>admin</username>
<password>admin123</password>
</server>
<server>
<id>nexus-snapshots</id>
<username>admin</username>
<password>admin123</password>
</server>
</servers>
...
</settings>
两个文件的 id 需要保持一致!