☰
MAVEN的使用详解
2026/10/10 1:15:08 网站建设 项目流程

文章目录

  • 相关配置
    • 本地仓库中几个文件的作用
    • setting配置文件
    • pom.xml配置文件
    • Maven坐标
      • 命名规则
      • Scope的值及意义
    • 依赖传递冲突问题
    • 继承
  • 常用功能
    • 将本地jar包安装到本地maven仓库
    • IDEA中如何查找jar包之间的依赖关系并忽略依赖的某个包
      • 根据类名查找依赖的jar包
    • 解决依赖冲突
    • 打包时跳过test测试
    • 修改打包名称的配置

相关配置

本地仓库中几个文件的作用

  • .jar:保存编译后的 Java 类和资源,是实际使用的程序包。
  • .pom:描述包的坐标、父 POM、依赖和版本管理等。Maven 根据它建立依赖树。不是根据jar包里面的原始POM
  • _remote.repositories:记录同目录中的 JAR、POM 来自哪个远程仓库,或是否通过本地安装获得。Maven 用它判断缓存是否适用于当前仓库配置。
  • .jar.lastUpdated:缓存对应 JAR 的远程解析失败及检查时间,避免短时间内反复请求。拉取远程失败时才会生成
  • .pom.lastUpdated:缓存对应 POM 的远程解析失败及检查时间。同.jar.lastUpdated拉取远程失败时才会生成

setting配置文件

<?xml version="1.0" encoding="UTF-8"?><settingsxmlns="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>D:/dev/repository</localRepository><!--阿里镜像配置--><mirrors><mirror><id>nexus-aliyun</id><mirrorOf>central</mirrorOf><name>nexus-aliyun</name><url>http://maven.aliyun.com/nexus/content/groups/public</url></mirror></mirrors><!-- 配置私有仓库 --><servers><server><id>private-repo-id</id><username>username</username><password>username</password></server></servers><profiles><profile><id>private-repo</id><repositories><repository><id>private-repo-id</id><name>private-repo-name</name><url>private-repo-url</url><releases><enabled>true</enabled></releases><snapshots><enabled>true</enabled></snapshots></repository></repositories></profile></profiles><activeProfiles><activeProfile>private-repo</activeProfile></activeProfiles></settings>
  • 配置私有仓库是配置账号密码的id是自定义,server的id要和仓库的id即profile.repository.id的值一致,仓库的名称profile.repository.name可以和id一致,activeProfiles.activeProfile和profile.id一致,activeProfiles的作用是激活私有仓库配置
  • 仓库优先级:私有仓库 > 阿里云镜像 > 默认中央仓库
  • mirrorOf:被代理的仓库的ID,central是maven的中央仓库的ID,此仓库的配置可以在maven安装包的lib目录下的maven-model-builder-xxx.jar包里的org\apache\maven\model\pom-4.0.0.xml文件可以查找到

配置私有仓库也可以把仓库信息配置到项目中的pom文件中,账号密码为了安全仍然配置到maven中

</project><repositories><repository><snapshots><enabled>true</enabled></snapshots><id>private-repo-id</id><name>private-repo-name</name><url>private-repo-url</url></repository></repositories></project>

pom.xml配置文件

<?xml version="1.0" encoding="UTF-8"?><projectxmlns="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"><parent><artifactId>artifactId</artifactId><groupId>groupId</groupId><version>4.0.0</version></parent><modelVersion>4.0.0</modelVersion><packaging>war</packaging><artifactId>artifactId</artifactId><description>web服务入口</description><dependencies><!-- spring-boot-devtools --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><optional>true</optional><!-- 表示依赖不会传递 --><exclusions><exclusion><!--排除(不导入)包中的jar不导入的配置--><groupId></groupId><artifactId></artifactId></exclusion></exclusions></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><fork>true</fork><!-- 如果没有该配置,devtools不会生效 --></configuration><executions><execution><goals><goal>repackage</goal></goals></execution></executions></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-war-plugin</artifactId><version>3.0.0</version><configuration><failOnMissingWebXml>false</failOnMissingWebXml><warName>mediaSystem</warName></configuration></plugin></plugins><finalName>mediaSystem</finalName></build></project>

Maven坐标

  • groupId :the unique identifier of the organization or group that created the project
    GroupID 是项目组织唯一的标识符,一般是main目录里java的目录结构,没有硬性要求一定要和JAVA的包的结构一致。
  • artifactId :unique base name of the primary artifact being generated by this project
    ArtifactID是项目的唯一的标识符,规范要求对应项目的名称,就是项目根目录的名称。

命名规则

  • GroupId一般分为多个段,第一段为域,第二段为公司名称。域又分为org、com、cn等等许多,其中org为非营利组织,com为商业组织。举个apache公司的tomcat项目例子:这个项目的GroupId是org.apache,它的域是org(因为tomcat是非营利项目),公司名称是apache,ArtifactId是tomcat。再比如我自己建项目,可以写com.leon.
  • ArtifactId 是项目名称,比如:apache的tomcat项目,再比如我自己的项目testProj。

Scope的值及意义

test、compile、runtime、provided、system、import。其中import主要是在dependencyManagement中使用,平常用的不多,所以主要看其它五种;

  • test:仅测试的时候使用,打包编译时,不会使用该包。
  • compile:一般默认是compile,编译的时候使用,整个项目周期都会用。打包的时候会将其打包。
  • runtime:相比compile,只是不参与编译阶段。
  • provided:编译和运行都有参与,但是不参与打包,一般servlet的包都是这个值。
  • system: 使用较少,需配合systemPath使用,和provided唯一的区别是从本地获取依赖。

依赖传递冲突问题

  1. 路径优先:当依赖中出现相同的资源时,层级越深,优先级越低,层级越浅,优先级越高,即直接依赖优先级高于间接依赖,简介依赖即依赖的依赖
  2. 声明优先:当资源在相同层级被依赖时,配置顺序靠前的覆盖掉配置顺序靠后的
  3. 特殊优先:当在同一个配置文件配置了相同资源的不同版本时,后配置的覆盖掉先配置的

继承

作用:可以实现子工程沿用父工程的依赖版本,起到版本控制的效果,使所有子工程使用的依赖版本一致
在父工程中配置依赖管理<dependencyManagement></dependencyManagement>,在子工程中引用父工程

<parent> <artifactId>父工程的artifactId</artifactId> <groupId>父工程的groupId</groupId> <version>父工程的version</version> </parent>

并在子工程中引用依赖,只需要此依赖的artifactId和groupId,不需要版本,版本在父工程中管理

常用功能

将本地jar包安装到本地maven仓库

mvn install:install-file-DgroupId=tk.mybatis-DartifactId=mapper-spring-boot-starter-Dversion=2.1.2-Dpackaging=jar-Dfile=mapper-spring-boot-starter-2.1.2.jar

解读

  • mvn install:install-file固定命令
  • -DgroupId=后面的值即是引入依赖的groupId标签的值
  • -DartifactId=后面的值即是引入依赖的artifactId的值
  • -Dversion=是引入依赖的version的值
  • -Dpackaging=jar是引入的安装以jar包形式
  • -Dfile=本地jar的路径,如果执行命令在jar包所在目录,直接写jar包名称即可

IDEA中如何查找jar包之间的依赖关系并忽略依赖的某个包

在idea中的右侧栏的maven工具,点击图中圈中的show Dependencies,

在下面的界面Ctrl + f 搜索要查找的jar包,然后根据图中的关联线即可查找到最终的依赖

根据类名查找依赖的jar包

双击shift键搜索类名

解决依赖冲突

一般有两种解决方案

  1. 使用exclusions标签排除
<dependency><groupId>net.oschina.j2cache</groupId><artifactId>j2cache-core</artifactId><version>2.8.0-release</version><exclusions><exclusion><groupId>org.slf4j</groupId><artifactId>slf4j-simple</artifactId></exclusion></exclusions></dependency>
  1. 直接在pom文件引入冲突依赖的最新依赖,覆盖掉其他版本

打包时跳过test测试

  1. 方法一:通过执行如下命令
    mvn install -DskipTests或者
    mvn install -Dmaven.test.skip=true

  2. 方法二:Idea 设置maven打包跳过test目录
    如下图:build,Execution,Deployment>build tools >maven >runner ---->VM Options

修改打包名称的配置

<modelVersion>4.0.0</modelVersion><artifactId>cat-home</artifactId><name>cat-home</name><!--打包格式--><packaging>war</packaging><build><resources><resource><directory>${basedir}/src/main/resources</directory><filtering>true</filtering></resource></resources><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-war-plugin</artifactId><version>2.1.1</version><configuration><!--打包包名--><warName>${project.artifactId}</warName><!--如果没有web.xml文件也不报错--><!--<failOnMissingWebXml>false</failOnMissingWebXml>--><webResources><resource><directory>src/main/resources</directory><filtering>false</filtering><targetPath>WEB-INF/classes</targetPath></resource><resource><directory>src/main/webapp</directory><filtering>false</filtering><includes><include>WEB-INF/web.xml</include></includes></resource></webResources></configuration></plugin></plugins></build>

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询