您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

Maven 私服搭建

在本节中,我们来介绍一下如何使用 Nexus 来搭建我们自己的 Maven 私服。在之前的章节中,我们已经介绍过关于 Maven 仓库的了,就像下图张展示的那样子,今天,我们就重点的来讲一下关于私服这部分。

Nexus 的创始人是 Tamas Cservenak。在大约 2005 年的时候,Tamas 开始开发了 Proximity 系统,开发这个系统的原因是当时的电信公司提供的 ADSL 网络的传输速度过慢。Proximity 系统的目的也很明确,代理和缓存中央仓库中的构件,在 Maven 需要下载构件的时候,不需要经过糟糕的 ADSL 网络。后来,到了 2007 年的时候,Sonatype 团队邀请了 Tamas 创建了 Nexus 项目。

我们可以发现这又是一段类似 git 的产生的历史。大神们总是用这种方式,来改变着我们的历史。

接下来,我们来安装 Nexus 。首先,我们可以去 Sonatype 的上下载对应的 Nexus 版本。不过这里还是要吐槽一下这个地址的非常慢。这里,我们使用的是nexus-2.11.2-03-bundle.tar.gz版本。

首先,我们将安装包到服务器的对应目录中;

我们将安装包进行解压,执行命令tar -xvf nexus-2.11.2-03-bundle.tar.gz,解压后,对 nexus-2.11.2-03 目录进行 mv nexus-2.11.2-03 nexus

编辑环境变量,vi /etc/profile,在的最后,根据自己服务器的情况,如下的配置;

进入到 bin 目录中,cd /usr/local/src/nexus/bin

在 bin 目录中,有 Nexus 的可执行,我们来执行 nohup ./nexus start &,来启动 Nexus,启动成功后,在浏览器中,输入http://ip:port/nexus可以浏览私服的网址;

至此,我们就安装好了 Nexus。

接下来,我们来介绍如何配置 Nexus,来让其帮我们代理 Maven 的中央仓库。

首先,我们登陆 Nexus ,认的密码是 admin/admin123,登陆成功后,我们可以看到左侧出现更多的;

如下图操作,我们来代理仓库;

在下方的配置,如下图进行操作;

创建好代理仓库之后,还需要配置一下 Public Repositories;

在配置好 Public Repositories 之后,我们更新一下该仓库的索引;

更新索引可能会需要一段时间,等到更新好之后,我们可以 Nexus 中想要的构件,这时候说明 Nexus 的索引已经更新完成;

在配置好 Nexus 之后,我们要在项目中使用私服要如何使用呢?答案是,和使用其他的仓库没有太大区别。接下来,我们来具体介绍一下如何在项目中使用。

打开 Maven 的 setting.xml ,将其中的 mirrors 节点和 profiles 节点改为如下配置;

<mirrors>
    <mirror>
        <id>mic-maven</id>
        <mirrorOf>*</mirrorOf>
        <name>mic maven</name>
        <url>http://ip:port/nexus/content/groups/public/</url>
    </mirror>
</mirrors>
<profiles>
    <profile>
        <id>maven profile</id>
        <activation>
            <jdk></jdk>
            <activeByDefault>true</activeByDefault>
        </activation>
        <repositories>
            <repository>
                <id>chenyao-central</id>
                <name>chenyao maven</name>
                <url>http://ip:port/nexus/content/groups/public/</url>
                <releases>
                	<enabled>true</enabled>
                </releases>
                <snapshots>
                	<enabled>true</enabled>
                </snapshots>
            </repository>
        </repositories>
        <pluginRepositories>
            <pluginRepository>
                <id>chenyao-central</id>
                <name>chenyao maven</name>
                <url>http://ip:port/nexus/content/groups/public/</url>
                <releases>
                	<enabled>true</enabled>
                </releases>
                <snapshots>
                	<enabled>true</enabled>
                </snapshots>
            </pluginRepository>
        </pluginRepositories>
    </profile>
</profiles>
<activeProfiles>
	<activeProfile>maven profile</activeProfile>
</activeProfiles>

在 setting.xml 中配置好 Nexus 之后,我们可以在项目中引入 jar 包来试一下,如果能够成功的将构件下载到本地,就说明我们的配置是正确的。

介于我们在团队内部使用的时候,还需要将项目的构建到 Nexus 中,所以,我们还需要进行单独的配置。

首先,我们在项目的 pom.xml 中, distributionManagement 节点,用于控制分发管理;

<distributionManagement>
    <repository>
        <id>mic-release</id>
        <name>mic maven</name>
        <url>http://ip:port/nexus/content/repositories/releases/</url>
    </repository>
    <snapshotRepository>
        <id>mic-snapshot</id>
        <name>mic maven</name>
        <url>http://ip:port/nexus/content/repositories/snapshots/</url>
    </snapshotRepository>
</distributionManagement>

由于 Nexus 需要登陆才能使用,所以需要在 setting.xml 中配置服务器的登陆信息;

<servers>
	<server>
        <id>mic-release</id>
        <username>admin</username>
        <password>admin123</password>
    </server>
    <server>
        <id>mic-snapshot</id>
        <username>admin</username>
        <password>admin123</password>
    </server>
</servers>

配置完成后,我们在 mall-order目录下执行mvn clean deploy

执行完之后,我们去 Nexus 中根据坐标可以查到这个构件。

至此,我们已经能够让 Nexus 来代理中央仓库,并且将我们自己的项目构建到私服中去,来提供给其他的项目中使用。

在本节中,我们首先介绍了什么是 Nexus ,然后介绍了 Nexus 在服务器中的安装与配置方式,最后,我们将私服配置到我们的项目中去,让其供我们的项目使用。

本文中,我们选择的是 Nexus 的 2.x 版本,之所以这样做是因为,3.x 版本建议 核心数为 4 核,但是,我的服务器只是双核的,所以,被迫选择了 2.x 版本,3.x 版本和 2.x 版本理念上没有太多区别,只是具体配置操作可能会有差异。

Nexus 是不建议使用 root 来启动的。这里我们有两个选择,是单独创建,来提供 Nexus 使用(建议);另一种就是像本节中的配置,在环境变量中 RUN_AS_USER 配置,来强制使用 root 启动。


联系我
置顶