Maven高级

目录

maven依赖传递

问题一:依赖冲突

方法一:使用maven提供的依赖调解原则

方法二:排除依赖

 方法三:版本锁定

分模块构建maven工程


Maven的原理

主要负责依赖管理项目构建

  • 依赖管理:对项目jar包的管理
  • 项目构建:通过命令可以完成项目清理、编译、测试、报告、打包、部队的整个过程。

mave的仓库类型

  • 本地仓库
  • 远程仓库
    • maven中央仓库
    • maven私服
    • 其它公共远程仓库

maven常用命令

  • clean:清理
  • compile:编译
  • test:测试
  • package:打包
  • install:安装

maven依赖传递

问题一:依赖冲突

方法一:使用maven提供的依赖调解原则

第一声明者优先原则,路径近者优先原则

第一声明者优先原则:定义在前面的有效,定义在后面的就会被忽略

路径近者优先原则:以路径近者为准(路径:依赖的传递路径)

方法二:排除依赖

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.0.5.RELEASE</version>
      <exclusions>
<!--        排除该依赖中的 spring-beans依赖-->
        <exclusion>
          <groupId>org.springframework</groupId>
          <artifactId>spring-beans</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
<!--      aop依赖中也包含beans-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aop</artifactId>
      <version>4.2.8.RELEASE</version>
    </dependency>

结果:beans的版本和aop一致

 方法三:版本锁定

采用直接锁定版本的方法确定依赖jar包的版本

步骤

  • 在dependecyManagement标签中锁定依赖的版本(注意 这个步骤仅仅是锁定,并未导入jar包)
  • 在dependencies标签中声明需要导入的maven坐标
 <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>3.4.5</version>
      </dependency>
     </dependencies>
  </dependencyManagement>

 <dependencies>
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
    </dependency>
  </dependencies>

分模块构建maven工程

一个项目,maven工程可以将其分解成若干个模块

 

 父工程与子工程的关系是继承关系,子工程自动继承父工程的资源

 子工程与子工程的关系是依赖,需要手动添加依赖子工程的资源

步骤:

父工程的打包方式:pom

    <groupId>org.example</groupId>
    <artifactId>maven_ssm</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>

子工程继承父工程:自动生成

web层继承父工程

    <parent>
        <artifactId>maven_ssm</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

在子工程里添加依赖

web层依赖service层

    <dependencies>
        <dependency>
            <groupId>org.example</groupId>
            <artifactId>maven_service</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

父工程:maven的聚合

   <modules>
        <module>maven_dao</module>
        <module>maven_service</module>
        <module>maven_web</module>
    </modules>

目录结构

 

 web层的applicationContext.xml 引入两个配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <import resource="dao.xml"/>
    <import resource="service.xml"/>
</beans>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值