WINDOWS中NEXUS的安装使用【ATCO整理】

来源:互联网 发布:单片机uart是什么意思 编辑:程序博客网 时间:2024/05/01 11:31

一、介绍

Nexus是一个强大的Maven仓库管理器,它极大地简化了自己内部仓库的维护和外部仓库的访问。利用Nexus你可以只在一个地方就能够完全控制访问 和部署在你所维护仓库中的每个Artifact。Nexus是一套“开箱即用”的系统不需要数据库,它使用文件系统加Lucene来组织数据。Nexus 使用ExtJS来开发界面,利用Restlet来提供完整的REST APIs,通过m2eclipse与Eclipse集成使用。Nexus支持WebDAV与LDAP安全身份认证。

Nexus的下载地址是:http://www.sonatype.org/nexus/archived/,WINDOWS下载的是ZIP包,如:nexus-2.11.0-02-bundle.zip(文件名带有bundle表示该文件集成了web服务器,也就不需要再部署到其他中间件上面了)。

到官网上将ZIP的压缩包下载下来解压之后发现有两个文件夹,一个是nexus-2.11.0-02,另一个是sonatype-work;第一个文件夹包含了Nexus运行所需要的文件,是运行Nexus必须的;第二个文件夹目录包含Nexus生成的配置文件、日志文件、仓库文件等,当需要备份Nexus的时候,默认备份的是此目录文件。

二、设置环境变量

找到nexus目录下nexus-2.11.0-02中的bin文件夹,将至bin的目录路径加入到操作系统的path变量中。


三、启动

在nexus-2.11.0-02中的bin文件夹的jsw文件夹中,按操作系统放置了一些可执行脚本,可按情况双击运行,也可在命令行进行。

打开cmd命令行,输入nexus命令,会看到如下提示:start/stop/restart/install/uninstall,只要在nexus命令后面加这些命令中的其中之一即可,如下图所示,先安装,然后启动,成功之后可以打开浏览器:http://localhost:8081/nexus/#welcome,看到欢迎界面。
单击右上角的“Log In”按钮进行登录,Nexus的默认管理员账户密码为admin/admin123。

四、配置Nexus

登录进去之后,可以看到有repositories,点击,发现Releases和Snapshots仓库,其中Releases表示内部的模块中Release模块的发布仓库,Snapshots表示发布内部的Snapshot模块的仓库。3rd party表示第三方依赖的仓库,这三种都是hosted类型的仓库,其中Proxy类型的仓库表示从远程中央仓库中寻找数据的仓库。

我们可以从上图中看到四种仓库类型:group(仓库组)、hosted(宿主)、proxy(代理)和virtual(虚拟)。其中每种类型的Format有Maven1或者Maven2,这里我们不看Maven1的。仓库的Policy(策略)表示该仓库为发布(Release)版本还是快照(Snapshot)版本仓库。

在图5中,Nexus列出了默认的几个仓库:

Public Repositories:仓库组,将所有策略为Release的仓库聚合并通过一致的地址提供服务。

3rd party:一个策略为Release的宿主类型仓库,用来部署无法从公共仓库获得的第三方发布版本构件。

Apache Snapshots:策略为Snapshots的代理仓库,用来代理Apache Maven仓库的快照版本构件。

Central:该仓库代理Maven的中央仓库,策略为Release,只会下载和缓存中央仓库中的发布版本构件。

Central M1 shadow:maven1格式的虚拟类型仓库。

Codehaus Snapshots:代理Codehaus Maven仓库快照版本的代理仓库。

Release:策略为Release的宿主类型仓库,用来部署组织内部的发布版本构件。

Snapshots:策略为Snapshots的宿主类型仓库,用来部署组织内部的快照版本构件。


五、下载索引

点击左侧的Views/Repositories->下面出现Repositories->点击右边的Central->点击下面的Configuration

将Download Remote Indexes值改为true,点击“save”后,点击左边的“Administration”->"Scheduled Tasks"链接,如果没有出现“Update Repositories Index”处于Running状态,那么需要在Public Repositories行右击,点击"Update Index"。

然后再点击Schedule Tasks就可以看到有任务处于Running状态了。

等到索引下载完成之后,就可以在"Repositories"界面中,选择Browser Index选项卡,可以看到Maven中央仓库内容的树形结构,如下图所示。

在左边的搜索框中输入Spring关键字,然后搜索,会出现一大堆与Spring相关的结果。

六、私有仓库配置
在MAVEN目录下的Settings.xml中配置远程仓库,Maven的Settings.xml中提供的profile是一组可选的配置,可以用来设置或者覆盖配置默认值,在Setting.xml文件中加入以下代码,

<profiles><profile><id>local_nexus</id><repositories><repository><id>local_nexus</id><name>local_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>local_nexus</id><name>local_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>local_nexus</activeProfile></activeProfiles>

上面的配置中,使用了一个id为local_nexus的profile,这个profile包含了相关的仓库配置,同时配置中又使用了activeProfiles元素将nexus这个profile激活,这样当执行Maven构建的时候,激活的profile会将仓库配置应用到项目中去。

通过上面的配置,我们会发现Maven除了从Nexus下载构件外还会从中央仓库下载构件。既然是私服,那么我们就只希望Maven下载请求都仅仅通过Nexus。我们可以通过镜像实现这一需求。可以创建一个匹配任何仓库的镜像,镜像的地址是私服,这样Maven对任何仓库的构件下载请求都会转到私服中。把上面的配置修改为如下配置:

<profiles><profile><id>local_nexus</id><repositories><repository><id>local_nexus</id><name>local_nexus</name><url>http://localhost:8081/nexus/content/groups/public/</url><releases><enabled>true</enabled></releases><snapshots><enabled>true</enabled></snapshots></repository><repository><id>central</id><url>http://repo.maven.apache.org/maven2</url><releases><enabled>true</enabled></releases><snapshots><enabled>true</enabled></snapshots></repository></repositories><pluginRepositories><pluginRepository><id>local_nexus</id><name>local_nexus</name><url>http://localhost:8081/nexus/content/groups/public/</url><releases><enabled>true</enabled></releases><snapshots><enabled>true</enabled></snapshots></pluginRepository><pluginRepository><id>central</id><url>http://repo.maven.apache.org/maven2</url><releases><enabled>true</enabled></releases><snapshots><enabled>true</enabled></snapshots></pluginRepository></pluginRepositories></profile></profiles><activeProfiles><activeProfile>local_nexus</activeProfile></activeProfiles>

七、部署构件到私服

我们在实际开发过程是多个人的,那么总有一些公共模块或者说第三方构件是无法从Maven中央库下载的。我们需要将这些构件部署到私服上,供其他开发人员下载。用户可以配置Maven自动部署构件至Nexus的宿主仓库,也可以通过界面手动上传构件。

使用Maven部署构件到Nexus私服上

日常开发的快照版本部署到Nexus中策略为Snapshot的宿主仓库中,正式项目部署到策略为Release的宿主仓库中,POM的配置方式如下:

<distributionManagement><repository><id>local_nexus_releases</id><name>core Release Repository</name><url>http://localhost:8081/nexus/content/repositories/releases/</url></repository><snapshotRepository><id>local_nexus_snapshots</id><name>core Snapshots Repository</name><url>http://localhost:8081/nexus/content/repositories/snapshots/</url></snapshotRepository></distributionManagement>

Nexus的仓库对于匿名用户只是只读的。为了能够部署构件,我们还需要再settings.xml中配置验证信息:

<pre name="code" class="plain"><servers><server><id>local_nexus_releases</id><username>admin</username><password>admin123</password></server><server><id>local_nexus_snapshots</id><username>admin</username><password>admin123</password></server></servers>

其中,验证信息中service的id应该与POM中repository的id一致。

在Nexus界面上手动部署第三方构件至私服

我们除了自己的构件要部署到Nexus私服上外,我们有可能还要将第三方构件(如:SQLService的JDBC)部署到Nexus上。这个时候,在Nexus界面上选择一个宿主仓库(如3rd party),再在页面下方选择Artifact Upload选项卡。填写对应的Maven坐标。然后点击“Select Artifact(s) for Upload”按钮从本机选择要上传的构件,然后点击“Add Artifact”按钮将其加入到上传列表中。最后,单击页面底部的“Upload Artifact(s)”按钮将构件上传到仓库中。


0 0
原创粉丝点击