Maven快速搭建Nexus私服仓库

star2017 1年前 ⋅ 748 阅读

需要发布一些公共组件jar时,需要搭建 Maven 私服方使本地添加私有依赖。

Maven 应用服务器 Sonatype nexus 官网:https://www.sonatype.com/

下载

下载应用服器安装包,Ubuntu系统,下载tar.gz包,上传到 Linux系统。
安装包:nexus-3.12.0-01-unix.tar.gz
官方文档:https://help.sonatype.com/repomanager3

安装

  1. 安装Java环境
  2. 将安装包解压到安装目录

#tar zxvf nexus-3.12.0-01-unix.tar.gz -C nexus/

  1. 给 Nexus 指定Java环境

cd /home/download/nexus/nexus-3.12.0-01/bin
vim nexus

#添加内容
INSTALL4J_JAVA_HOME_OVERRIDE=/usr/local/java/jdk1.8.0_172

运行

第一次运行会执行一系列的初始化操作

#cd /home/download/nexus/nexus-3.12.0-01/bin

#./nexus run
初始化完成运行成功后,会看到有success的标志

访问

URL:http://ip:port ,默认端口是:8081;
默认管理员账号:admin, 密码:admin123

  1. 管理员账号登录后,点击设置,在左侧导航栏找到【User】添加一个用户,用于发布时需要的账号密码验证。
  2. 直接使用默认就已创建的Mave仓库,分别点击进【maven-releases】和【maven-snapshots】仓库,在底部修改发布更新类型为【Allow redeploy】。

发布配置

  1. 在工在的pom.xml文件, <project>标签里添加如下配置
    发布配置,配置发布版仓库和快照版仓库

    <distributionManagement>
        <repository>
            <id>hz-repo</id>
            <url>http://10.0.1.91:8081/repository/maven-releases/</url>
        </repository>
        <snapshotRepository>
            <id>hz-repo</id>
            <url>http://10.0.1.91:8081/repository/maven-snapshots/</url>
        </snapshotRepository>
    </distributionManagement>
    

    在Maven的settings.xml文件添加发布 jar 的账号密码

    <servers>   
        <server>
            <id>hz-repo</id>
            <username>hzdsj</username>
            <password>123456</password>
        </server>
    </servers>
    

使用私有仓库

在Maven的settings.xml文件镜像来源添加公司的仓库,可配多个<mirror>

<mirrors>    
    <!-- 公司仓库 --> 
    <mirror>
      <id>nexus</id>
      <mirrorOf>*</mirrorOf>
      <url>http://10.0.1.91:8081/repository/maven-public/</url>
    </mirror>    
</mirrors>

其它配置

主要参考官方文档,官方文档提供了如何将应用设置作为为Linux服务启用、区分发布仓库、快照仓库、仓库组,创建代理仓库等等。
官方quick-start-guide:https://help.sonatype.com/learning/repository-manager-3/proxying-maven-and-npm-quick-start-guide
也可参考网上博客资料。

<!-- maven settings -->

<settings>
  <mirrors>
    <mirror>
      <!--This sends everything else to /public -->
      <id>nexus</id>
      <mirrorOf>*</mirrorOf>
      <url>http://localhost:8081/repository/maven-proxy/</url>
    </mirror>
  </mirrors>

  <profiles>
    <profile>
      <id>nexus</id>
      <!--Enable snapshots for the built in central repo to direct -->
      <!--all requests to nexus via the mirror -->
      <repositories>
        <repository>
          <id>central</id>
          <url>http://central</url>
          <releases><enabled>true</enabled></releases>
          <snapshots><enabled>true</enabled></snapshots>
        </repository>
      </repositories>
     <pluginRepositories>
        <pluginRepository>
          <id>central</id>
          <url>http://central</url>
          <releases><enabled>true</enabled></releases>
          <snapshots><enabled>true</enabled></snapshots>
        </pluginRepository>
      </pluginRepositories>
    </profile>
  </profiles>

  <activeProfiles>
    <!--make the profile active all the time -->
    <activeProfile>nexus</activeProfile>
  </activeProfiles>
</settings>

可以参考:Maven私服Nexus3.x环境构建操作记录

更多内容请访问:IT源点

相关文章推荐

全部评论: 0

    我有话说: