Maven 基础标签之版本管理和冲突解决

前言

我们在做java项目的时候由于jar包太多,我们就需要使用maven做项目管理,管理项目的jar包依赖,包括打包上线

maven基础

Maven 是一个项目管理工具,主要用于项目构建,依赖管理,项目信息管理

每个maven 项目根目录都会有一个 pom.xml文件, 负责项目构建,依赖管理
在这个文件里面,你只需要添加相应的jar包坐标配置,maven就会自动帮你下载相应jar包,不需要我们手动去构建添加jar包很方面,尤其在项目很大,需要jar包依赖很多时候,根本离不开maven

[图片上传失败...(image-bf76db-1628750432956)]

maven的代码库中,每一个jar包也有自己的 pom.xml文件配置,只要你配置的jar包所依赖的其他jar包都会被maven自动下载下来。

这里会涉及到问题maven多模块实现以及循环依赖 后面会讲解

     
      cn.soboys  项目组项目所在团队
      kmall  项目具体名称模块名
      1.0-SNAPSHOT  项目版本号
      

maven都会通过,--坐标来定位一个jar包
在maven互联网仓库上,并进行下载

Maven 会根据 POM 文件自动下载所需 JAR 包。如果以后你在本地其它项目中也使用此 JAR 包,那么 Maven 会首先检查本地是否有 JAR 包,如果没有再从中央仓库下载。

maven 项目结构命令

image
  1. src/main/java - 存放项目.java文件;
  2. src/main/resources - 存放项目资源文件;
  3. src/test/java - 存放测试类.java文件;
  4. src/test/resources - 存放测试资源文件;
  5. target - 项目输出目录;
  6. pom.xml - Maven核心文件(Project Object Model);

maven常用命令

  1. mvn archetype:create 创建Maven项目
  2. mvn compile 编译源代码
  3. mvn deploy 发布项目
  4. mvn test-compile 编译测试源代码
  5. mvn test 运行应用程序中的单元测试
  6. mvn site 生成项目相关信息的网站
  7. mvn clean 清除项目目录中的生成结果
  8. mvn package 根据项目生成的jar
  9. mvn install 在本地Repository中安装jar
  10. mvn eclipse:eclipse 生成eclipse项目文件
  11. mvnjetty:run 启动jetty服务
  12. mvntomcat:run 启动tomcat服务
  13. mvn clean package -Dmaven.test.skip=true 清除以前的包后重新打包,跳过测试类

maven 常用标签

parent标签

在maven多模块项目中引用父pom依赖,在springboot项目中就有父依赖

  
    
        org.springframework.boot
        spring-boot-starter-parent
        2.4.7
         
    

properties标签

定义一些全局属性值,常用于jar包版本定义全局管理jar包版本后面可以${} 取值

在springboot项目中父pom会定义一些项目jar包版本依赖 ,所以我们在引用jar时候才不用写jar包版本,会自动跟随父pom中定义的jar包版本


    org.springframework.boot
    spring-boot-dependencies
    2.4.7
  

    5.16.2
    2.7.7
    1.9.89
    2.15.0
    1.9.6
    3.18.1
    4.0.6
    4.0.3
    2.1.4
    3.2.0
    1.10.22
    2.8.8
    4.9.0
    1.5.1
    1.15
    2.8.0
    3.11
    1.6
    2.9.0
    3.0.10
    11.5.5.0
    1.0.11.RELEASE
    10.14.2.0
    4.1.22
    2.10.9.2
    3.9.4
    7.9.3
    2.2.0
    7.1.1
    2.3.31
    3.0.1
    3.0.3
    2.3.4
    2.5.14
    2.8.7
    1.4.200
    2.2
    4.0.3
    2.1.1
    5.4.32.Final
    6.1.7.Final
    3.4.5
    2.5.2
    2.44.0
    4.1.4
    4.5.13
    4.4.14
    11.0.11.Final
    2.20
    2.11.4
    1.2.2
    1.3.5
    2.0.3
    1.1.6
    1.0.2
    1.6.7
    2.2.3
    4.0.4
    1.2.7
    1.3.3
    2.0.2
    1.1.2
    2.1.6
    2.3.3
    1.4.2
    2.3.3
    3.1.4
    1.2.0
    1.3.2
    1.1.1
    2.3.1
    2.3.1
    2.0.1
    1.1.4
    1.0
    1.6.2
    1.1
    2.2
    1.3
    2.0.1.Final
    1.1
    1.2.0
    3.0.11
    3.4.2.Final
    7.6.1.Final
    2.0.6
    3.3.0
    2.32
    8.5.54
    2.2.0.v201112011158
    1.1.9
    9.4.42.v20210604
    1.15
    1.2.13
    1.6.2
    3.14.11
    2.4.0
    2.3.1
    1.5.0
    1.2
    1.3.1
    4.13.2
    5.7.2
    2.6.2
    1.4.32
    1.4.3
    6.0.5.RELEASE
    3.10.3
    2.13.3
    1.2.3
    1.18.20
    2.7.3
    1.8
    3.3.0
    3.1.0
    3.8.1
    3.1.2
    2.8.2
    3.0.0-M3
    2.22.2
    3.2.0
    2.5.2
    3.2.2
    3.2.0
    3.2.0
    3.2.0
    3.2.4
    3.2.1
    2.22.2
    3.3.1
    1.6.7
    1.9.14
    3.6.28
    4.1.2
    8.4.1.jre8
    8.0.25
    1.9.22
    4.1.3
    4.1.65.Final
    2.0.39.Final
    8.36.1
    8.20.2
    19.3.0.0
    3.14.9
    19.8.0.0
    1.2.2
    42.2.20
    0.9.0
    2.3.2
    4.4.0
    Arabba-SR10
    5.10.0
    1.0.3
    2020.0.7
    3.3.0
    1.1.1
    1.3.8
    1.2.1
    2.2.21
    1.5.3
    3.141.59
    2.44.0
    4.6.8
    4.0.1
    1.7.30
    1.27
    8.5.2
    2.3.8
    4.3.3
    2020.0.9
    5.3.8
    1.2.6
    5.4.7
    2.6.9
    2.3.4.RELEASE
    2.0.5.RELEASE
    1.3.1
    5.4.6
    2020.0.4
    3.0.10.RELEASE
    3.32.3.3
    1.6.7
    3.0.12.RELEASE
    2.0.1
    3.0.4.RELEASE
    3.0.4.RELEASE
    2.5.3
    9.0.46
    4.0.14
    2.2.8.Final
    2.8.1
    3325375
    0.46
    1.6.3
    1.0.2
    2.7.0
  

dependencyManagement标签

Maven多模块的时候,管理依赖关系是非常重要的,各种依赖包冲突,查询问题起来非常复杂,于是就用到了

在springboot项目中父模块就定义了

  
        
            
             
            org.springframework.boot
            spring-boot-starter
            2.4.7
          
          
            org.springframework.boot
            spring-boot-starter-activemq
            2.4.7
          
          
            org.springframework.boot
            spring-boot-starter-actuator
            2.4.7
          
          
            org.springframework.boot
            spring-boot-starter-amqp
            2.4.7
          
          
            org.springframework.boot
            spring-boot-starter-aop
            2.4.7
          
          
            org.springframework.boot
            spring-boot-starter-artemis
            2.4.7
          
          
            org.springframework.boot
            spring-boot-starter-batch
            2.4.7
          
          
            org.springframework.boot
            spring-boot-starter-cache
            2.4.7
          
          
            org.springframework.boot
            spring-boot-starter-data-cassandra
            2.4.7
          
          
            org.springframework.boot
            spring-boot-starter-data-cassandra-reactive
            2.4.7
          
          
            org.springframework.boot
            spring-boot-starter-data-couchbase
            2.4.7
          
          
            org.springframework.boot
            spring-boot-starter-data-couchbase-reactive
            2.4.7
          
          
            org.springframework.boot
            spring-boot-starter-data-elasticsearch
            2.4.7
          
          
            org.springframework.boot
            spring-boot-starter-data-jdbc
            2.4.7
          
          
            org.springframework.boot
            spring-boot-starter-data-jpa
            2.4.7
          
          
            org.springframework.boot
            spring-boot-starter-data-ldap
            2.4.7
          
          
            org.springframework.boot
            spring-boot-starter-data-mongodb
            2.4.7
          
          
            org.springframework.boot
            spring-boot-starter-data-mongodb-reactive
            2.4.7
          
          
            org.springframework.boot
            spring-boot-starter-data-r2dbc
            2.4.7
          
          
            org.springframework.boot
            spring-boot-starter-data-redis
            2.4.7
          
          
            org.springframework.boot
            spring-boot-starter-data-redis-reactive
            2.4.7
          
          
            org.springframework.boot
            spring-boot-starter-data-neo4j
            2.4.7
          
          
            org.springframework.boot
            spring-boot-starter-data-rest
            2.4.7
          
          
            org.springframework.boot
            spring-boot-starter-data-solr
            2.4.7
          
          
            org.springframework.boot
            spring-boot-starter-freemarker
            2.4.7
          
          
            org.springframework.boot
            spring-boot-starter-groovy-templates
            2.4.7
          
          
            org.springframework.boot
            spring-boot-starter-hateoas
            2.4.7
          
          
            org.springframework.boot
            spring-boot-starter-integration
            2.4.7
          
          
            org.springframework.boot
            spring-boot-starter-jdbc
            2.4.7
          
          
            org.springframework.boot
            spring-boot-starter-jersey
            2.4.7
          
          
            org.springframework.boot
            spring-boot-starter-jetty
            2.4.7
          
          
            org.springframework.boot
            spring-boot-starter-jooq
            2.4.7
          
          
            org.springframework.boot
            spring-boot-starter-json
            2.4.7
          
          
            org.springframework.boot
            spring-boot-starter-jta-atomikos
            2.4.7
          
          
            org.springframework.boot
            spring-boot-starter-jta-bitronix
            2.4.7
          
          
            org.springframework.boot
            spring-boot-starter-log4j2
            2.4.7
          
          
            org.springframework.boot
            spring-boot-starter-logging
            2.4.7
          
          
            org.springframework.boot
            spring-boot-starter-mail
            2.4.7
          
          
            org.springframework.boot
            spring-boot-starter-mustache
            2.4.7
          
          
            org.springframework.boot
            spring-boot-starter-oauth2-client
            2.4.7
          
          
            org.springframework.boot
            spring-boot-starter-oauth2-resource-server
            2.4.7
          
          
            org.springframework.boot
            spring-boot-starter-quartz
            2.4.7
          
          
            org.springframework.boot
            spring-boot-starter-reactor-netty
            2.4.7
          
          
            org.springframework.boot
            spring-boot-starter-rsocket
            2.4.7
          
          
            org.springframework.boot
            spring-boot-starter-security
            2.4.7
          
          
            org.springframework.boot
            spring-boot-starter-test
            2.4.7
          
          
            org.springframework.boot
            spring-boot-starter-thymeleaf
            2.4.7
          
          
            org.springframework.boot
            spring-boot-starter-tomcat
            2.4.7
          
          
            org.springframework.boot
            spring-boot-starter-undertow
            2.4.7
          
          
            org.springframework.boot
            spring-boot-starter-validation
            2.4.7
          
          
            org.springframework.boot
            spring-boot-starter-web
            2.4.7
          
          
            org.springframework.boot
            spring-boot-starter-webflux
            2.4.7
          
          
            org.springframework.boot
            spring-boot-starter-websocket
            2.4.7
          
          
            org.springframework.boot
            spring-boot-starter-web-services
            2.4.7
          
        

那么在子模块中只需要即可,也就是我们创建springboot项目中

 
            org.springframework.boot
            spring-boot-starter-aop
        

使用dependencyManagement可以统一管理项目的版本号,确保应用的各个项目的依赖和版本一致,不用每个模块项目都弄一个版本号,不利于管理,当需要变更版本号的时候只需要在父类容器里更新,不需要任何一个子项目的修改;如果某个子项目需要另外一个特殊的版本号时,只需要在自己的模块dependencies中声明一个版本号即可。子类就会使用子类声明的版本号,不继承于父类版本号

dependencies标签

用于引入项目依赖

  
        
        
            org.springframework.boot
            spring-boot-starter-aop
        
        
    

注意

Dependencies相对于dependencyManagement,所有生命在dependencies里的依赖都会自动引入,并默认被所有的子项目继承。
dependencyManagement里只是声明依赖,并不自动实现引入,因此子项目需要显示的声明需要用的依赖。

如果不在子项目中声明依赖,是不会从父项目中继承下来的;只有在子项目中写了该依赖项,并且没有指定具体版本,才会从父项目中继承该项,并且version和scope都读取自父pom;另外如果子项目中指定了版本号,那么会使用子项目中指定的jar版本。

maven冲突解决

我们可以通过exclusions 标签过滤掉不需要的依赖


            org.springframework.boot
            spring-boot-starter-web
            
                
                    org.springframework.boot
                    spring-boot-starter-logging
                
                
                    org.springframework.boot
                    spring-boot-starter-tomcat
                
            
  

maven打包和插件

下面列举的常用打包和插件

 
    
       
        
            true
            src/main/resources
        
    


        
            
                org.apache.maven.plugins
                maven-compiler-plugin
                ${maven.compiler.plugin.version}
                
                    ${java.version}
                    ${java.version}
                    ${project.build.sourceEncoding}
                
            

            
                org.apache.maven.plugins
                maven-resources-plugin
                ${maven.resources.plugin.version}
                
                    ${project.build.sourceEncoding}
                
            

            
                org.apache.maven.plugins
                maven-jar-plugin
                ${maven.jar.plugin.version}
            

           

            
                org.springframework.boot
                spring-boot-maven-plugin
                
                    
                        
                            repackage
                        
                    
                
            
        
    

你可能感兴趣的:(Maven 基础标签之版本管理和冲突解决)