从零搭建Maven本地私服和简单使用

    xiaoxiao2021-04-14  38

    前言:

    不知道你对maven中央仓库导入依赖的方法怎么看,反正我是挺不喜欢这种方法的,弄个maven项目简直不要太浪费时间,而且最后你还会发现好多jar包都没有导入.

    所以,特地来写篇maven本地仓库的搭建.

    通过建立自己的私服,就可以降低中央仓库负荷,节省外网宽带,加速maven构建,自己部署构建等,从而高效的使用Maven

    有三种专门的Maven仓库管理软件可以来帮助大家建立私服:Apache的Archiva,JFrog的Artifactory和Sonatype的Nexus.

    在此我们介绍Nexus,目前最流行的Maven仓库管理软件

    1.Nexus简介

    Nexus有开源版和专业版.一下是开源版的一些特性:

    较小的内存占用(最少仅为28K)基于ExtJs基于Restlet的完全REST API支持代理仓库,宿主仓库和仓库组基于文件系统,不需要数据库支持仓库索引和搜索支持从界面上传Maven构件细粒度的安全控制

    2.安装Nexus(JDK建议使用1.7)

    Nexus是典型的JavaWeb应用,他有两种安装包,一种是包含Jetty容器的Bundle包,一种是不包含Web的war包

    下载地址:nexus下载地址

    Buddle自带了Jetty容器,因此用户不需要额外的Web容器就能直接启动Nexus.此处将该方式的安装.

    war包的安装只需要放到tomcat等web容器下启动就好了.

    Window的安装方式较为简单,此处讲解CentOS的安装.

    命令:

    #su root   //切换到管理员

    下载

    # wget https://sonatype-download.global.ssl.fastly.net/nexus/oss/nexus-2.11.2-03-bundle.tar.gz

    不过还是建议在windows上面下载了然后拖到linux下解压安装

    进入配置文件

    修改配置文件

     # vi nexus.properties

        保存命令     按ESC键 跳到命令模式,然后:     :w   保存文件但不退出vi     :w file 将修改另外保存到file中,不退出vi     :w!   强制保存,不推出vi     :wq  保存文件并退出vi     :wq! 强制保存文件,并退出vi     q:  不保存文件,退出vi     :q! 不保存文件,强制退出vi     :e! 放弃所有修改,从上次保存文件开始再编辑       之后,回车,ok!

    进入以下路径修改成以root启动

    修改成以root启动

    保存 上面已经说过

    启动

    ./nexus start 

    启动以后去看启动日志:

    日志路径.

    出现如上问题,将jdk换成1.7,再次启动.

    如果提示权限不够,使用命令chmod 775 nexus.

    启动完成没报错就可以在浏览器中输入了:ip:8081/nexus

    将nexus设置为开机自启动

    #vi  /etc/rc.d/rc.local  编辑该文档,在该文档最后一行加上你nexus所在目录路径,然后./nexus start

    至此maven本地私服搭建完成.

    3.创建Nexus代理仓库

    安装完成以后,接下来创建一个阿里的代理仓库.保证你速度快的飞起

    其他配置不做描述,有兴趣自己百度.

    然后将阿里的代理仓库放入自己的仓库组中.

    4.maven项目索引 下载Maven项目索引,项目索引是为了使用者能够在私服站点查找依赖使用的功能

    保存后后台会运行一个任务,点击菜单栏的Scheduled Tasks选项即可看到有个任务在RUNNING。 同样,要使用阿里的仓库,也需要右键,repair index,载完成后,Maven索引就可以使用了,在搜索栏输入要搜索的项,就可以查到相关的信息。例如spring-core

    5.配置Maven从Nexu下载构件

    <!-- 配置相关私服信息指定仓库 --> <repositories> <repository> <id>nexus</id> <name>nexus</name> <!-- 仓库组所在路径 --> <url>http://192.168.0.107: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://192.168.0.107:8081/nexus/content/groups/public/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </pluginRepository> </pluginRepositories>但是这样的配置只对当前Maven项目有效,在实际应用中,我们往往想要通过一次配置就能让本机所有的Maven项目都能使用自己的Maven私服,这个时候,就要在settings.xml文件里面进行设置

    <!-- 配置nexus仓库 --> <profile> <id>nexus</id> <repositories> <repository> <id>nexus</id> <name>Nexus</name> <url>http://192.168.0.107:8081/nexus/content/groups/public/</url> <releases> <enable>true</enable> </releases> <snapshots> <enable>true</enable> </snapshots> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>nexus</id> <name>Nexus</name> <url>http://192.168.0.107:8081/nexus/content/groups/public/</url> <releases> <enable>true</enable> </releases> <snapshots> <enable>true</enable> </snapshots> </pluginRepository> </pluginRepositories> </profile> <!-- 激活这几个配置 --> <activeProfile> <activeProfile>nexus</activeProfile> </activeProfile>以上配置已经能让本机所有的Maven项目从Nexus私服下载构建,但是Maven除了从nexus上下载构建以外还会不时的访问中央仓库central,但是我们希望的是所有的Maven下载请求都仅仅通过Nexus,以全面发挥私服的作用.这时候就需要用到Maven镜像配置,可以创建一个匹配任何仓库的镜像,镜像的地址为私服.这样Maven对任何仓库的构件下载请求都会转到私服中.

    具体配置:

    <mirrors> <!-- 让所有请求都走私服 --> <mirror> <id>nexus</id> <url>http://192.168.0.107:8081/nexus/content/groups/public/</url> <mirrorOf>*</mirrorOf> </mirror> </mirrors> <profiles> <profile> <id>nexus</id> <repositories> <repository> <id>central</id> <url>http://central</url> <releases> <enable>true</enable> </releases> <snapshots> <enable>true</enable> </snapshots> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>central</id> <url>http://central</url> <releases> <enable>true</enable> </releases> <snapshots> <enable>true</enable> </snapshots> </pluginRepository> </pluginRepositories> </profile> <!-- 激活这几个配置 --> <activeProfile> <activeProfile>nexus</activeProfile> </activeProfile> </profiles>可以看到他们的id都是central.也就是说,覆盖了超级POM中央仓库的配置,他们的url已无关紧要,因为所有请求都会通过镜像访问私服地址.

    配置仓库及插件的主要目的是开启对快照版本下载的支持,当maven要下载发布版本或者快照版本的构建时,它首先检查central,看该类型的构件是否支持下载,得到正面回答以后,再根据镜像匹配规则转而访问私服仓库地址

    6.部署构建至Nexus

    日常开发生成的快照版本构件可以直接部署到Nexus中策略为Snapshot的宿主仓库中,项目正式发布的构件则应该部署到Nexus中策略为Realease的宿主仓库中.

    在项目的pom.xml中的配置为

    <distributionManagement> <repository> <id>nexus-releases</id> <name>Nexus Releases Repository</name> <!-- 指定正式版发布的依赖地址 --> <url>http://192.168.0.107:8081/nexus/content/repositories/releases/</url> </repository> <!-- 快照版 --> <snapshotRepository> <id>nexus-snapshots</id> <name>Nexus Snapshots Repository</name> <url>http://192.168.0.107:8081/nexus/content/repositories/snapshots/</url> </snapshotRepository> </distributionManagement>但是Nexus的仓库对于匿名用户是只读的,为了能够部署构建还需要在settings.xml中配置认证信息

    <servers> <!-- 配置认证信息 --> <server> <id>nexus-releases</id> <username>admin</username> <password>admin123</password> </server> <server> <id>nexus-snapshots</id> <username>admin</username> <password>admin123</password> </server> </servers>配置完这些以后当你需要发布当前项目时候,只需要使用maven命令

    clean deploy

    发布成功,然后就可以去私服中看当前项目是否发布成功

    时间什么的也是一目了然.

    7.手动部署第三方构建至Nexus

    某些Java Jar文件(如Oracle)的JDBC驱动,由于许可证的因素,它们无法公开的放在公共仓库,此外还有大量的小型开源项目,他们没有把自己的构件分发到中央仓库,也没有维护自己的仓库,因此无法从公共仓库获得,这时候用户就需要自己将这类构件手动下载到本地,然后通过nexus的界面上传到私服中.

    上传完以后你就可以根据上传的坐标在maven中使用它了.

    参考资料:

    Maven实战

    转载请注明原文地址: https://ju.6miu.com/read-669670.html

    最新回复(0)