Nexus就是Maven的私服
在日常开发中我们在使用maven时经常会遇到下面的问题
1.一些无法从外部仓库下载的构件,例如内部的项目部署到私服上,以便供其他依赖项目使用。
2. 为了节省带宽和时间,在局域网内架设一个私有的仓库服务器,用其代理所有外部的远程仓库。当本地Maven项目需要下载构件时,先去私服请求,如果私服没有,则再去远程仓库请求,从远程仓库下载构件后,把构件缓存在私服上。这样,即使暂时没有Internet链接,由于私服已经缓存了大量构件,整个项目还是可以正常使用的。同时,也降低了中央仓库的负荷。
3.微服务中,可能我们需要调用其他项目组的接口,但是拿不到源码,此时可以将要调用的接口项目打成jar包,发布到私服。我们自己的项目只要从maven私服下载即可。
使用docker安装nexus私服
1.docker下载镜像
docker pull sonatype/nexus3
2.运行容器,-v 将容器内部/var/nexus-data挂载到主机/root/nexus-data目录。
docker run -d -p 8888:8081 --name nexus -v /root/nexus-data:/var/nexus-data --restart=always sonatype/nexus3
启动比较耗时 可以通过docker inspect 容器id 查询容器信息
docker inspect a1683e57757a
使用curl 尝试访问 访问成功说明服务启动成功
测试访问 192.168.3.61:8888,成功访问证明搭建成功
第一次使用admin登陆时会要我们重设密码,初始密码在docker容器中的/nexus-data/admin.password 文件中可以查看
使用 docker exec 进入容器内部
docker exec -it a1683e57757a /bin/bash
获取秘钥登陆 修改密码登陆即可
上传jar包到私服
点击设置 仓库 可以看到仓库信息
创建仓库,点击Create repository,然后选择maven2(hosted) 本地仓库。然后输入仓库名称(test-release)。
在version policy中选择这个仓库的类型,这里选择release,在Deployment policy中选择Allow redeploy(这个很重要).
创建成功后,点击刚创建好的仓库,其中URL为仓库地址
回到仓库,点击maven-public 仓库组中添加刚刚创建的本地仓库 test-release
将test-release加入到仓库组中
到这一步就已经在Nexus服务器添加存放第三方Jar包的本地仓库
点击左侧菜单栏的Users菜单,然后点击Create local user.我这里创建了一个用户,账号密码都是:xuxu
找到本地maven中的settings.xml文件
添加上面注册的信息
<server>
<id>nexus-snapshot</id>
<username>xuxu</username>
<password>xuxu</password>
</server>
<server>
<id>nexus-release</id>
<username>xuxu</username>
<password>xuxu</password>
</server>
id
:待会输入命令的时候需要用到username
:表示Nexus服务器的登陆账号password
:表示Nexus服务器的登陆密码
将自己本地下载jar包上传到maven私服
找到放置要上传jar包的文件
打开cmd控制台
mvn deploy:deploy-file -DgroupId=com.xuxu -DartifactId=commons-lang3-3.7 -Dversion=3.7 -Dpackaging=jar -Dfile=C:\Users\Administrator\Desktop\testjar\commons-lang3-3.7.jar -Durl=http://192.168.3.63:8888/repository/test-release/ -DrepositoryId=nexus-release
执行该命令
其中:
DgroupId、DartifactId、Dversion
:构成了该jar包在pom.xml的坐标,自己起名字也是可以的.Dpackaging
:表示打包类型.Dfile
:表示需要上传的jar包的绝对路径.Durl
:私服上第三方仓库的地址,打开nexus——>repositories菜单,可以看到该路径。DrepositoryId
:服务器的表示id,就是我们在setting.xml文件中配置的serverId。
将公司内部项目打包并上传至maven私服
创建一个maven项目
pom中添加依赖
<!--注意限定版本一定为RELEASE,因为上传的对应仓库的存储类型为RELEASE -->
<!--指定仓库地址 -->
<distributionManagement>
<repository>
<!--此名称要和settings.xml中设置的ID一致 -->
<id>nexus-release</id>
<url>http://192.168.3.63:8888/repository/test-release/</url>
</repository>
</distributionManagement>
<build>
<plugins>
<!--发布代码Jar插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.7</version>
</plugin>
<!--发布源码插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
这里一定要注意 id 要和setting.xml中配置服务ID保持一致不然会报错
右键项目选择show in ------terminal
terminal终端中输入 mvn clean deploy 部署命令
执行完毕后 访问nexus控制台可以看到已经成功部署上传该jar包
测试 先删除本地工程xuxu_maven_test_jar
新建一个maven工程 xuxu_maven_test 引入pom依赖
测试类中调用该jar包中的类
public class Test {
public static void main(String[] args) {
TestJar testJar = new TestJar();
testJar.test1();
TestJar.test2();
}
}
说明成功。