06.maven私服的安装、启动、使用

私服的安装、启动、使用

公司在自己的局域网内搭建自己的远程仓库服务器,称为私服,私服服务器即是公司内部的 maven 远程仓库,每个员工的电脑上安装 maven 软件并且连接私服服务器,员工将自己开发的项目打成 jar 并发布到私服服务器,其它项目组从私服服务器下载所依赖的构件(jar)。私服还充当一个代理服务器,当私服上没有 jar 包会从互联网中央仓库自动下载;


私服.png

一、搭建私服环境

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

1). 私服的安装和启动

为了好演示,以下演示是基于win10环境,方便学习;公司中会有专门的安装在linux环境下的私服服务器,

  1. 安装包 nexus-2.12.0-01-bundle.zip 解压到一个文件夹即可

  2. 关键文件

    • nexus-2.12.0-0
      • bin
        • nexus.bat 服务安装/启动/卸载/文件
      • conf
        • nexus.properties 配置监听端口文件 默认8081
    • sonatype-work - 私服仓库【启动服务程序后才能看到内部】
      • nexus
        • storage -- 存放各个仓库,查找顺序
          • releases 发布版仓库
          • snapshots 测试版仓库
          • thirdparty 第三方
          • central 中央仓库
          • apache-snapshots apache中央仓库
  3. 安装服务:确保8081没有被占用

管理员方式打开终端 cd 到 nexus.bat 文件所在目录下

nexus.bat install -- 安装服务;nexus 服务被安装
  1. 卸载服务
nexus.bat uninstall -- 移除服务
  1. 启动服务
nexus.bat start -- 启动程序
-- 或者打开win服务列表,选择 nexus ,选择启动

2). 私服的配置访问

  1. nexus.properties 配置文件
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 运行程序目录
  1. 浏览器访问 访问 localhost:8081/nexus
    • 用户名:admin
    • 密码: admin123

二、仓库介绍


私服管理系统.png

1). 仓库类型

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

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

2). 仓库分类

  1. apache-snapshots :代理仓库存储 snapshots 构件,代理地址 https://repository.apache.org/snapshots/
  2. central-m1 :virtual类型仓库,兼容Maven1版本的jar或者插件
  3. releases : 本地仓库,存储releases构件。
  4. snapshots : 本地仓库,存储snapshots构件。
  5. thirdparty :第三方仓库
  6. Public Repositories :仓库组

三、私服的应用

1). 案例,将ssm_dao模块代码上传到私服

  1. 配置本地安装的maven的config下的settings.xml文件;文件中的注释中有配置示例,Ctrl + F 搜索关键的标签,配置到与之对应的位置即可。
<server>
    <id>releases</id>
    <username>admin</username>
    <password>admin123</password>
</server>
<server>
    <id>snapshots</id>
    <username>admin</username>
    <password>admin123</password>
</server>

releases 连接发布版本项目仓库; snapshots 连接测试版本项目仓库

  1. dao_ssm中的pom.xml文件中配置
<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> 对应!

  1. 执行dao插件 deploy 命令: 本地仓库会上传一份因为deploy执行之前执行了install (生命周期的嵌套) 命令,远程仓库会上传一份

2). 从远程仓库下载配置

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

  1. 配置 本地安装的maven的config目录下的settings.xml文件;文件中的注释中有配置示例,Ctrl + F 搜索关键的标签,配置到与之对应的位置即可。

注意,如果配置了阿里镜像,下面的配置不会生效,演示时将阿里镜像注释掉,演示完了取消注释!

<!-- 下载jar包配置 -->
<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>
  1. 同上1,配置profile
<activeProfiles>
    <activeProfile>dev</activeProfile>
</activeProfiles>
  1. 配置 web pox.xml 下载路径
<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>
  1. 测试,将本地仓库的dao包删除,执行web 的tomcat7:run命令,看控制台日志
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容