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


首先下载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链接,如下:

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

配置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 

Maven 3.1.0 发布,项目构建工具

Linux 安装 Maven

Maven3.0 配置和简单使用

Ubuntu下搭建sun-jdk和Maven2

Maven使用入门

本文永久更新链接地址

相关内容