Nexus私服简介及搭建

nexus私服简介及搭建

  • 一 私服是什么?
    • 1.1 私服的作用
    • 1.2 Nexus下载
    • 1.3 使用Nexus搭建私服
      • 1.3.1可以修改默认端口
    • 1.4 登陆私服时密码不对的问题解决方案
  • 二私服的使用
    • 2.1 一些私服的专业词
    • 2.2 创建 Proxy Repository 代理仓库。
    • 2.3新建自定义的仓库
    • 2.4自定义组资源库
  • 三 在Maven 项目使用 Nexus 私服
    • 3.1创建角色
    • 3.2 创建用户
    • 3.3 maven setting.xml文件配置
    • 3.4部署的仓库是用POM中distributionManagement元素来定义的。
    • 3.5用来下载依赖是用POM中的repositories

一 私服是什么?

一个特殊的远程仓库,它是架设在局域网内的仓库服务,供局域网内的开发人员使用。 当Maven需要下载构建的使用, 它先从私服请求,如果私服上没有的话,则从外部的远程仓库下载,然后缓存在私服上 ,再为Maven的下载请求提供服务。
Nexus私服简介及搭建_第1张图片
Nexus私服简介及搭建_第2张图片

1.1 私服的作用

内网访问,节省外网带宽。
一次外网下载,内网所有用户就可以只下载私服缓存,加速 Maven 项目构建。
允许上传和下载私有库,并且不被外部访问,更加安全。
减少外部网络因素,提供项目构建的稳定性。
方便内部项目服务的依赖引用,而不需要其他项目的完整源代码。
有利于公共构件的维护
提高工作效率
等等

1.2 Nexus下载

我们选择使用最流行的开源 Maven 仓库管理软件 - Nexus,来快速搭建 Maven 私服
Nexus的官网
https://www.sonatype.com/products/repository-oss-download下载
百度网盘下载
链接:https://pan.baidu.com/s/1VoS6FYX-PeHaokv6popfXg
提取码:0haa

1.3 使用Nexus搭建私服

在 bin 目录下使用命令框窗口去输入 nexus /run 命令启动
启动成功
Nexus私服简介及搭建_第3张图片
浏览器上输入地址:http://localhost:8081/ 访问
Nexus私服简介及搭建_第4张图片

1.3.1可以修改默认端口

Nexus\nexus-3.20.1-01\etc\nexus-default.properties
Nexus私服简介及搭建_第5张图片

1.4 登陆私服时密码不对的问题解决方案

1 旧版本
账号 admin
密码 admin123
2 新版本
默认的UserName:admin
第一次登陆的时的密码是随机生成的在admin.password文件里面 复制密码登陆
Nexus私服简介及搭建_第6张图片
Nexus私服简介及搭建_第7张图片

二私服的使用

2.1 一些私服的专业词

hosted,本地仓库(也叫宿主仓库),通常我们会部署自己的构件到这一类型的仓库或者是第三方的包(如:oracel的)。
proxy,代理仓库,它们被用来代理远程的公共仓库,如maven中央仓库。
group,仓库组,用来合并多个hosted/proxy仓库,通常我们配置maven依赖仓库组
maven-central:maven中央库,默认从https://repo1.maven.org/maven2/拉取jar
maven-releases:私库发行版jar
maven-snapshots:私库快照(调试版本)jar
maven-public:仓库分组,把上面三个仓库组合在一起对外提供服务,在本地maven基础配置settings.xml中使用。
Hosted有三种方式:Releases、Snapshot、Mixed
Releases: 一般是已经发布的Jar包
Snapshot: 未发布的版本
Mixed:混合的

2.2 创建 Proxy Repository 代理仓库。

Nexus私服简介及搭建_第8张图片
Nexus私服简介及搭建_第9张图片

2.3新建自定义的仓库

点击Create repository,然后选择类型为 maven2(hosted)
Nexus私服简介及搭建_第10张图片
Nexus私服简介及搭建_第11张图片
Nexus私服简介及搭建_第12张图片

2.4自定义组资源库

Nexus私服简介及搭建_第13张图片

Nexus私服简介及搭建_第14张图片

三 在Maven 项目使用 Nexus 私服

3.1创建角色

一个角色下有多个用户 ,有了角色才能分配给用户权限
Nexus私服简介及搭建_第15张图片
Nexus私服简介及搭建_第16张图片

3.2 创建用户

Nexus私服简介及搭建_第17张图片
创建成功
Nexus私服简介及搭建_第18张图片

3.3 maven setting.xml文件配置

:定义jar包下载的Maven仓库、定义部署服务器。
但是某些配置例如username和password就保存在构建服务器中的settings.xml中。

id:server 的id,用于匹配distributionManagement库id,比较重要。
username, password:用于登陆此服务器的用户名和密码
privateKey, passphrase:设置private key,以及passphrase
filePermissions, directoryPermissions:当库文件或者目录创建后,需要使用权限进行访问。参照unix文件许可,如664和775
:表示镜像库,指定库的镜像,用于增加其他库。mirror相当于一个拦截器,它会拦截maven对remote repository的相关请求,把请求里的remote repository地址,重定向到mirror里配置的地址。
id,name:唯一的标志,用于区别镜像
url:镜像的url
mirrorOf:此镜像指向的服务id

在maven中配置私服,编辑setting.xml
  Maven的中央仓库地址默认是:https://repo.maven.apache.org/maven2/,可以通过修改settings.xml文件来修改默认的中央仓库地址:



<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
          
  	<localRepository>D:/maven/repositorylocalRepository>
	<interactiveMode>trueinteractiveMode>
    <offline>falseoffline>
	
	<servers>
		<server>
		    
         <id>maven-releasesid>
         <username>adminusername>
         <password>2321035705Mapassword>
     server>

     <server>
         <id>maven-snapshotsid>
         <username>adminusername>
         <password>2321035705Mapassword>
     server>

     <server>
         <id>myself_groupid>
         <username>adminusername>
         <password>2321035705Mapassword>
     server>
	servers>
	
	<mirrors>
		
		<mirror>  
	      <id>alimavenid>  
	      <name>aliyun mavenname>  
	      <url>http://maven.aliyun.com/nexus/content/groups/public/url>  
	      <mirrorOf>centralmirrorOf>          
	    mirror>
    mirrors>

	
	<profiles>
		<profile>
		   <id>nexusid>
			    <activation>
			     <activeByDefault>trueactiveByDefault>
				 <jdk>1.8jdk>
			    activation>
			    <repositories>
					
				    <repository>
						<id>nexus-groupid>
						<url>http://localhost:8081/repository/nexus-group/url>
						
						<releases>
							<enabled>trueenabled>
						releases>
						<snapshots>
							<enabled>trueenabled>
						snapshots>
					repository>
					
					
					
				repositories>      
				<pluginRepositories>
					
					
					<pluginRepository>
						<id>nexus-groupid>
						<url>http://localhost:8081/repository/nexus-group/url>
					
						<releases>
							<enabled>trueenabled>
						releases>
						<snapshots>
							<enabled>trueenabled>
					   snapshots>
					pluginRepository>
				pluginRepositories>
			profile>

			<profile>
		     <id>jdk-1.8id>
			 <activation>
			     <activeByDefault>trueactiveByDefault>
				 <jdk>1.8jdk>
			 activation>
			 <properties>
			     <maven.compiler.source>1.8maven.compiler.source>
				 <maven.compiler.target>1.8maven.compiler.target>
				 <maven.compiler.compilerVersion>1.8maven.compiler.compilerVersion>
			 properties>
		profile>
	profiles>
	
	
	<activeProfiles>
	
		<activeProfile>nexusactiveProfile>
		<activeProfile>jdk-1.8activeProfile>  
	activeProfiles>
settings>

3.4部署的仓库是用POM中distributionManagement元素来定义的。

准备发布的jar 运行:mvn deploy即可

  <distributionManagement>
    <repository>
        <id>maven-releasesid>
        <name>Nexus Release Repositoryname>
        <url>http://localhost:8081/repository/maven-releases/url>
    repository>
    <snapshotRepository>
        <id>maven-snapshotsid>
        <name>Nexus Snapshot Repositoryname>
        <url>http://localhost:8081/repository/maven-snapshots/url>
    snapshotRepository>
distributionManagement>

3.5用来下载依赖是用POM中的repositories

<repositories>
  <repository>
    <id>nexusid>
    <url>http://121.4.207.231:8081/repository/maven-public/url>
    <releases>
      <enabled>trueenabled>
    releases>
    <snapshots>
    <enabled>trueenabled>
    snapshots>
  repository>
repositories>

你可能感兴趣的:(java,maven)