0%

Sonatype Nexus mavn私服安装与maven上传jar包

为什么要使用Maven私服?

某些jar包仅供内部使用,不适合在中央库发布。不用私服,团队间只能私底下传输打出来的jar包了,私服就是用来管理这些包。

Sonatype Nexus是什么?

Sonatype Nexus就是一个maven私服,也可以称做代理服务器。原理是代理中央库,同时管理私有库,在使用时,我们只要把maven的镜像地址配置成nexus地址即可。

Sonatype Nexus的安装

懒得折腾,所以直接用docker了。使用的是Sonatype Nexus Repository Manager 2镜像。当然,如果你有钻研学习精神,还是自己按官方教程一步一步来,否则建议直接用docker,一个字,快!

docker安装不再介绍

step1: 创建数据目录

Sonatype Nexus会有一些索引之类的数据需要放到外部目录,我们先创建它,使用docker都是这么个套路,需要把数据放到外部。假设这里的目录是/Users/xxx/docker/nexus-data

step2: 下载并启动Sonatype Nexus

docker run -d -p 8081:8081 --name nexus -v /Users/xxx/docker/nexus-data:/sonatype-work sonatype/nexus

docker会去下载相应的镜像,并运行,端口号8081,打开http://localhost:8081/nexus/确认成功

到此安装完成,默认账号密码admin/admin123

Sonatype Nexus 配置

此时如果使用搜索功能,会发现什么都找不到,因为现在还没有中央库的索引。

上面说过,Sonatype Nexus需要代理中央库,所有maven下载库的请求都会通过Nexus,如果Nexus上没有,去中央库下载,如果有,直接返回了。

更新中央库索引。
更新索索

如果你现在在国外,或者连着很快的vpn,那么,过一会,应该就可以更新完索引了,因为中央库服务器在国外,国内下载速度慢。

国内用户怎么办呢?在架这个nexus之前,大家应该都用阿里云的镜像吧?速度还算快,那么我们把默认的中央库地址换成阿里云的
替换地址
阿里云上还有个jcenter,也加上,再更新索引。

在服务器上执行需谨慎,我的服务器昨天被封,到目前为止还是访问不了阿里云maven镜像,服务器换IP不容易。

大概过了一两个小时,我这下完了,下完其实还要在本地建索引什么的,需要点时间,反正我这边监控网络已经没速度了,docker的cpu占用还是很高,cpu风扇狂转,这一步需要时间,请耐心。

索引建完以后(Administration/Scheduled Tasks是可以看到当前任务的),就可以搜索到中央库上的包了。

此时,Nexus已经可以使用了,当然,改密码,配权限,不用说了,自己改.

客户端配置settings.xml

settings.xml在~/.m2/下,编辑:

<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">

    <servers>
        <server>
            <id>nexus</id>
            <username>admin</username>
            <password>admin123</password>
        </server>
    </servers>

    <mirrors>
        <mirror>
            <id>Nexus</id>
            <mirrorOf>*</mirrorOf>
            <name>Nexus Repository</name>
            <url>http://localhost:8081/nexus/content/groups/public</url>
        </mirror>
    </mirrors>

    <profiles>
        <profile>
            <id>nexus</id>
            <!--Enable snapshots for the built in central repo to direct -->
            <!--all requests to nexus via the mirror -->
            <repositories>
                <repository>
                    <id>nexus</id>
                    <url>http://localhost:8081/nexus/content/content/groups/public</url>
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                    <snapshots>
                        <enabled>true</enabled>
                    </snapshots>
                </repository>
            </repositories>
            <pluginRepositories>
                <pluginRepository>
                    <id>nexus</id>
                    <url>http://localhost:8081/nexus/content/content/groups/public</url>
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                    <snapshots>
                        <enabled>true</enabled>
                    </snapshots>
                </pluginRepository>
            </pluginRepositories>
        </profile>
    </profiles>

    <activeProfiles>
        <!--make the profile active all the time -->
        <activeProfile>nexus</activeProfile>
    </activeProfiles>
</settings>

账号密码和服务器地址改一下就可以了。

Maven项目配置

打开pom.xml,加入:

<distributionManagement>
        <snapshotRepository>
            <id>nexus</id>
            <name>nexus snapshots</name>
            <url>http://localhost:8081/nexus/content/repositories/snapshots
            </url>
        </snapshotRepository>
        <repository>
            <id>nexus</id>
            <name>nexus releases</name>
            <url>http://localhost:8081/nexus/content/repositories/releases
            </url>
        </repository>
    </distributionManagement>

发布项目到nexus

mvn package