linux安装nexus做私服

介绍

Nexus 是Maven仓库管理器。我们使用maven从中央管理仓库里面下载所需要的artifact的。但是从远程仓库里面下载,会对网络上面有较高要求,虽然现在又阿里的仓库,
但是也需要很多时间,所以公司一般都是搭建自己的私服仓库,这样可以节约很多时间和带宽。提供了强大的仓库管理功能,构件搜索功能,它基于REST,友好的UI是一个
extjs的REST客户端,它占用较少的内存,基于简单文件系统而非数据库。阿里的仓库也是基于nexus来使用的。

nexus内部使用的web容器是jetty。与tomcat不同,具体区别可以去百度。

下载

下载地址:http://www.sonatype.org/nexus/

oss版本是免费开源的,而professional则是专业收费的版本。

如果是linux上面下载的话,下载nexus-XXXX-bundle.tar.gz包,这个是免安装的包。

如果是安装在windows上面,需要配置环境变量。

安装

在linux上面创建一个文件夹,用来装nexus的文件。我一般都是建在usr/local/nexus路径
终端上面使用命令:mkdir 来创建文件夹

然后解压下载的nexus包。终端使用命令:

1
tar -zxvf nexus-XXXX-bundle.tar.gz

解压后,就不用进行安装了。

然后我们需要在解压的文件夹里面改一些配置。进入文件夹下的conf文件夹,找到nexus.properties文件,然后进入编辑模式。

1
vim nexus.properties

修改里面的端口号,找到application-port=xxxx,修改成你需要的端口号,下面的ip地址如果要改的话可以改,作用是允许该ip地址下的子域访问。

然后进入解压文件夹的bin文件夹里面,找到nexus可执行文件,然后进行修改,找到RUN_AS_USER=XX,将后面的XX改成root,否则启动会报错。并且要将前面的注释符#去掉,这样才有效。
在此路径下执行运行nexus可执行文件命令

1
./nexus start

然后就可以在本机linux浏览器上面访问了。如果想要让其他机器访问的话,需要开放防火墙的端口号。

1
2
3
iptables -A INPUT -ptcp --dport  刚刚设置的端口号 -j ACCEPT
service iptables save //保存配置
service iptables restart //重启防火墙规则

然后我们就可以在外面的的机器通过linux的ip地址加端口号加nexus就可以访问了。例如:

1
192.168.0.156:9010/nexus

需要登录才能看到maven详细页面。默认名admin,密码admin123。

索引文件

索引文件就是nexus访问的页面上面可以看到的jar包,不过索引文件将其jar包做成文件的形式,并且带有目录的信息。
索引文件可以自动更新,也可以手动更新。自动更新是从远程仓库里面找没有的包下载过来,但是由于下载很慢,所以不推荐使用。手动更新其实就是替换
索引文件夹下的文件就可以了。

索引文件下载地址:http://repo.maven.apache.org/maven2/.index/
下载nexus-maven-repository-index.gz和nexus-maven-repository-index.properties的文件
jar文件下载地址:http://maven.outofmemory.cn/org.apache.maven.indexer/indexer-core/5.1.1/

然后解压索引文件包

1
Java  -jar  indexer-cli-5.1.0.jar  -u  nexus-maven-repository-index.gz  –d  indexer

那个indexer是解压后的文件夹名,可以自己设置

删除sonatype-work/nexus/indexer/central-ctx/路径下的所有文件

1
rm -rf *

把之前解压的文件全部复制到此路径下。

1
cp  -r  *  /usr/local/nexus/sonatype-work/nexus/indexer/central-ctx/

然后再次启动就可以了。

发布到仓库

我们可以手动添加到仓库里,也可以自动构建发布到仓库。

1.手动发布的话,要先找到3rd party仓库(第三方仓库),然后找到下面的artifact upload。
选择GAV Parameters,然后下面的信息,自己选择,填写完后点击add artifact,然后再点击upload artifact 就完成了发布包。

2.自动更新:
需要在pom.xml文件里添加以下信息

1
2
3
4
5
6
7
8
9
10
<distributionManagement>
<repository>
<id>releases</id> <!-- 发布版仓库id -->
<url>http://ip地址:9010/nexus/content/repositories/releases</url>
</repository>
<snapshotRepository>
<id>snapshots</id><!-- 快照版仓库id -->
<url>http://ip地址:9010/nexus/content/repositories/snapshots</url>
</snapshotRepository>
</distributionManagement>
当然想要发布到哪个仓库,就配置哪个仓库名。然后配置本地仓库的setting.xml文件,找到<server>标签,在里面添加
1
2
3
4
5
6
7
8
9
10
11
<!-- 配置maven私服账号,方便上传下载 -->
<server>
<id>releases</id>
<username>admin</username>
<password>admin123</password>
</server>
<server>
<id>snapshots</id>
<username>admin</username>
<password>admin123</password>
</server>

然后我们就可以在maven项目里面选择maven的deploy就可以,自动发布到仓库了。

私服下载构建

我们在使用maven项目的时候,我们在pom添加依赖的时候还是先根据本地仓库是否存在jar文件,如果没有然后再去阿里的仓库,并没有通过我们
的私服仓库,因为我们没有在本地仓库的配置文件(setting.xml)里面配置路径。

1.修改本地仓库的settings.xml文件,需要注意的是配置路径的顺序是有关于寻找的顺序,也就是说如果阿里路径配置在私服路径上面,还是会优先选择阿
里的仓库,所以我们应该将私服的路径写在阿里的路径之前。找到,添加/修改内容为:

1
2
3
4
5
6
7
8
9
<!-- maven私服镜像仓库访问地址 -->
<mirror>
<id>public</id><!-- 固定命名-->
<name>Public Repositories</name> <!-- 镜像仓库名称,自己命名 -->
<!-- 镜像仓库有效范围,* 代表全部-->
<mirrorOf>*</mirrorOf>
<!-- 镜像仓库访问地址(仓库组访问地址)-->
<url>http://ip地址:9010/nexus/content/groups/public/</url>
</mirror>

2.配置pom.xml文件,加上: 需要注意的是这种配置只对当前maven项目有效

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<repositories>
<repository>
<id>nexus</id>
<name>Nexus Repository</name>
<url> http:// ip地址:9010/nexus/content/groups/public/</url>
<releases><!-- 发布版本是否支持更新 -->
<enabled>true</enabled>
</releases>
<snapshots><!-- 快照版本是否支持更新 -->
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>snapshots</id>
<url>http:// ip地址:9010/nexus/content/repositories/snapshots</url>
</repository>
</repositories>

如果想要对本机的所有maven项目有效,配置settings.xml文件,配置profile元素,不过不推荐使用,加上:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<!-- 配置maven私服构建下载配置 -->
<profile>
<id>public</id>
<repositories>
<repository>
<id>nexus</id>
<name>Nexus Repository</name>
<url> http:// ip地址:9010/nexus/content/groups/public/</url>
<releases><!-- 发布版本是否支持更新 -->
<enabled>true</enabled>
</releases>
<snapshots><!-- 快照版本是否支持更新 -->
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>snapshots</id>
<url>http:// ip地址:9010/nexus/content/repositories/snapshots</url>
</repository>
</repositories>
</profile>

然后,在settings.xml文件中最后加上:

1
2
3
4
5
6
<!-- 激活profile配置,否则配置不生效 -->
<activeProfiles>
<!-- 在profiles 中配置几个profile,就需要激活几个配置 -->
<activeProfile>jdk-1.8</activeProfile><!-- 激活jdk的配置,否则配置不生效 -->
<activeProfile>public-nexus</activeProfile><!-- 激活nexus的配置,否则配置不生效 -->
</activeProfiles>