💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
[TOC] # 简介 公司在自己的局域网内搭建自己的远程仓库服务器,称为私服,私服服务器即是公司内部的 maven 远程仓库,每个员工的电脑上安装 maven 软件并且连接私服服务器,员工将自 己开发的项目打成 jar 并发布到私服服务器,其它项目组从私服服务器下载所依赖的构件 (jar)。 私服还充当一个代理服务器,当私服上没有 jar 包会从互联网中央仓库自动下载,如下 图: ![](https://img.kancloud.cn/3e/7b/3e7b5954619100317f33a2b9f2b592d8_435x387.png) 下载 Nexus, 下载地址:http://www.sonatype.org/nexus/archived/ [https://help.sonatype.com/repomanager3/download](https://help.sonatype.com/repomanager3/download) windows要 cmd 进入 bin 目录,执行 `nexus.bat install` cmd 进入 bin 目录,执行 `nexus.bat start` # 配置文件 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 运行程序目录 ~~~ wrapper.conf这个文件下面改下java的这个 ~~~ wrapper.java.command=/Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home/bin/java ~~~ 默认密码 admin和admin123 [http://127.0.0.1:8081/nexus](http://127.0.0.1:8081/nexus) ![](https://img.kancloud.cn/9b/ad/9badeb1170202484e88a7637c70a52f4_324x214.png) 这个里面可以改密码 # 仓库类型 nexus 的仓库有 4 种类型: ![](https://img.kancloud.cn/16/58/1658f87ed1301919d5ced0d8bb97f7ae_360x310.png) 1. hosted,宿主仓库,部署自己的 jar 到这个类型的仓库,包括 releases 和 snapshot 两部 分,Releases公司内部发布版本仓库、 Snapshots 公司内部测试版本仓库 2. proxy,代理仓库,用于代理远程的公共仓库,如 maven 中央仓库,用户连接私服,私服自动去中央仓库下载 jar 包或者插件。 3. group,仓库组,用来合并多个 hosted/proxy 仓库,通常我们配置自己的 maven 连接仓库组。 4. virtual(虚拟):兼容Maven1 版本的jar或者插件 nexus 仓库默认在 sonatype-work 目录中: ![](https://img.kancloud.cn/94/a5/94a5d1c0a8cadc9196b76aa269b79296_193x228.png) * central:代理仓库,代理中央仓库 ![](https://img.kancloud.cn/87/0a/870a682b6b7b606ded8a6e6d9ece75f7_710x426.png) * apache-snapshots:代理仓库 存储 snapshots 构件,代理地址 https://repository.apache.org/snapshots/  central-m1:virtual 类型仓库,兼容 Maven1 版本的 jar 或者插件  releases:本地仓库,存储releases构件。 * snapshots:本地仓库,存储snapshots构件。 * thirdparty:第三方仓库 * public:仓库组 ![](https://img.kancloud.cn/99/6e/996ee21490107cc8c357df47729ec404_1142x1406.png) # 发布到私服 ## 需求 企业中多个团队协作开发通常会将一些公用的组件、开发模块等发布到私服供其它团队 或模块开发人员使用。 本例子假设多团队分别开发 ssm\_dao、ssm\_service、ssm\_web,某个团队开发完在 ssm\_dao 会将 ssm\_dao 发布到私服供 ssm\_service 团队使用,本例子会将 ssm\_dao 工程打成 jar 包发布到私服。 ![](https://img.kancloud.cn/4c/ab/4cabba5b86274e8ef9cf6aed987509b6_636x207.png) ## 配置 第一步:需要在客户端即部署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 连接测试版本项目仓库 ![](https://img.kancloud.cn/4e/d3/4ed321f1f6c6efe853a48ae86a5ad125_735x81.png) 第二步: 配置项目 pom.xml 配置私服仓库的地址,本公司的自己的 jar 包会上传到私服的宿主仓库,根据工程的版本号 决定上传到哪个宿主仓库,如果版本为 release 则上传到私服的 release 仓库,如果版本为 snapshot 则上传到私服的 snapshot 仓库 url是私服网页上的对应仓库的url ![](https://img.kancloud.cn/e0/af/e0af9e8b24d74ab206585f239ca5b33b_1454x258.png) ~~~ <distributionManagement> <repository> <id>releases</id> <name>Nexus Release Repository</name> <url>http://localhost:8081/nexus/content/repositories/releases/</url> </repository> <snapshotRepository> <id>snapshots</id> <name>Nexus Snapshot Repository</name> <url>http://localhost:8081/nexus/content/repositories/snapshots/</url> </snapshotRepository> </distributionManagement> ~~~ 注意:pom.xml 这里 和 settings.xml 配置对应 你要在dao中把包上传就在dao中deploy的 根据本项目 pom.xml 中 version 定义决定发布到哪个仓库, 如果 version 定义为 snapshot, 执行 deploy 后查看 nexus 的 snapshot 仓库, 如果 version 定义为 release 则项目将发布到 nexus 的 release 仓库,本项目将发布到 release 仓库. **测试** 可以通过 http 方式查看 ![](https://img.kancloud.cn/b3/0a/b30a149b147f8e8bf875c1442537f295_706x222.png) # 从私服下载jar包 ## 需求 没有配置 nexus 之前,如果本地仓库没有,去中央仓库下载,通常在企业中会在局域网 内部署一台私服服务器,有了私服本地项目首先去本地仓库找 jar, 如果没有找到则连接私 服从私服下载 jar 包,如果私服没有 jar 包私服同时作为代理服务器从中央仓库下载 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> ~~~ ![](https://img.kancloud.cn/01/f6/01f6e1005b56e191fa574ca1466d7e1c_919x402.png) # 第三方jar包安装到本地或私服 1. 导入本地库 2种方式 ~~~ ----进入jar包所在目录运行 mvn install:install-file -DgroupId=com.alibaba -DartifactId=fastjson -Dversion=1.1.37 -Dfile=fastjson-1.1.37.jar -Dpackaging=jar ----打开cmd直接运行 mvn install:install-file -DgroupId=com.alibaba -DartifactId=fastjson -Dversion=1.1.37 -Dpackaging=jar -Dfile=C:\my_java\fastjson-1.1.37.jar ~~~ 然后去仓库看下 2. 导入私服 需要在 maven 软件的核心配置文件 settings.xml 中配置第三方仓库的 server 信息 要能登录到第三方 ~~~ <server> <id>thirdparty</id> <username>admin</username> <password>admin123</password> </server> ~~~ 才能执行一下命令 2种方式 ~~~ ----进入jar包所在目录运行 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 ----打开cmd直接运行 mvn deploy:deploy-file -DgroupId=com.alibaba -DartifactId=fastjson -Dversion=1.1.37 -Dpackaging=jar -Dfile=C:\my_java\fastjson-1.1.37.jar -Durl=http://localhost:8081/nexus/content/repositories/thirdparty/ -DrepositoryId=thirdparty ~~~ **参数说明** 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 仓库可以看到这包。 # 多个私服切换 setting.xml 家和公司两套 activeProfiles属性指定id ![](https://img.kancloud.cn/6d/5e/6d5eb1b7628df7be8f5a484a07ef934f_584x488.png) # nexus3界面 > Repository:仓储系统 * Blob Stores:这个可以看成是存储空间,管理空间,主要是进行管理Repositorles中仓库的,默认有一个default存储,可以创建存储空间 * Reposltorles:仓库,这就是我们所说的仓库的概念了,仓库一共分为三种类型,宿主(hosted),代理(proxy)和分组(group) * 宿主:第三方构建所能上传的仓库,可以创建多个name属性不同的宿主仓库,用于管理不同的项目 * 代理:访问网络nexus服务器,用途:打个比方的说,想使用Junit包,但是分组仓库中没有,就需要去访问网络上的中央仓库中下载到你的代理仓库.(因为实际从网络仓库中download的是代理仓库,ps:默认访问网络中仓库的是游客,这个无需去计较了!) * 分组:分组仓库的主要作用就是将你的宿主仓库和代理仓库进行连接,比如说不同的项目创建的不通的宿主仓库,却使用了相同的代理仓库.这种就可以使用分组仓库进行连接!!!!!!! * Content Selectors:未知,不了解.从大概的意思上就是nexus组件的使用,和创建自己的组件 > Security:安全管理,账号的权限,SSL证书的配置都在这里 * Prlvileges:每种仓库的增删改查,浏览等权限,可以单独进行配置,被使用于Roles * Roles:角色权限,相当于一个权限模板,提供给User使用!默认有两种权限,一个是游客(anonymous),一个是管理员(admin) * Users:用户,这个就是访问nexus私服的用户了,默认同样是有两个,一个是管理员(admin),一个是游客(anonymous),管理员继承了Roles中admin权限模板,具有所有管理权限,而游客只有浏览权限 * Anonymous:游客(或者叫匿名用户) 请忽略他 * LDAP:通讯录? 不了解,请忽略他 * Realms:领域?什么领域? 不了解,请忽略他 * SSL Certlficates:SSL证书配置 > Support:支持,分析系统运行状态,系统信息,日志查看都在这里 * Analytlcs:分析你的组组件在如何的使用Nexus * Logging:系统各项组件日志 * Metrlcs:内存,线程,磁盘目前使用图 * Support ZIP:选择Support中各种分析文件打成ZIP文件,其中包括系统的运行状态啊,JVM啊,配置文件啊,日志啊等等之类的! * System InforMation:系统信息,这个比较常用,可以看Nexus运行状态,版本信息,配置文件,组件信息等..... -- System:系统设置 * Bundles:系统插件具体信息的查看,我是这么理解的!有问题,请留言! * Capabilities:管理和配置你的Nexus具有什么能力,正常使用默认的就好! * Email Server:Email服务的配置,用于发邮件(在系统出现问题时) * HTTP: 整体传输协议的配置,默认的就好! * Licensing:许可证配置,SSL许可证? * Recent Connections:7天内访问系统信息,都谁访问过nexus服务器 * Nodes:节点管理? * Tasks:系统任务 ## 仓库创建 登录管理界面Administration->Repository->Repositories->左上角Create repository(创建仓库) 我们创建一个Maven2类型的宿主(hosted)仓库(Maven2(hosted)),以下步骤下图所示 ![](https://img.kancloud.cn/af/38/af383edceefaa08f0531c9e83eeaf926_672x626.png) 上图提到了Storage仓储的这点,那么下图就去创建一个仓储,仓储是可以创建多个的,每个仓储可以存储不通类型仓库的数据\_ 创建仓储:Repository->Blob Stores->Create blob store,下图所示 ![](https://img.kancloud.cn/94/07/94075ff0966b13fc7f6bfa3d4f1e9e56_751x293.png) 创建步骤重述:先创建仓储,在创建仓库,创建仓库时选择你创建的仓储,就可以了(一个仓库只能选择一个仓储) ## 权限管理 Nexus的权限控制采用的是典型的权限(Privilege)和角色(Role)机制。角色与权限是一对多的关联,用户与角色也是一对多的关联。由于Nexus的主要功能就是管理Maven仓库,所以其权限(Privilege)主要是对仓库进行CRUD(增查改删)操作的权限。作为Nexus的用户,我们能进行管理的就是对仓库的增删改查权限,其他的例如UI权限等是Nexus内建的,用户无法增删改,这个在下一节会讲到。 单个权限(Privilege)所能控制的级别是一个仓库(Repository,包括Group)中存储路径匹配指定正则表达式的一组项目(Artifact)的增删查改(create,delete,read,update)中的某一个操作。正因为其使用了正则表达式来选定要控制的Artifact所以非常灵活。 Nexus的权限分为三类: 1. 应用权限(Application Privilege):主要是用户的UI操作权限和系统管理权限,例如通过UI登录、进行用户管理等。 2. 仓库目标权限(Repository Target Privilege):我们作为用户能自定义的唯一一种权限,就是对仓库中的项目的CRUD操作权限。 3. 仓库浏览权限(Repository View Privilege):通过UI界面浏览一个仓库的权限,在创建每个仓库时,Nexus会自动创建一个对应该仓库的View权限,拥有该仓库的view权限才能在系统的Repositories视图中看到该仓库。 以上三类权限中,应用权限和仓库浏览权限两种是由Nexus创建的,我们并不能对这些权限进行增删改操作,只能选择使用或不使用它们。我们能自定义的权限只有仓库目标权限 --- 创建用户:Security->Users->左上角Create user,创建详细如图所示: ![](https://img.kancloud.cn/cc/a4/cca4c546e9ab8f777cf57bbe2dce0164_649x659.png) 创建用户需要一个用户所属的模板,可以继承多个模板,相当于同时有了多种模板所具有权限,下面进行创建一个模板 创建角色模板:Security->Roles->Create Role->Nexus role ![](https://img.kancloud.cn/bd/9c/bd9c28b152182d1b66823eddfb756e93_510x549.png) Privileges: 管理仓库的多种权限,以一个我创建的仓库 aaa截图说明: ![](https://img.kancloud.cn/4a/85/4a85f6abf72a99090479824111176312_1457x750.png) 强调:权限管理步骤复述,需要先具有一个角色模板(roles),同时也需要一个Privileges(当构建仓库的时候,默认会构建完成),之后创建User,链接所属Role就可以了* ## 访问 setting.xml 在`<mirrors></mirrors>`标签中添加`mirror`子节点: ~~~xml <mirror> <id>maven-private</id> <mirrorOf>maven-private</mirrorOf> <url>https://repo.liming.pub/nexus/repository/maven-private/</url> </mirror> ~~~ 或者在`<prifiles></prifiles>`标签中增加增加profile子节点: ~~~xml <profile> <id>maven-private</id> <repositories> <repository> <id>maven-private</id> <url>https://repo.liming.pub/nexus/repository/maven-private/</url> <snapshots> <updatePolicy>always</updatePolicy> </snapshots> </repository> </repositories> </profile> ~~~ 在`<servers></servers>`标签中添加`server`子节点: ~~~xml <server> <id>maven-private</id> <username>用户名</username> <password>密码</password> </server> ~~~ 工程pom中增加发布仓库配置 ~~~xml <profiles> <profile> <id>maven-protect</id> <distributionManagement> <repository> <id>maven-protect-releases</id> <url>https://repo.liming.pub/nexus/repository/maven-protect-releases/</url> </repository> <repository> <id>maven-protect-snapshots</id> <url>https://repo.liming.pub/nexus/repository/maven-protect-snapshots/</url> </repository> </distributionManagement> </profile> <profile> <id>maven-private</id> <distributionManagement> <repository> <id>maven-private-releases</id> <url>https://repo.liming.pub/nexus/repository/maven-private-releases/</url> </repository> <repository> <id>maven-private-snapshots</id> <url>https://repo.liming.pub/nexus/repository/maven-private-snapshots/</url> </repository> </distributionManagement> </profile> </profiles> ~~~ 在setting.xml的`<servers></servers>`标签中添加`server`子节点 ~~~xml <server> <id>maven-private-snapshots</id> <username>用户名</username> <password>密码</password> </server> <server> <id>maven-private-releases</id> <username>用户名</username> <password>密码</password> </server> ~~~ ## 部署命令 通过maven 的-P参数激活指定的profile 参数的值是profile的id,多个profile以逗号分割,如果不想激活某个默认的profile,就在它的id前加个! ~~~ mvn deploy -Pmaven-private ~~~ 查看当前生效的profile: ~~~ mvn help:active-profiles -Pmaven-private ~~~ # maven离线使用 要在无网络访问的环境下使用maven,由于私有仓库无法同步中央仓库,这种情况下,可以将本地仓库整个打包拷贝过去。注意要将各个目录下的`_remote.repositories`文件删除,否则maven也不会使用本地仓库的jar。 删除命令如下: ~~~ find ~/.m2/ -name _maven.repositories | xargs rm ~~~