Linux 搭建maven私服及上传代码到私服

为什么要搭建私服

1、从远程仓库上获得的资源比较慢;
2、当出现网络问题或者其它问题时,下载到不完整资源导致下载的资源不可用;
3、一些公共组件不希望第三方获取到源码,可以将编译好的jar上传到私服给第三方使用;

安装Nexus

安装

  1. 准备工作:安装jdk1.8.安装步骤: https://blog.csdn.net/zjhcxdj/article/details/99955106
  2. 下载地址:https://www.sonatype.com/download-oss-sonatype0,选择UNIX版本。下载不下来可以到这里下载:https://download.csdn.net/download/zjhcxdj/11956505
  3. 上传latest-unix.tar.gz安装包到linux服务器上,到安装包所在的目录下,解压安装包到/usr/local/:tar -zxvf latest-unix.tar.gz -C /usr/local/.
  4. 到解压目录(/usr/local/)下建立软连接:sudo ln -s nexus-3.19.1-01 nexus.

启动Nexus

到/usr/local/nexus/bin目录下,执行:./nexus {start|stop|run|run-redirect|status|restart|force-reload}

访问Nexus

Nexus的默认端口是8081,这个端口属于未开放端口.如果是局域网内搭建的私服的话,可以把服务器防火墙关掉(执行:systemctl stop firewalld.service),浏览器输入:http://localhost:8081/就可以访问了.

默认用户:admin,nexus2的默认密码是admin123,但是nexus3需要执行指令查看默认密码:cat /usr/local/sonatype-work/nexus3/admin.password

上传jar包到私服

在本地仓库下增加setting.xml文件

id可以随便去定义.

<?xml version="1.0" encoding="UTF-8"?>
<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">

  <servers>
     <!--配置私服-->
     <server>
      <id>nexus-snapshots</id>
      <username>admin</username>
      <password>admin123</password>
    </server>
    <server>
      <id>nexus-releases</id>
      <username>admin</username>
      <password>admin123</password>
    </server>
  </servers>

  <mirrors>
    <!--配置私服-->
    <mirror>
		<id>maven-public</id>
		<name>maven-public</name>
		<url>http://${host}/repository/maven-public/</url>
		<mirrorOf>*</mirrorOf>
    </mirror>
  </mirrors>

  <profiles>
   
  </profiles>

</settings>

idea 配置maven

idea的配置文件,要使用该setting.xml文件.点击File->setting,输入maven,到maven的设置页面.如果所示:
在这里插入图片描述

项目当中配置私服地址

在项目的pom.xml文件里,添加以下代码.pom.xml的id及setting.xml文件里的id.

   <properties>
       <nexus.host>http://${private_address}:8081</nexus.host>
   </properties>
   <distributionManagement>
       <repository>
           <id>nexus-releases</id>
           <name>Nexus Release Repository</name>
           <url>${nexus.host}/repository/maven-releases/</url>
       </repository>
       <snapshotRepository>
           <id>nexus-snapshots</id>
           <name>Nexus Snapshot Repository</name>
           <url>${nexus.host}/repository/maven-snapshots/</url>
       </snapshotRepository>
   </distributionManagement>

上传jar包到私服

idea执行指令:mvn clean deploy,本地打包编译好了后,就会上传到私服上去了.

猜你喜欢

转载自blog.csdn.net/zjhcxdj/article/details/102914041
今日推荐