0%

Maven私服教程

项目打包

maven项目打包设置项目的相关属性

image-20240302125437598

java 项目默认打包方式是 jar

1
<packaging>jar</packaging>

编写的工具类的GAV的值,可以在其他项目中使用

1
2
3
<groupId>com.ruoyi</groupId>
<artifactId>ruoyi-forward</artifactId>
<version>3.8.7</version>

当前的项目需要被其他项目引用,可以打包(jar ),发布到中央仓库(远端仓库),或者发布到本地仓库

1
mvn install

image-20240302130710858

成功后可以在本地仓库看到project

image-20240302131114509

Maven项目的webapp的打包方式是 war

1
<packaging>war</packaging>

Mven的依赖和坐标

每一个maven管理的project都会有自己的坐标 GAV

1
2
3
<groupId>com.ruoyi</groupId>
<artifactId>ruoyi-forward</artifactId>
<version>3.8.7</version>

Maven的依赖范围

使用scope标签进行定义

image-20240302132226009

  • test:在测试环境中使用

  • runtime:不参与编译,只有运行的时候使用(发布到Tomcat中的时候用 JDBC)

  • compile (不写就默认):运行、测试、编译

  • provided:只参与编译,测试环境(Servlet-api )

  • system:如果jar使用的是非Maven仓库,引入的本地jar文件(不推荐使用)

列如:

image-20240302133143568

依赖的传递性

三个项目

  • mvn-web

    • mvn_test1
  • mvn_test1

    • common-fileipload

相当于

  • mvn-web
    • mvn_test1
    • common-fileipload

就近原则

传递依赖,(最短路径之后)有限申明的优先级最高

当前pom文件中声明不同版本,最后一层覆盖之前的版本.

有效 pom

本地POM:当前使用的项目中的POM.Xml

超级POM: 在maven安装目录里的jar包

实际的POM文档是他们的集合

依赖的排除

image-20240302191024397

  • optional: 依赖不具有传递性,只在当前项目有效

image-20240302191124050

聚合项目

  • 联合编译
  • 同步maven声明周期
  • maven
    • projectA
    • projectB

Maven 私服

开源的项目可以发布到中央仓库,有不能发布到外网的情况,需要准备自己的私服

将私服安装好后,启动

1
2
3
4
5
6
7
8
9
root@xiaobai-B250M-D2VX-SI:/usr/local/nexus/nexus/bin# ls
contrib nexus nexus.rc nexus.vmoptions
root@xiaobai-B250M-D2VX-SI:/usr/local/nexus/nexus/bin# ./nexus
Usage: ./nexus {start|stop|run|run-redirect|status|restart|force-reload}
root@xiaobai-B250M-D2VX-SI:/usr/local/nexus/nexus/bin# ./nexus start
Starting nexus
root@xiaobai-B250M-D2VX-SI:/usr/local/nexus/nexus/bin# ./nexus status
nexus is running.
root@xiaobai-B250M-D2VX-SI:/usr/local/nexus/nexus/bin#

仓库介绍

jcenter是安卓的仓库

image-20240302195702530

hsoted:在本地存储的普通的仓库

一般保存公司内部的项目

proxy :中央仓库,是代理仓库

group :仓库的集合(很多个其他仓库的集合)


使用教程

获取到使用的地址

使用的时候使用group

http://192.168.0.126:8081/service/rest/repository/browse/maven-public/

测试本地项目打包到私服

项目中添加私服的地址

1
2
3
4
5
6
7
8
9
<!--  开发项目中配置  配置私服的地址  本地项目可以发布到私服-->
<distributionManagement>
<repository>
<id>maven-releases</id>
<name>name</name>
<url>http://192.168.0.126:8081/repository/maven-releases/</url>
</repository>
</distributionManagement>

在settings.xml中配置服务器的权限

1
2
3
4
5
<server>
<id>maven-releases</id>
<username>admin</username>
<password>juihuib203</password>
</server>

image-20240304090845673

一些第三方的没有使用maven管理的包,可以通过upload上传到maven私服

使用私服下拉数据到本地

1
2
3
4
5
6
7
<!-- settings 文件配置 镜像配置-私服 -->
<mirror>
<id>NexuLo</id>
<name>NexuLo-public</name>
<mirrorOf>*</mirrorOf>
<url>http://192.168.0.126:8081/service/rest/repository/browse/maven-public/</url>
</mirror>