【Maven】Maven 与 私服搭建

Maven详解

1. 什么是maven?

Maven这个单词来自于意第绪语(犹太语),意为知识的积累。

​ Maven是专门用于管理和构建java项目的工具,即全生命周期的项目管理工具,基于项目对象模型(POM)的概念,通过一小段描述信息来管理项目的构建、报告和文档。

主要功能有:

  • 提供了一套标准化的项目结构
  • 提供了一套标准化的构建流程(编译、测试、打包、发布…)
  • 提供了一套依赖管理机制

maven发展历史

image-20231012203106463

maven的体系结构

image-20231012195831386

2. maven下载与配置

2.1 下载maven

访问网站 https://maven.apache.org

image-20221225193053478

image-20221225193358873

image-20221225194023576

image-20221225194143162 image-20221225194219474

image-20221225194307487

2.2 maven文件配置

打开conf\settings.xml文件

配置本地仓库位置

<localRepository> 本地地址 <localRepository>

<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
   | The path to the local repository maven will use to store artifacts.
   |
   | Default: ${user.home}/.m2/repository
  -->
    <localRepository>C:\maven-repository</localRepository>
远程镜像仓库配置

由于中央仓库在境外,网络访问不稳定,故在开发过程中大多配置中央仓库的镜像仓库。

image-20221231101418977

<mirror>
  <id>aliyunmaven</id>
  <mirrorOf>central</mirrorOf>
  <name>阿里云公共仓库</name>
  <url>https://maven.aliyun.com/repository/public</url>
</mirror>
profile配置

配置maven默认使用的jdk环境。

image-20221231101536919

<profile>
    <id>jdk-1.8</id>
    <activation>
       <activeByDefault>true</activeByDefault>
       <jdk>1.8</jdk>    
    </activation>
    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
    </properties>
</profile>

2.3 idea环境安装配置maven

在idea中打开File/Settings

image-20221231101822894

配置本地maven

image-20231012152006401

3. maven核心思想

3.1 maven仓库

​ 用来存放第三方jar包、maven所需要的插件以及自己开发的maven项目

image-20221224210030934

分类:

  1. 远程仓库

    1. 私服:如:maven-nexus,一般时由公司或单位创建的一个仓库,由公司维护
    	2. 中央仓库:Maven官方维护的大型仓库
    	3. 中央仓库镜像:直连中央仓库很慢,国内为了便捷访问中央仓库,有一些大公司提供的镜像仓库,如阿里云、腾讯云等
    
  2. 本地仓库

    ​ 通过本地maven配置文件settings.xml 中配置的存储在本地磁盘的文件夹

3.2 maven管理项目的生命周期

也是管理项目周期的常用命令。

image-20221224185546095

注意:

执行任何一个生命周期阶段,之前的生命周期阶段都会被执行。

img

img

3.3 maven项目对象模型 Pom

pom概述

​ Pom(project object model),意思为项目对象模型。通过pom.xml表示maven项目

主要描述了项目:包括配置文件,开发者需要遵循的规则,组织和项目的url,项目的依赖性,以及其他所有的项目相关因素。

image-20221224185644952

dependency中的gav(坐标)
image-20221224205636197

​ 使用以下三个向量在 Maven 的仓库中确定一个 Maven 项目,是其唯一标识

  1. groupid:定义了项目属于哪个组织或公司,通常是域名反写。例如:公司是mycom,项目为myapp,那么groupId就应该是com.mycom.myapp.
  2. artifactId:当前项目的模块名称
  3. version:当前模块的版本
<groupId>com.mqf.maven</groupId> 
<artifactId>Hello</artifactId>
<version>1.0-SNAPSHOT</version>
<!--项目打包的构件类型,包括jar、war、ear、pom等 jar为java项目,war为web项目-->
 <packaging>jar</packaging>

注:SNAPSHOT是快照的意思,代表版本不稳定、尚处于开发中的版本。如果设为Release版本则代表稳定的版本

​ 如何通过坐标在仓库中查找 jar 包?

  1. 将 gav 三个向量连起来

com.mqf.maven + Hello + 1.0-SNAPSHOT

  1. 以连起来的字符串作为目录结构到仓库中查找对应项目jar包

com/mqf/maven/Hello/1.0-SNAPSHOT/Hello-0.0.1-SNAPSHOT.jar

※注意:自己的 Maven 项目必须执行安装操作才会进入本地仓库。安装的命令是:mvn install

scope

表示该gav依赖的有效范围

image-20221224205651577

搭建maven私服-nexus

企业中私服maven仓库Nexus依赖引用流程

image-20231012190021793

下载nexus

访问官网: https://help.sonatype.com/repomanager3/product-information/download

image-20221230152153312

Linux系统中安装nexus

上传压缩文件到linux服务器

在Linux中创建文件夹nexus

在nexus文件夹下上传压缩包

image-20231012113835383

image-20221230153518060

image-20221230153722055

解压并重命名

解压nexus-3.40.1-01-unix.tar.gz并修改文件名为nexus-3.40.1

tar -zxvf nexus-3.40.1-01-unix.tar.gz

修改文件名

mv nexus-3.40.1-01-unix.tar.gz nexus-3.40.1

image-20221230154255201

nexus的目录结构

image-20231012114050161

nexus-3.40.1 : 服务器文件夹,启动程序、配置nexus等工作。

sonatype-work: 工作空间,存储仓库的数据文件。

nexus配置

配置nexus.vmoptions文件
  1. 进入nexus-3.40.1/bin文件夹

cd /usr/local/software/nexus/nexus-3.40.1-01/bin

image-20221230154506369

image-20221230154534475
  1. 编辑nexus.vmoptions文件

根据自己机器内存大小,适当配置内存。内存太小未来启动nexus会失败。

vim nexus.vmoptions

image-20221230155224205
配置端口

默认端口为8081,如果需要在要在文件中配置端口。(如果不需改端口,此处可以忽略)

进入etc文件夹

image-20221230155555257

修改nexus-default.properties文件

image-20221230155709022

image-20221230155804893

开放linux的相应端口

检查系统开放的端口号

firewall-cmd --list-ports

添加8081端口

firewall-cmd --zone=public --add-port=端口号/tcp --permanent

重新加载防火墙

firewall-cmd --reload

运行nexus

启动nexus

image-20231012115442400

运行命令**./nexus start**

[root@localhost bin]# ./nexus  start
WARNING: ************************************************************
WARNING: Detected execution as "root" user.  This is NOT recommended!
WARNING: ************************************************************
Starting nexus
image-20221230160054424
查看nexus的运行状态

在浏览器中输入Linux的ip地址访问nexus http://192.168.216.129:8081/

image-20221230161445094

image-20221230161508314

修改管理员密码

查看管理员账户密码

  1. 进入sonatype-work\nexus3文件夹

    image-20221230205551738
  2. 查看管理员密码

    image-20221230205629156
  • 修改管理员密码

    1. 浏览器登录nexus

      image-20221230205922783

    2. 修改密码

      image-20221230210005734

      image-20221230210125712

      image-20221230210220617

      image-20221230210254339


配置私有仓库

nexus中的默认仓库

maven-releases (Version policy=Release)默认只允许上传不带SNAPSHOT版本尾缀的包,默认部署策略是Disable redeploy 不允许重复上传相同版本号信息的jar,避免包版本更新以后使用方无法获取到最新的包。

maven-snapshots (Version policy=Snapshot)只允许上传带SNAPSHOT版本尾缀的包,默认部署策略是Allow redeploy,允许重复上传相同版本号信息的jar,每次上传的时候会在jar的版本号上面增加时间后缀信息。

maven-central 中央仓库的拷贝,如果环境可以访问中央仓库,则可以获取到相关的包,否则没用

maven-public 仓库组,不是实际一个一个仓库地址,只是将现有的组合到一次,可以通过它看到所属组内全部仓库的jar信息

image-20221230210907451

创建自定义仓库

选择maven2(hosted)

image-20221230211211724

创建自定义仓库

image-20231012191901373

创建release(发行仓库)和snapshot(测试仓库)【实际开发中的解决方案】

实际开发中,会将测试版本项目先部署到测试仓库,发行的项目再部署到发行仓库

image-20231023202027262

image-20231023202105203

添加新建仓库到maven-public群组中

选中maven-public群组,之后访问maven-public就可以访问自己的私有仓库了。

image-20221230212044626

添加仓库到群组maven-public群组中

image-20221230212308330

image-20221230212415878

最终三个仓库都添加进去

image-20231023202141844

批量上传本地文件到自定义仓库中

上传本地maven仓库内容到linux服务器
image-20221230213556643
编辑批量上传的脚本文件

在本地仓库上传后的文件夹(maven-repository)下创建一个shell脚本,命名 repo.sh

创建脚本

touch repo.sh

编辑脚本

vim repo.sh

#!/bin/bash
while getopts ":r:u:p:" opt; do
    case $opt in
        r) REPO_URL="$OPTARG"
        ;;
        u) USERNAME="$OPTARG"
        ;;
        p) PASSWORD="$OPTARG"
        ;;
    esac
done
  
find . -type f -not -path './mavenimport\.sh*' -not -path '*/\.*' -not -path '*/\^archetype\-catalog\.xml*' -not -path '*/\^maven\-metadata\-local*\.xml' -not -path '*/\^maven\-metadata\-deployment*\.xml' | sed "s|^\./||" | xargs -I '{}' curl -u "$USERNAME:$PASSWORD" -X PUT -v -T {} ${REPO_URL}/{} ;

image-20221230220027490

添加脚本执行权限

给脚本repo.sh添加执行权限

chmod +x repo.sh

image-20231012192525965

导入本地仓库到nexus私有仓库

执行以下命令

./repo.sh -u nexus用户名 -p nexus密码 -r 仓库地址 ./:表示当前文件下执行

./repo.sh -u admin -p 123456 -r http://192.168.216.129:8081/repository/wonu-repositoty/

image-20231012193008382

执行结果:

image-20221230220912896

image-20231012144152637

idea项目部署到nexus私服

Maven的setting配置(conf/settings.xml)[在上述第2节一般配置的基础上进行]
配置server

image-20231012194358518

 <server>
      <!-- id 不要随意,与pom中引入的id要相同 -->
      <id>wonu-repository</id>
      <username>admin</username>
      <password>123456</password>
    </server>
    <server>
      <id>wonu-releases</id>
      <username>admin</username>
      <password>123456</password>
    </server>
    <server>
      <id>wonu-snapshots</id>
      <username>admin</username>
      <password>123456</password>
    </server>
  </servers>
配置镜像

中央仓库的资源从阿里云访问,其它资源来自nexus私服。

image-20231012194810695

 <mirrors>
    <!-- mirror
     | Specifies a repository mirror site to use instead of a given repository. The repository that
     | this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
     | for inheritance and direct lookup purposes, and must be unique across the set of mirrors.
     |
    <mirror>
      <id>mirrorId</id>
      <mirrorOf>repositoryId</mirrorOf>
      <name>Human Readable Name for this Mirror.</name>
      <url>http://my.repository.com/repo/path</url>
    </mirror>
     -->
    <!--配置阿里云镜像-->
	  <mirror>
		  <id>aliyun-maven</id>
		  <mirrorOf>central</mirrorOf>
		  <name>阿里云公共仓库</name>
		  <url>https://maven.aliyun.com/repository/public</url>
	  </mirror>
    <!--maven私服-->
    <mirror>
      <id>maven-public</id>
      <mirrorOf>*</mirrorOf>
      <name>nexus私服</name>
      <url>http://192.168.216.129:8081/repository/maven-public/</url>
    </mirror>

  </mirrors>

私服访问地址从下图获取:

image-20221231104245101

配置nexus仓库设置
<profiles>
    ...
<!--nexus的配置-->
  <profile>
  <!-- profile 的 id -->
  <id>nexus</id>
  <repositories>
    <repository>
        <!--仓库 id,repositories 可以配置多个仓库,保证 id 不重复-->
        <id>nexus</id>
        <!--仓库地址,即 nexus 仓库组的地址-->
        <url>http://192.168.216.129:8081/repository/maven-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://192.168.216.129:8081/repository/maven-public/</url>
      <!--是否下载 releases 构件-->
      <releases>
          <enabled>true</enabled>
      </releases>
      <!--是否下载 snapshots 构件-->
      <snapshots>
          <enabled>true</enabled>
      </snapshots>
  </pluginRepository>
</pluginRepositories>
</profile>
    ...
</profiles>

<!-- 激活上述nexus 的配置 -->
  <activeProfiles>
    <activeProfile>nexus</activeProfile>
  </activeProfiles>
项目中配置发布管理

在项目的pom.xml文件中添加<distributionManagement>

注意!对于多模块的maven项目,应该在父模块的pom文件中进行发布配置

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.wnhz.nexus</groupId>
    <artifactId>wnhz-nexus</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.13</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <!--定义springboot的maven打包插件-->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <!-- <distributionManagement> -->
    <!--     <repository> -->
              <!--注意:该id 一定要与maven settings.xml中server中 的id 一致,否住会出现401 权限错误-->
     <!--       <id>wonu-repository</id>  -->
     <!--       <url>http://192.168.216.129:8081/repository/wonu-repositoty/</url>  -->
    <!--     </repository>  -->
   <!--  </distributionManagement> -->
    
    <distributionManagement>
        <!--注意:该id 一定要与maven settings.xml中server中 的id 一致,否住会出现401 权限错误-->
        <snapshotRepository>
            <id>wonu-snapshots</id>
            <url>http://192.168.216.129:8081/repository/wonu-snapshots/</url>
        </snapshotRepository>
        <repository>
            <id>wonu-releases</id>
            <url>http://192.168.216.129:8081/repository/wonu-releases/</url>
        </repository>
    </distributionManagement>

    <repositories>
        <repository>
            <id>nexus</id>
            <url>http://192.168.216.129:8081/repository/maven-public/</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
    </repositories>

</project>
部署项目

在maven中运行部署,部署(deploy)项目到私服nexus中创建的仓库

image-20221231110237600

image-20230924183901078

默认的测试项目部署到了测试仓库

image-20231023203422652

此时发行仓库内为空

image-20231023203537211

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值