maven-nexus学习总结

一.nexus下载及配置

1.1 下载 nexus

Nexus 是 Maven 仓库管理器,通过 nexus 可以搭建 maven 仓库,同时 nexus 还提供强大的仓库管理功能,构件搜索功能等。

下载Nexus, 下载地址:http://www.sonatype.org/nexus/archived/

附:https://www.sonatype.com/download-sonatype-trial?submissionGuid=a816d909-19e8-4a16-a7d1-4fe357a2713b

 1.2 安装nexus

解压nexus-2.14.11-01-bundle.zip,本教程将它解压在 F 盘,进入bin 目录:

 

安装成功在服务中查看有nexus 服务:

1.3 卸载 nexus(nexus.bat uninstall)

查看window 服务列表nexus 已被删除。

nexus.bat unistall  卸载

nexus.bat install 安装

nexus.bat start  启动

1.4 启动 nexus

方法 1:

cmd 进入bin 目录,执行nexus.bat start

方法 2:

直接启动nexus 服务

查看nexus 的配置文件conf/nexus.properties

# Jetty section

application-port=8081 # nexus 的访问端口配置

application-host=0.0.0.0  # nexus 主机监听配置(不用修改)

nexus-webapp=${bundleBasedir}/nexus # nexus 工程目录

nexus-webapp-context-path=/nexus # nexus 的web 访问路径

# Nexus section

nexus-work=${bundleBasedir}/../sonatype-work/nexus # nexus 仓库目录

runtime=${bundleBasedir}/nexus/WEB-INF # nexus 运行程序目录

访问:

http://localhost:8081/nexus

 

 使用Nexus 内置账户admin/admin123 登陆:

点击右上角的Log in,输入账号和密码 登陆

 

登陆成功:

1.5  仓库类型

查看nexus 的仓库:

nexus 的仓库有 4 种类型:

  1. hosted,宿主仓库,部署自己的 jar 到这个类型的仓库,包括 releases 和 snapshot 两部分,Releases 公司内部发布版本仓库、 Snapshots 公司内部测试版本仓库
  2. proxy,代理仓库,用于代理远程的公共仓库,如 maven 中央仓库,用户连接私服,私服自动去中央仓库下载jar 包或者插件。
  3. group,仓库组,用来合并多个hosted/proxy 仓库,通常我们配置自己的maven 连接仓库组。
  4. virtual(虚拟):兼容Maven1 版本的jar 或者插件

nexus 仓库默认在sonatype-work 目录中:

  • central:代理仓库,代理中央仓库

 

  • apache-snapshots:代理仓库

存储 snapshots 构件,代理地址https://repository.apache.org/snapshots/

  • central-m1virtual 类型仓库,兼容Maven1 版本的jar 或者插件
    • releases:本地仓库,存储releases 构件。
    • snapshots:本地仓库,存储snapshots 构件。
    • thirdparty:第三方仓库
    • public:仓库组

二. 将项目发布到私服

2.1 需求

企业中多个团队协作开发通常会将一些公用的组件、开发模块等发布到私服供其它团队 或模块开发人员使用。

2.2 配置

第一步:需要在客户端即部署 ssm_dao 工程的电脑上配置 maven 环境,并修改 settings.xml

文件,配置连接私服的用户和密码 。

此用户名和密码用于私服校验,因为私服需要知道上传的账号和密码是否和私服中的账号和 密码一致。

<server>
    <id>releases</id>
    <username>admin</username>
    <password>admin123</password>
</server>
<server>
    <id>snapshots</id>
    <username>admin</username>
    <password>admin123</password>
</server>

releases 连接发布版本项目仓库

snapshots 连接测试版本项目仓库

 第二步: 配置项目pom.xml

配置私服仓库的地址,本公司的自己的jar  包会上传到私服的宿主仓库,根据工程的版本号决定上传到哪个宿主仓库,如果版本为 release 则上传到私服的 release 仓库,如果版本为snapshot 则上传到私服的 snapshot 仓库

<distributionManagement>
	<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>

 注意:pom.xml 这里<id> 和 settings.xml 配置 <id> 对应!

2.3 测试

将项目dao 工程打成jar 包发布到私服:

1、首先启动nexus

2、对 ssm_dao 工程执行deploy 命令

根据本项目pom.xml中version 定义决定发布到哪个仓库,如果version 定义为snapshot, 执行deploy 后查看nexus 的snapshot 仓库,如果version 定义为release 则项目将发布到nexus 的release 仓库,本项目将发布到 snapshot 仓库:

也可以通过http 方式查看: 

三. 从私服下载 jar 

3.1 管理仓库组

nexus中包括很多仓库,hosted 中存放的是企业自己发布的jar包及第三方公司的jar包,proxy 中存放的是中央仓库的 jar,为了方便从私服下载 jar 包可以将多个仓库组成一个仓库组,每个工程需要连接私服的仓库组下载jar 包。

打开nexus 配置仓库组,如下图:

上图中仓库组包括了本地仓库、代理仓库等。

 3.2 在 setting.xml 中配置仓库

在客户端的 setting.xml 中配置私服的仓库,由于 setting.xml 中没有 repositories 的配置标签需要使用profile 定义仓库

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

配置成功后通过 eclipse 查看有效 pom,有效 pom 是 maven 软件最终使用的 pom 内容,程序员不直接编辑有效pom,打开有效pom

 有效pom 内容如下:

下边的pom 内容中有两个仓库地址,maven 会先从前边的仓库的找,如果找不到 jar 包再从下边的找,从而就实现了从私服下载jar 包。

<repositories>
<repository>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
<id>public</id>
<name>Public Repositories</name>
<url>http://localhost:8081/nexus/content/groups/public/</url>
</repository>
<repository>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>central</id>
<name>Central Repository</name>
<url>https://repo.maven.apache.org/maven2</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>public</id>
<name>Public Repositories</name>
<url>http://localhost:8081/nexus/content/groups/public/ </url>
</pluginRepository>
<pluginRepository>
<releases>
<updatePolicy>never</updatePolicy>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>central</id>
<name>Central Repository</name>
<url>https://repo.maven.apache.org/maven2</url>
</pluginRepository>
</pluginRepositories>

3.3 测试从私服下载 jar 包

测试 1:局域网环境或本地网络即可

在 ssm_service 工程中添加以上配置后,添加 ssm_dao 工程的依赖,删除本地仓库中 ssm_dao

工程,同时在eclipse 中关闭 ssm_dao 工程。观察控制台:

项目先从本地仓库找 ssm_dao,找不到从私服找,由于之前执行 deploy 将 ssm_dao 部署到私服中,所以成功从私服下载ssm_dao 并在本地仓库保存一份。

如果此时删除私服中的 ssm_dao,执行 update project 之后是否正常? 如果将本地仓库的 ssm_dao 和私服的 ssm_dao 全部删除是否正常? 测试 2:需要互联网环境

在项目的 pom.xml 添加一个依赖,此依赖在本地仓库和私服都不存在,maven 会先从本地仓库找,本地仓库没有再从私服找,私服没有再去中央仓库下载,jar   包下载成功在私服、本地仓库分别存储一份。

四. 把第三方 jar 包放入本地仓库或私服

4.1 导入本地库

随便找一个jar 包测试,可以先CMD 进入到jar 包所在位置,运行

mvn install:install-file -DgroupId=com.alibaba -DartifactId=fastjson -Dversion=1.1.37

-Dfile= fastjson-1.1.37.jar -Dpackaging=jar

4.2 导入私服

需要在maven 软件的核心配置文件settings.xml 中配置第三方仓库的 server 信息

<server>
    <id>thirdparty</id>
    <username>admin</username>
    <password>admin123</password>
</server>

 

才能执行一下命令

mvn deploy:deploy-file -DgroupId=com.alibaba -DartifactId=fastjson -Dversion=1.1.37
-Dpackaging=jar -Dfile=fastjson-1.1.37.jar
-Durl=http://localhost:8081/nexus/content/repositories/thirdparty/
-DrepositoryId=thirdparty

4.3 参数说明

DgroupId 和DartifactId 构成了该 jar 包在 pom.xml 的坐标,项目就是依靠这两个属性定位。自己起名字也行。

Dfile 表示需要上传的jar 包的绝对路径。

Durl 私服上仓库的位置,打开nexus——>repositories 菜单,可以看到该路径。

DrepositoryId 服务器的表示 id,在 nexus 的configuration 可以看到。

Dversion 表示版本信息,

 

关于jar 包准确的版本:

 

包的名字上一般会带版本号,如果没有那可以解压该包,会发现一个叫 MANIFEST.MF 的文件,

 

这个文件就有描述该包的版本信息。

 

比如Specification-Version: 2.2 可以知道该包的版本了。

 

上传成功后,在nexus 界面点击 3rd party 仓库可以看到这包。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值