此文介绍如何在Windows服务器上搭建Maven代理服务器Nexus。 按照步骤设置,到最后的结果是可以在 CMD 下面使用Maven 指令 mvn deploy,即可以将项目布置到代理服务器(Nexus)上。
其实搜下Nexus服务器搭建教程一大把,但发现好像对3.*以上的Nexus搭建在Windows的教程没有。要去看官方的文档或者教程。英文好的朋友可以直接去官方看,Nexus Repository Manager 3,主要看 Installation 节点与 Quick Start Guide - Proxying Maven and NPM。 看完几乎就能自己搭建出来了。
下面开始吧。
准备:1.系统JDK 1.8 ;
2.下载Windows 64位的Nexus(此链接版本:3.7.1);
step 1:解压下载的 Nexus,会得到两个文件夹如下。nexus是使用jetty运行的,先把服务跑起来。 
进入nexus-3.7.1-02/bin文件夹,在此处打开CMD( 点文件夹内空白处,Shift+ 右键),运行指令: “nexus /run ”。
如果最后看到显示 “Started Sonatype Nexus”,即表示启动成功了。启动不成功往上翻日志,大多情况是JRE不对或者 端口被占用。
注:1.此处其实启动了jetty,默认端口 8081。修改默认端口在 /sonatype-work/nexus3/etc/nexus.properties 文件里面
的 application-port。如下
检验上面nexus服务是否启动成功,打开 http://localhost:8081 (由于我的8081被占用,改为8055),如果能正常进去,即代表启动成功。页面即是nexus仓库页面。
step 2.配置代理仓库Nexus
这里假设你能打开 Nexus服务:localhost:8081。点击右上角
Sign in 登陆,然后点击“设置”(齿轮),左边导航栏选择:
Repository/Repositories 查看已有的仓库。如下
正常的话会有已经预设的仓库,其中:
maven-Central即是Maven的中央仓库 https://repo1.maven.org/maven2/ ;
maven-Releases 存放项目的发行版(稳定版);
maven-snapshots 存放项目开发版(即不稳定版);
maven-public 的type是个"group",即是个组。它是上面3个组合到一起,也就是以后去Public仓库取构件,即即可以在3个仓库中去搜索。
step 3.测试发布一个项目到Nexus上
3-1.maven 配置
<settings>
<mirrors>
<mirror>
<!--This
sends everything else to
/public -->
<id>nexus</id>
<mirrorOf>*</mirrorOf>
<url>http://localhost:8081/repository/maven-public/</url>
</mirror>
</mirrors>
<servers>
<server>
<id>nexus</id>
<username>admin</username>
<password>admin123</password>
</server>
</servers>
<profiles>
<profile>
<id>nexus</id>
<!--Enable
snapshots for the
built in central repo to direct -->
<!--all
requests to nexus via the mirror -->
<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>
<!--make
the profile active all the time -->
<activeProfile>nexus</activeProfile>
</activeProfiles>
</settings>
上面是maven的setting.xml文件,这个文件可能存在
1.${M2_HOME}/conf/settings.xml;
2. 用户的目录:${user.home}/.m2/settings
。
上面配置中,除了<mirror>中的<url>需要更改外,在不知其意义前提下,建议不要更改。
简单介绍下上面节点:
1.<mirror>节点是中url是刚刚配置的公共仓库"maven-public"的url。如果不知道,可以到这个“public"仓库中查看。其中<mirrorOf>设置为”*“代表所有对构件的请求都会通过这个mirror;
2.<profile>下面的<repository>好像是覆盖Super
Pom中的配置用的;
3.<activeProfiles>即总是启用对应
ID的<profile>;
4.
<services> 是配置项目中部署到对应url时提供身份认证的。上面例子中即布置到“maven-releases" or
"maven-snapshot"
两个仓库时的登陆名跟密码。如果你没有改nexus的密码,即lfyw
3-2.测试项目Pom配置
在项目的pom.xml中加入如下节点:
<project>
...
<distributionManagement>
<repository>
<id>nexus</id>
<name>Releases</name>
<url>http://localhost:8081/repository/maven-releases</url>
</repository>
<snapshotRepository>
<id>nexus</id>
<name>Snapshot</name>
<url>http://localhost:8081/repository/maven-snapshots</url>
</snapshotRepository>
</distributionManagement>
这里的仓库url 变成对应nexus中"maven-releases" 与 "maven-snapshots"
仓库对应地址。如果你没有更新nexus配置,
这里copy到对应的项目pom.xml中即可。
接下来是测试:在对应的maven 运行指令 mvn deploy。如果你的是snapshot版本,到nexus
对应的地方查看。
以前一直在CSDN看东西,这是我第一次在CSDN写教程,页面排版、详细程度可能都有很大问题。望各位轻点骂 。 -_-||