Maven私服使用

私服搭建

下载私服

Nexus 是 Maven 仓库管理器, 通过 nexus 可以搭建 maven 仓库,同时 nexus 还提供强大的仓库管理功能,构件搜索功能等。下载 Nexus, 下载地址:http://www.sonatype.org/nexus/archived/

安装私服

解压 nexus-2.12.0-01-bundle.zip,cmd 进入 bin 目录,执行 nexus.bat install
安装成功在服务中查看有 nexus 服务

卸载私服

cmd 进入 nexus 的 bin 目录,执行: nexus.bat uninstall

启动私服

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

访问私服

查看 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 运行程序目录北京市昌平区建材城西路金燕龙办公楼一层 电话:400-618-9090
访问:
http://localhost:8081/nexus

使用 Nexus 内置账户 admin/admin123 登陆:
点击右上角的 Log in,输入账号和密码 登陆

仓库类型

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

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

项目发部到私服

公司项目有些公用模块,需要发部到私服中以便拉取

配置

  1. 客户端maven配置文件setting.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 连接测试版本项目仓库

  2. 修改项目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 这里 和 settings.xml 配置 对应!

从私服下载依赖

没有配置 nexus 之前,如果本地仓库没有,去中央仓库下载,通常在企业中会在局域网内部署一台私服服务器, 有了私服本地项目首先去本地仓库找 jar,如果没有找到则连接私服从私服下载 jar 包,如果私服没有 jar 包私服同时作为代理服务器从中央仓库下载 jar 包,这样做的好处是一方面由私服对公司项目的依赖 jar 包统一管理,一方面提高下载速度, 项目连接私服下载 jar 包的速度要比项目连接中央仓库的速度快的多

管理仓库组

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

在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>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值