阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Maven实战——使用Nexus创建私服

193次阅读
没有评论

共计 4908 个字符,预计需要花费 13 分钟才能阅读完成。

首先下载 Nexus,官方下载地址是 http://nexus.sonatype.org/download, 我们可以根据需要下载不同的 Bundle 包,都是有.tar.gz、.zip 和.war 格式的

1、bundle 方式安装 nexus
nexues 的 Bundle 自带了 Jetty 容器,因此用户不需要额外的 web 容器就能直接启动 nexus。首先将 Bundle 文件解压到,会得到两个目录:
nexus-webapp-1.7.2/: 该目录包含了 Nexus 运行所需要的文件,如启动脚本、依赖 jar 包
sonatype-work/:该目录包含了 Nexus 生成的配置文件、日志文件、仓库文件等
当用户需要备份 Nexus 的时候,默认备份 sonatype-work 目录
在 window 系统用户进入 nexus-webapp/bin/jsw/windows-x86-32 子目录直接运行 nexus.bat 就能启动 Nexus。
这时,打开浏览器 http://localhost:8081/nexus/ 就能看到 Nexus 的界面,如果要停止 Nexus 可以再命令行按 Ctrl+C
其他脚本说明:
InstallNexus.bat: 将 Nexus 安装成 Window 服务
Uninstallnexus.bat: 卸载 Nexus Window 服务
Startnexus.bat: 启动 Nexus Window 服务
Stopnexus.bat: 停止 Nexus Window 服务
Pausenexus.bat: 暂停 Nexus Window 服务
Resumenexus.bat: 恢复暂停的 Nexus Window 服务
其他的安装安装方式就不一一介绍了

2、登录 Nexus
Nexus 默认管理员的用户名和密码是 admin/admin123

Nexus 的仓库和仓库组
1、Nexus 内置的仓库
单机 Nexus 界面左边导航栏中干的 Repositories 链接,如下:

Maven 实战——使用 Nexus 创建私服

从图中可以看到四种仓库类型:group(仓库组)、hosted(宿主)、proxy(代理)和 virtual(虚拟)。此外仓库还有一个属性为 Policy(策略),表示该仓库为发布(Release)版本仓库还是快照(Snapshot)版本仓库。最后两列的值为仓库的状态和路径。
解释下各个仓库的作用:
1、Maven Central:该仓库代理 Maven 中央仓库,其策略为 Release,因此只会下载和缓存中央仓库的发布版本构件
2、Releases:这是一个策略为 Release 的宿主类型仓库,用来部署组织内部的发布版本构件
3、Snapshots:这是一个策略为 Snapshot 的宿主类型仓库,用来部署组织内部的快照版本构件
4、3rd party:这是一个策略为 Release 的宿主类型仓库,用来部署无法从公共仓库获得的第三方版本构件
5、Apache Snapshots:这是一个策略为 Snapshot 的代理仓库,用来代理 Apache Maven 仓库的快照版本构件
6、Public Repositories:该仓库组将上述所有策略为 Release 的仓库聚合并通过一致的地址提供服务
7、Public Snapshot Repositories:该仓库组将上述所有策略为 Snapshot 的仓库聚合并通过一致的地址提供服务

Nexus 仓库分类概念

Maven 实战——使用 Nexus 创建私服

从上图可以看出 Maven 可以直接从宿主仓库下载构件,Maven 也可以从代理仓库下载构件,而代理仓库会间接的从远程仓库下载并缓存构件。最后,为了方便 Maven 可以从仓库组下载构件,而仓库组没有实际内容,他会转向宿主仓库或者代理仓库获得实际构件内容。
登录之后点击 add 按钮,可以创建各种类型的仓库,如下图所示:

Maven 实战——使用 Nexus 创建私服

配置 Maven 从 Nexus 下载构件
当需要为项目添加 Nexus 私服上的 public 仓库时,配置如下:

<project>
    …
    <repositories>
        <repository>
            <id>nexus</id>
            <name>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>nexus</id>
            <name>Nexus</name>
            <url>http://localhost:8081/nexus/context/groups/public/</url>
            <snapshots><enabled>true</enabled></snapshots>
        </pluginRepository>
    </pluginRepositories>
    …
</project>

这样的配置只对当前 Maven 项目有效,在实际应用中,我们往往想要通过一次配置就能让本机所有的 Maven 项目都使用自己的 Maven 私服。
这个时候我们想到 settings.xml 文件,该文件中的配置对所有本机 Maven 项目有效,但是 settings.xml 并不支持直接配置 repositories 和 pluginsRepositories。所幸 Maven 还提供了 Profile 机制,能让用户将仓库配置放到 settings.xml 的 profile 中,代码如下:

<settings>
    …
    <profiles>
        <profile>
            <id>nexus</id>
            <repositories>
                <id>nexus</id>
                <name>Nexus</name>
                <url>http://localhost:8081/nexus/content/groups/public/</url>
                <releases><enabled>true</enabled></releases>
                <snapshots><enabled>true</enabled></releases>
            </repositories>
            <pluginRepositories>
                <pluginRepository>
                    <id>nexus</id>
                    <name>Nexus</name>
                    <url>http://localhost:8081/nexus/content/groups/public/</url>
                    <releases><enabled>true</enabled></releases>
                    <snapshots><enabled>true</enabled></releases>
                </pluginRepository>
            </pluginRepositories>
        </profile>
    </profiles>
    <activeProfiles>
        <activeProfile>nexus</activeProfile>
    </activeProfiles>
    …
</settings>

该配置中使用了一个 id 为 nexus 的 profile,这个 profile 包含了相关的仓库配置,同时配置中又使用了 activeProfile 元素将 nexus 这个 profile 激活。这样当执行 Maven 构建的时候,激活的 profile 会将仓库配置应用到项目中去。Maven 除了从 Nexus 下载构件之外,还会访问中央仓库 central,我们希望的是所有 Maven 下载请求都仅仅通过 Nexus,以全面发挥私服的作用。这需要借助 Maven 镜像配置了。可以创建一个匹配任何仓库的镜像,镜像地址为私服,这样 Maven 对任何仓库的构件下载请求都会转到私服中,具体配置如下:

<settings>
    …
    <mirrors>
        <mirror>
            <id>nexus</id>
            <mirrorOf>*</mirrorOf>
            <url>http://localhost:8081/nexus/content/groups/public</url>
        </mirror>
    </mirrors>
    <profiles>
        <profile>
            <id>nexus</id>
            <repositories>
                <repository>
                    <id>central</id>
                    <url>http://central</url>
                    <releases><enabled>true</enabled></releases>
                    <snapshots><enabled>true</enabled></releases>
                </repository>
            </repositories>
            <pluginRepositories>
                <pluginRepository>
                    <id>central</id>
                    <url>http://central</url>
                    <releases><enabled>true</enabled></releases>
                    <snapshots><enabled>true</enabled></snapshots>
                </pluginRepository>
            </pluginRepositories>
        </profile>
    </profiles>
    <activeProfiles>
        <activeProfile>nexus</activeProfile>
    </activeProfiles>
    …
</settings>

这里需要解释的是仓库插件及插件仓库配置,他们 id 都为 central,覆盖了超级 POM 中的中央仓库配置,他们的 url 已无关紧要,因为所有的请求都会通过镜像访问私服地址。配置仓库及插件仓库的主要目的是开启对快照版本下载的支持。当 Maven 需要下载发布版本或者快照构件时,首先检查 central,看该类型构件��否支持下载,得到正面回答后再根据镜像匹配规则转而访问私服仓库地址。

Maven 权威指南_中文完整版清晰 PDF  http://www.linuxidc.com/Linux/2014-06/103690.htm

Maven 3.1.0 发布,项目构建工具 http://www.linuxidc.com/Linux/2013-07/87403.htm

Linux 安装 Maven http://www.linuxidc.com/Linux/2013-05/84489.htm

Maven3.0 配置和简单使用 http://www.linuxidc.com/Linux/2013-04/82939.htm

Ubuntu 下搭建 sun-jdk 和 Maven2 http://www.linuxidc.com/Linux/2012-12/76531.htm

Maven 使用入门 http://www.linuxidc.com/Linux/2012-11/74354.htm

本文永久更新链接地址:http://www.linuxidc.com/Linux/2015-06/118609.htm

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-20发表,共计4908字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中