maven基础知识
1. maven的作用
依赖管理: 对jar包的管理方式(pom)
一键构建: 一个命令搞定整个构建过程
2. maven的常用命令
清理: clean, 清理掉上一次项目构建产生的文件, 删除target目录
编译: compile, 把正式的java代码转换为class代码
测试: test,将测试代码编译并且运行
打包: package,将正式的资源打成jar war pom包
安装: install,将打好的包安装到本地仓库的过程
部署: deploy,将打好的包安装到私服的过程
3. maven的生命周期
清理: clean
默认: compile test package install deploy
站点: site
4. maven仓库的种类
本地
私服
中央
5. maven的主要标签
<groupId>公司名</groupId>
<artifactId>项目名</artifactId>
<packaging>打包方式(jar war pom)</packaging>
<version>版本</version>
SNATSHOP 快照 测试版
RELEASE 发行 稳定版
分模块开发
传统的web程序进行jar包管理的方式
企业级开发的jar包管理方式---maven的方式进行jar包的管理
maven程序的配置文件
拆分与聚合的思想
拆分 将原来的一个项目拆分成一个个小的模块, 每个模块都有自己的坐标, 当其它项目需要本模块的功能时, 只需要按照坐标引入本模块就可以了
聚合 通过坐标的形式,将各个模块组装在一起,完成一个项目的功能
mvc (moudle / viewer(JSP/html/asp/安卓/ios) /controller)
项目
|--view层(jsp)
|--controller(接口)
|--service(服务)
|--dao/mapper(数据库操作)
父子工程搭建
父工程
|---web项目(独立的项目)
|---网页
|---controller层
|--service项目(独立的项目)
|--service接口
|---impl 接口实现
|--dao/mapper项目
|---entity/domain/pojo实体类
|---dao层
接口
xml文件
解压leyouMaven-100qi.rar
解压后编辑setting.xml文件
创建父工程 ssm-parent
配置项目的maven路径,非常重要
在资料中,找到ssm-parent的pom文件,然后打开,复制对应<dependences>下的内容到ssm-parent父工程的pom.xml文件中
<dependencies>
<!--mybatis相关的坐标-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.0</version>
</dependency>
<!--spring相关的坐标-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.7</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.15</version>
</dependency>
<!--springmvc相关的坐标-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!--mybatis-spring-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.1</version>
</dependency>
<!--辅助坐标-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.6</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.12</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
<dependences>下的jar包引入与maven仓库的对照关系
创建dao模块 ssm-dao
创建service模块 ssm-service
<dependencies>
<!--service层引用dao层的类时,需要导入dao层的jar包-->
<dependency>
<groupId>com.itheima</groupId>
<artifactId>ssm-dao</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
创建web模块 ssm-web
快捷方式: 在pom文件中的<dependences>下,可以使用 dep 快速生成<dependence></dependence>依赖的结构
<dependencies>
<!--快捷方式: 在-->
<dependency>
<groupId>com.itheima</groupId>
<artifactId>ssm-service</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
maven工程, 在编译阶段,可以通过源码直接产生依赖; 但是在运行阶段,必须通过jar包进行引用
通过插件,将maven项目,转化为一个web项目
- 在plugin下添加 JBLJaveToWeb插件,然后重启idea
- 将maven项目转化为web项目
转化后,会将源程序做两处变更
1)pom文件中 将打包 方式 变为 <packaging>war</packaging>
2)在main下添加了一个webapp文件夹,
两套关系梳理
在上面的工程中, 存在两套关系
父子工程之间: 继承关系, 子工程可以使用父工程中的jar包
模块工程之间: 默认是没有任何关系的,但是可以通过依赖建立两者之间的关系,而且这个依赖关系是可以传递的
web-->service--->dao 相当于web--->dao
web-->service 直接依赖
web-->dao 间接依赖
依赖冲突
什么是依赖冲突
在依赖的传递过程中,很容易出现同一jar包的版本冲突问题,这个就称为依赖冲突
依赖冲突解决
第一声明优先原则
在pom文件定义依赖,先声明的依赖为准。
路径近者优先原则
从依赖程序开始算起,到被依赖的程序,以路径短的为准。
依赖排除
依赖排除就是在依赖引入的过程中,通过exclusions
标签排掉指定的跟随依赖
版本锁定
面对众多的依赖,有一种方法不用考虑依赖路径、声明优先等因素,可以采用直接锁定版本的方法确定依赖构件的版本
版本锁定后,系统会以锁定的版本的为准添加到工程中,此方法在企业开发中常用。
优化:提取版本信息