可用两种方式安装启动nexus,下载在命令行启动,或者放到tomcat
下载并在命令行启动nexus
1.下载nexus(https://www.sonatype.com)
2.下载后解压文件,将解压后的nexus文件放在你自己想要的地方
3.配置环境变量(和配置java的环境变量一样)
4.安装和启动nexus
(一)下载并在tomcat启动安装Nexus
下载Nexus的war包(本文使用的是nexus-2.5.war),将该war包放在tomcat服务器的webapps目录下,重启tomcat。打开http://localhost:8080/nexus-2.5/,你将看到以下页面:
点击右上角的“Log In”,输入用户名admin,密码admin123(这是Nexus默认的),此后点击右侧的“Repositories”,显示当前Nexus所管理的Repository,默认情况下Nexus为我们创建了以下主要的Repository:
Public Repositories,这是一个Repository Group,它所对应的URL为http://localhost:8080/nexus-2.5/content/groups/public/,该Repository Group包含了多个Repository,其中包含了Releases、Snapshots、Third Party和Central。Repository Group的作用是我们只需要在自己的项目中配置该Repository Group就行了,它将自动从其所包含的Repository中下载依赖,比如如果我们声明对Spring的依赖,那么根据Repository Group中各个Repository的顺序(可以配置),Nexus将首先从Releases中下载Spring,发现没有,再从Snapshots中下载(极大可能也没有,因为它是个Snapshots的Repository),依次查找,最后可能在Central Repository中找到。在配置项目的Repository时,我们应该首先考虑Public Repositories。
3rd party,该Repository即是存放你公司所购买的第三方软件库的地方,它是一个由Nexus自己维护的一个Repository。
Apache Snapshots,看名字你就应该知道这是个什么样的Repository,这是一个代理Repository,即最终的依赖还是得在Apache官网上去下载,然后缓存在Nexus中。
Central,这就是代理Maven Central Repository的Repository。
Releases,你自己的项目要发布时,就应该发布在这个Repository,他也是Nexus自己维护的Repository,而不是代理。
Snapshots,你自己项目Snapshot的Repository。
(二)用Nexus Repository取代Maven Central Repository
在完成(一)之后,我们只是创建了一个专属的Nexus Repository,我们的项目默认还是使用的Maven Central Repository,所以这时我们需要将Maven Central Repository换成自己创建的Nexus Repository,可以通过修改~/.m2/settings.xml来达到这样的目的。在该settings.xml文件中(没有的话可以创建一个),加入以下配置:
复制代码
<!--This sends everything else to /public -->
<id>nexus</id>
<mirrorOf>*</mirrorOf>
<url>http://localhost:8080/nexus-2.5/content/groups/public</url>
<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>
nexus
复制代码以上配置通过mirror和profile的方式将central Repository全部转向到我们自己的Public Repositories,包括release版本和snapshot版本(Maven默认允许从Maven Central Repository下载release版本,这是合理的,如果你使用了别人的snapshot版本,一边你在使用,一边别人在开发,别人将API签名一换,哦豁)。这样一来,我们项目中的所有依赖都从Nexus的Public Repositories下载,由于其中包含了对Maven Central Repository的代理,所以此时Maven Central Repository中的类库也是可以间接下载到的。此时你可以将~/.m2/repository/中所有的内容删除掉,再在项目中执行“mvn clean install”,你将在终端中看到Maven已经开始从Nexus 的Public Repositories中下载依赖了,比如:
Downloading: http://localhost:8080/nexus-2.5/content/groups/public/org/codehaus/plexus/plexus-archiver/1.2/plexus-archiver-1.2.pom
请注意,此时我们的项目本身不需要做任何修改。我们只是创建了另一个Repository和修改了Maven的默认配置(学习完本文后,你应该需要将~/.m2/settings.xml还原,不然如果下次在构建之前自己的Nexus服务器没有启动,构建将失败。)。
(三)在项目中配置Nexus Repository的信息
接下来,我们开始着手如何将自己的项目部署到Nexus Repository中。这个也简单,第一我们需要在项目中指明部署目的Repository的URL,第二我们需要提供用户名和密码,哪能让你胡来。
我们知道,对于一个Maven项目而言,如果你的项目版本号中有“SNAPSHOT”字样,则表示此时的项目是snapshot版本,即处于开发中。否则,Maven则认为这是一个release版本。所以我们在部署时,需要分别配置这两种发布版本所对应的Repository。在项目的pom.xml文件中配置需要发布的目标Repository:
复制代码
<repository>
<id>releases</id>
<name>Nexus Release Repository</name>
<url>http://localhost:8080/nexus-2.5/content/repositories/releases/</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<name>Nexus Snapshot Repository</name>
<url>http://localhost:8080/nexus-2.5/content/repositories/snapshots/</url>
</snapshotRepository>
复制代码
在上面的配置中,我们分别配置了release版本和snapshot版本所对应的Repository,如果你项目的版本中包含了“SNAPSHOT”,此时将发布到Nexus的Snapshots Repository,否则发布在Releases Repository。
至于提供用户名和密码,这些信息当然不能放在项目中,一是不安全,另外不同的人可能有不同的用户名和密码,你不至于在每次部署时都修改一次吧。所以,Maven将这些信息的存放地点放在了~/.m2/settings.xml文件中,每台机器(基本上就是每个人啦)都可以有不同的settings.xml文件。在该文件中加入以下配置:
releases
admin
admin123
snapshots
admin
admin123
admin和admin123都是Nexus默认的,另外特别需要注意的是,这里的需要和上面项目pom.xml文件中配置Repostory的对应起来。
(四)发布到Nexus Repository
万事具备,只欠东风,在项目中执行:
mvn deploy
部署成功,再在Nexus中查看部署情况:
此时我们部署的是项目的snapshot版本,上图中的“me”目录中既包含了作者所部署的项目。