maven私服的使用

时间:2021-03-06 16:32:11

使用的版本是nexus2

比较犀利的一个博客https://www.cnblogs.com/tyhj-zxp/p/7605879.html

一.安装搭建私服(windows)

  bin目录cmd执行nexus install

  可以使用 nexus命令在bin目录下查看命令

  查看服务中的nexus中多了一个nexus服务启动即可

  Nexus 本地网址 localhost:8081/nexus admin/admin123

二.基本概念介绍

  仓库类型:

    Ø  hosted,本地仓库,通常我们会部署自己的构件到这一类型的仓库。比如公司的第二方库。

    Ø  proxy,代理仓库,它们被用来代理远程的公共仓库,如maven*仓库。

    Ø  group,仓库组,用来合并多个hosted/proxy仓库,当你的项目希望在多个repository使用资源时就不需要多次引用了,只需要引用一个group即可。

  仓库:

    Releases:这里存放我们自己项目中发布的构建, 通常是Release版本的, 比如我们自己做了一个FTP Server的项目, 生成的构件为ftpserver.war, 我们就可以把这个构建发布到Nexus的Releases本地仓库. 关于符合发布后面会有介绍.

    Snapshots:这个仓库非常的有用, 它的目的是让我们可以发布那些非release版本, 非稳定版本, 比如我们在trunk下开发一个项目,在正式release之前你可能需要临时发布一个版本给你的同伴使用, 因为你的同伴正在依赖你的模块开发, 那么这个时候我们就可以发布Snapshot版本到这个仓库, 你的同伴就可以通过简单的命令来获取和使用这个临时版本.

    3rd Party:顾名思义, 第三方库, 你可能会问不是有*仓库来管理第三方库嘛,没错, 这里的是指可以让你添加自己的第三方库, 比如有些构件在*仓库是不存在的. 比如你在*仓库找不到Oracle 的JDBC驱动, 这个时候我们就需要自己添加到3rdparty仓库。

三.使用eclipse配置上传和下载jar

  1)上传jar包

    a)配置远程仓库认证在maven配置文件settings.xml servers 标签下配置

 <server>
<!--releases 连接发布版本项目仓库-->
<id>releases</id>
<!--访问releases这个私服上的仓库所用的账户和密码-->
<username>admin</username>
<password>admin123</password>
</server>
<server>
<!--snapshots 连接测试版本项目仓库-->
<id>snapshots</id>
<!--访问releases这个私服上的仓库所用的账户和密码-->
<username>admin</username>
<password>admin123</password>
</server>

    b)在项目的pom文件中配置project下面配置,注意和上面的两者的id一致

<distributionManagement>
<!--pom.xml这里<id> 和 settings.xml 配置 <id> 对应 -->
<repository>
<id>releases</id>
<url>http://localhost:8081/nexus/content/repositories/releases/</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<url>http://localhost:8081/nexus/content/repositories/snapshots/</url>
</snapshotRepository>
</distributionManagement>

    c)deploy命令上传jar 到时候会根据项目的版本上传jar/war包到私服

  2)下载jar包 

    0)大部分公共的远程仓库无须认证就可以直接访问,但我们在平时的开发中往往会架设自己的Maven远程仓库,出于安全方面的考虑,我们需要提供认证信息才能访问这样的远程仓库。认证信息必须配置在settings.xml文件中。在settings.xml中配置认证信息更为安全。如果私服没有认证就能访问私服的话就不用配置了,配置方式也是settings.xml servers 标签下配置

maven settings 里面配置的信息
<settings>
...
<!--配置远程仓库认证信息-->
<servers>
<server>
<id>bbsnexus</id>
<username>licui</username>
<password>a111111</password>
</server>
</servers>
...
</settings> 上面代码我们配置了一个id为bbsnexus的远程仓库认证信息。Maven使用settings.xml文件中的servers元素及其子元素server配置仓库认证信息。认证用户名为licui,认证密码为a111111。这里的关键是id元素,id没有要求,随便定义,但是后面配置远程仓库的id必须和这里的id保持一致。正是这个id将认证信息与仓库配置联系在了一起。 接下来就是 profile中的 repository/pluginRepository 中的配置了,和下面的配置代码是一样的但是 id需要和仓库认证信息一致 bbsnexus

    1)在maven的setting配置.下载jar两种方式一个是在pom中配置适合一个项目的,另一个就是在maven的配置文件settings里面配置,适合多个项目,只在配置文件里面配置就行了

    在settings里面

<profile>
<!--profile的id-->
<id>dev</id>
<repositories>
<repository>
<!--仓库id,repositories可以配置多个仓库,保证id不重复-->
<id>nexus</id>
<!--仓库地址,即nexus仓库组的地址-->
<url>http://localhost:8081/nexus/content/groups/public/</url>
<!--是否下载releases构件-->
<releases>
<enabled>true</enabled>
</releases>
<!--是否下载snapshots构件-->
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<!-- 插件仓库,maven的运行依赖插件,也需要从私服下载插件 -->
<pluginRepository>
<!-- 插件仓库的id不允许重复,如果重复后边配置会覆盖前边 -->
<id>public</id>
<name>Public Repositories</name>
<url>http://localhost:8081/nexus/content/groups/public/</url>
</pluginRepository>
</pluginRepositories>
</profile> <!--使用profile定义仓库需要激活才可生效-->
<activeProfiles>
<activeProfile>dev</activeProfile>
</activeProfiles>

    或者在pom里面

<repositories>
<repository>
<!--仓库id,repositories可以配置多个仓库,保证id不重复 -->
<id>nexus</id>
<!--仓库地址,即nexus仓库组的地址 -->
<url>http://localhost:8081/nexus/content/groups/public/</url>
<!--是否下载releases构件 -->
<releases>
<enabled>true</enabled>
</releases>
<!--是否下载snapshots构件 -->
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>

    2)配置镜像

<mirror>
<!--镜像id,mirrors中可以配置多个镜像,保证id不重复-->
<id>nexus</id>
<!--mirrorOf指定仓库id,仓库组包括了id为 releases、snapshots、thirdparty 、central 的仓库,可以配置具体的id,如果镜像所有的仓库则设置为*-->
<mirrorOf>*</mirrorOf>
<!--镜像地址-->
<url>http://localhost:8081/nexus/content/groups/public/</url>
</mirror>

四.使用私服在网站上上传和下载jar包

  a.下载不说了,自己试试就行了

  b.上传jar包移步   https://www.cnblogs.com/tyhj-zxp/p/7605879.html