1. 安装准备
nexus的下载地址: http://www.sonatype.org/nexus/go , 本文选用的是nexus-2.14.4-03-bundle.tar.gz,这个bundle包内嵌Jetty web容器,直接用命令就可以启动nexus服务。
另外,因为jetty运行在jre环境下,所以还需要预先安装好jdk,本文选用jdk 1.7
2. 安装与配置
2.1 安装与运行
1、上传 nexus-2.14.4-03-bundle.tar.gz 文件到/usr/local/nexus目录
2、解压到当前目录
tar -zxvf nexus-2.14.4-03-bundle.tar.gz
这时在nexus目录有两个文件夹,分别是nexus-2.14.4-03和sonatype-work。前一个是nexus 服务所在的文件夹,后一个是私有库目录。
3、编辑 Nexus 的 nexus.properties 文件,配置端口和 work 目录信息(本文选择不改变)。
vim /usr/local/nexus/nexus-2.14.4-03/conf/nexus.properties
文件的部分内容如下:
application-port=8081 //访问端口
application-host=0.0.0.0
nexus-webapp=${bundleBasedir}/nexus
nexus-webapp-context-path=/nexus
nexus-work=${bundleBasedir}/../sonatype-work/nexus //下载的文件存放的目录
runtime=${bundleBasedir}/nexus/WEB-INF
4、编辑 nexus 脚本, 配置 RUN_AS_USER 参数
vim /usr/local/nexus/nexus-2.14.4-03/bin/nexus
这里设置运行该服务的用户是root
#RUN_AS_USER=
改为:
RUN_AS_USER=root
5、防火墙中打开 8081 端口(具体端口与上面nexus.properties 文件中的一致)
# vi /etc/sysconfig/iptables
添加:
-A INPUT -m state --state NEW -m tcp -p tcp --dport 8081 -j ACCEPT
保存后重启防火墙
# service iptables restart
6、启动 nexus
/usr/local/nexus/nexus-2.14.4-03/bin/nexus start
这货运行过程挺久的,耐心等待。 在浏览器输入 http://192.168.88.16:8081/nexus/ ,如果运行成功可以看到下面的画面
2.2 配置
1、管理员身份登录
点击右上角的“log on”进行登录,默认用户名和密码:admin / admin123
登录成功后,在左边的侧边栏点击repositories,可以看到当前仓库的情况
-
group 仓库组: Nexus 通过仓库组的概念统一管理多个仓库,在项目中直接请求仓库组即可请求到仓库组管理的多个仓库
-
hosted 宿主仓库: 主要用于发布内部项目构件或第三方的项目构件 (如购买商业的构件)以及无法从公共仓库获取的构件(如 oracle 的 JDBC 驱动)
-
proxy 代理仓库: 从远程中央仓库中寻找数据的仓库
-
releases 内部的模块中release模块的发布仓库
-
snapshots 内部的SNAPSHOT模块的仓库
-
3rd party 第三方依赖的仓库,这个数据通常是由内部人员自行下载之后发布上去
2、 更新索引
(1) 自动更新
由于国情的原因,连接maven中央库的速度非常慢,一般不建议自动更新索引。
-
打开Repositories标签,选中远程仓库并打开Configuration,将Download Romote Location 设置为true
-
在远程仓库上右键选择Update Index,Nexus会自动建立一条任务计划;一般远程仓库都比较大,构建会比较多,索引文件会很大,像http://repo1.maven.org/maven2 就有几百M,因此需要的时间就比较长
-
可以进入Scheduled Tasks查看任务的执行情况,当执行完成时,远程仓库的索引就已经建立完毕了
(2) 手动更新
- 下载索引文件
需要下载三个文件 nexus-maven-repository-index.gz、nexus-maven-repository-index.properties 和 indexer-cli-5.1.1.jar,并放在同一个目录。
indexer-cli-5.1.1.jar下载地址: http://maven.outofmemory.cn/org.apache.maven.indexer/indexer-cli/5.1.1/
nexus-maven-repository-index.gz 和 nexus-maven-repository-index.properties的下载地址: http://repo.maven.apache.org/maven2/.index/
- 解压索引文件
java -jar indexer-cli-5.1.1.jar -u nexus-maven-repository-index.gz -d indexer
- 解压后的文件拷贝到指定目录
程序运行完成之后可以发现indexer文件夹下出现了很多文件,将这些文件放置到{nexus-home}/sonatype-work/nexus/indexer/central-ctx目录下,重新启动nexus
当我们更新索引成功后,可以在Browse Index后选项卡看到许多文件
3. 发布项目到nexus
- 在预发布的项目POM文件中配置发布的nexus服务地址
<distributionManagement>
<repository>
<id>nexus-releases</id>
<name>Nexus Release Repository</name>
<url>http://192.168.88.16:8081/nexus/content/repositories/releases/</url>
</repository>
<snapshotRepository>
<id>nexus-snapshots</id>
<name>Nexus Snapshot Repository</name>
<url>http://192.168.88.16:8081/nexus/content/repositories/snapshots/</url>
</snapshotRepository>
</distributionManagement>
上面URL就是之前配置的nexus服务地址。配置完后,如果这时发布项目到私服肯定会报未授权的错误,我们还需要配置用于发布的账号和密码。
- 配置发布用的账号和密码
修改maven的setting.xml,这里使用的是nexus的管理员账号和密码,我们也可以在security的user中添加用于发布用的账户
<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>
- 发布项目
mvn clean deploy
项目成功发布后,我们可以在nexus的平台的仓库里看到发布的项目。发布分为snapshot版本和release版本。通过修改项目里的pom.xml的version后缀,可以改变是发布快照版本还是正式版。即需要发布快照版本,修改为:<version>0.0.1-SNAPSHOT</version>;当下需要发布正式版时,<version>0.0.1-RELEASE</version>
4. 手动上传第三方jar到私服
有些jar假如jar包在中央仓库中是没有的,这样就不能在远程仓库中拉取放入到私服中,这时需要手动上传jar包。这里以oracle的jdbc包为例。
- 选中宿主库——3rd party,之后选择Artifact Upload上传至宿主空间。
5. 配置私服为中央仓库
(1)仅在本项目有效
在项目中添加
<repositories>
<!-- 私有库地址 -->
<repository>
<id>nexus</id>
<url>http://192.168.88.16:8081/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<!--插件库地址 -->
<pluginRepository>
<id>nexus</id>
<url>http://192.168.88.16:8081/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
(2)全局有效
修改maven的setting.xml
<profiles>
<profile>
<id>nexus</id>
<activation>
<activeByDefault>false</activeByDefault>
<jdk>1.6</jdk>
</activation>
<repositories>
<!-- 私有库地址-->
<repository>
<id>nexus</id>
<url>http://192.168.88.16:8081/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<!--插件库地址-->
<pluginRepository>
<id>nexus</id>
<url>http://192.168.88.16:8081/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
<!--激活profile-->
<activeProfiles>
<activeProfile>nexus</activeProfile>
</activeProfiles>