image-20200813094303377

1.为什么要使用maven?

1、节省空间

  • 普通传统项目

image-20200813094831880

  • Maven项目

image-20200813094850918

l 为什么maven项目占用的空间这么小[e1] 呢?

项目中没有jar包

l Maven项目需要jar包吗[e2] ?

必须要

l 如果需要jar包,那么jar包在何处?[e3]

都在maven仓库,在本地仓库,或者远程仓库。

2、依赖管理

  • 传统开发中,项目中需要使用任何jar包,可以直接使用项目中的,但是使用了maven之后,因为jar不在项目中了,全部依托在maven的仓库中,所以项目需要仅仅依赖于maven仓库。

  • 多个项目只需要共享一份maven仓库的jar包即可。

总结:使用maven,方便于对jar的管理

3、项目的一键式构建

编码、编译、测试(junit)、运行、打包、部署

在maven的世界中,只需要一个 mvn tomcat:run就能把项目跑起来

Maven能干的事情:

编译、测试(junit)、运行、打包、部署

4、跨平台

因为java是可以跨平台的

2、Maven是什么

Maven是Apache旗下的一个开源项目,是纯java开发,并且只能用来管理java项目以及jar包的。

3、如何是应用maven

1、下载maven

官网地址:https://maven.apache.org/

官网下载地址:https://maven.apache.org/download.cgi

image-20200813095554772

image-20200813095526725

image-20200813095704926

2、安装maven

image-20200813095834843

直接解压到一个没有中文没有空格的文件夹里解压后的目录如下:

image-20200813095933422

Maven软件的核心文件:

E:\webstudy\maven\apache-maven-3.6.3\conf

image-20200813100023794

3、配置maven仓库

1.为什么要配置maven仓库
2、maven仓库的类型

maven仓库有三种类型:

默认情况下,maven会自动去登录当前操作系统的账号目录下的叫做.m2文件夹下的respository寻找jar包

例如: C:\Users\sunfei.m2\repository

  • 本地仓库(自己维护):本地仓库的配置,只需要修改maven软件目录中setting.xml文件就可以了

image-20200813100639750

1
<localRepository>E:\webstudy\maven\apache-maven-3.6.3\maven-repo</localRepository>
  • 远程仓库(私服)-公司团队维护
  • 中央仓库-(maven团队维护)

阿里仓库属于代理

image-20200813100801806

3、配置远程仓库

image-20200813101505324

1
2
3
4
5
6
<mirror>
<id>nexus-aliyun</id>
<mirrorOf>*,!jeecg,!jeecg-snapshots</mirrorOf>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>
4、配置环境变量

1、添加系统变量

image-20200813100957265

2、在系统变量path添加如下内容

image-20200813101053738

4、eclipse配置maven

安装m2e插件(maven插件)

在ecplise开发工具内部已经集成好了maven插件,我们只需要配置

1、修改eclipse中默认maven软件版本

image-20200813105159687

image-20200813105311519

2、修改ecplise中默认的maven本地仓库

image-20200813105331200

5、eclipse创建maven项目

image-20200813110022370

image-20200813110128765

  • 如果不勾选create a simple project (skip) archetype selection 选项 - 没有跳过骨架模板,那么创建的maven项目,是不完整的 会少一些默认的文件夹

  • 如果勾选create a simple project (skip) archetype selection 选项 - 跳过骨架模板

  • 那么创建的maven项目,项目的目录结构 是很完整的

    不勾选是显示的页面

image-20200813103718373

image-20200813104044987

勾选之后显示出来的完整工程

image-20200813104442943

image-20200813105033039

处理maven的编译版本

image-20200813113847392

在pom.xml中加入;在当前的maven项目目录下,进行处理。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<build>
<!-- 配置了很多插件 -->
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>

创建一个servlet

1.导入jar包,也就是用maven坐标方式导入

image-20200813112506808

手动在webapp文件夹下创建一个WEB-INF文件夹,在里面放一个web.xml文件;maven无法自己创建.

maven坐标详解

image-20200813110322450

pom.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<project xmlns="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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.gec.ceshi</groupId>
<artifactId>day0813</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>first</name>
<description>first_test</description>

<!-- 添加servlet-api,jsp-api -->
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.9</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
</dependencies>

<build>
<!-- 配置了很多插件 -->
<plugins>
<plugin>
<groupId>com.gec.ceshi</groupId>
<artifactId>day0813</artifactId>
<version>3.6.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>

</project>

在eclipse只需要ctrl+s就会自动导包,将pom.xml中的坐标导入到Maven Dependencies中

image-20200813114814778

运行servlet程序

image-20200813141946386

image-20200813141951029

项目名上右键—->选择RunAs—>maven bulid… ->输入tomcat:run

可能会存在的问题

image-20200813142031468

重新添加一次jdk

6、使用ecplise自动添加依赖jar包

1、添加jar包

1、在maven项目的pom.xml

右击 pom.xml -> maven -> Add Dependency

image-20200813142232642

image-20200813142239274

2、在pom.xml身上

右击 -> Add Dependency

3、在ecplise下方选择

image-20200813142325276

2、创建索引

image-20200813142349402

在local Repository右键选择 ->Rebuild Index

image-20200813142418206

7、聚合、拆分工程

1、为什么要聚合拆分

在软件开发领域中,总体上软件的开发分为两类:传统项目、互联网项目

**传统项目:按照层来分 **

Pojo层、dao层、service层、web层

互联网项目:按照业务来分

例如:京东商城

​ 用户管理、商品管理、订单管理

2、什么是拆分、聚合

  • 那么在开发的时候,我们将每一个模块分开进行开发,充分的发挥团队中每一个人的特长,单独完成一个功能,这种操作称之为拆分工程

  • 那么当每个人都彼此完成自己的单模块的之后,最终需要将每个人的单模块合并到父工程中去,组成一个完整的工程,这种操作称之为聚合工程

3、注意

  • 父工程留下pom.xml文件,主要是方便于父工程旗下所有的子模块通用的jar所对应的坐标集中在父工程中去配置,将来的子模块不需要在自己的pom.xml配置,父工程已经有的通用jar包!
  • 父工程中基本不写任何代码

【注意】:

新建的父工程,一定要检查并且做三件事情

  • 不要使用默认的maven版本,改为我们自己的3.3.9版本

  • 不要使用默认的.m2仓库路径,改为我们自己的仓库。

  • 工程的打包类型改为:pom类型

4、jar包依赖传递

  • 父工程导入jar包,子工程可以自动继承父工程的jar包

  • 子工程之间jar不会互相继承

8、包冲突

因为不同的jar中很多时候,会导入一些相同的jar包,那么这时候,程序去执行的时候,并不能指导到底应该去执行哪个jar包下面对应的jar版本,必然会出错。

留下高版本,去除低版本

1.去除jar(重点掌握)

直接在需要排除的jar包右键

image-20200813153636448

2.版本锁定(了解)

方式一:

image-20200813154721164

注意观察:导入jar包,在选择jar包的时候,有一个 🔒 的标识

image-20200813155707410

  • 会在锁定的jar包就加上一把锁的标志

  • 而且这并不是引入jar包,引入jar包还是需要加入

    image-20200813155101025

  • 如果还是引入其他版本也是可以的,会出现override警告。

方式二:

提取常量放入pom.xml的属性中

1
2
3
4
5
6
<properties>
  <spring.version>4.2.4.RELEASE</spring.version>
<hibernate.version>5.0.7.Final</hibernate.version>
  <struts.version>2.3.24</struts.verson>

</properties>

引入属性通过ognl表达式带入到依赖的版本中:例如:${spring.version}

注意: properties中key名可以任意,但是必须要保证properties中key和下面dependency中的version中的value值名称一致,否则就找不到对应的jar包,因为坐标不一致

9、maven中jar包版本调解

当一个项目依赖的构件比较多时,它们相互之前存在依赖,当你需要对依赖版本统一管理时如果让maven自动来处理可能并不能如你所愿:

举个栗子:

org.apache.struts依赖spirng-beans-3.0.5,

spring-context依赖spring-beans-4.2.4,

但是发现spirng-beans-3.0.5加入到工程中,而我们希望spring-beans-4.2.4加入工程。

同时加入以下依赖,观察依赖:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<!--  struts2-spring-plugin依赖spirng-beans-3.0.5 				-->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>2.3.24</version>
</dependency>


<!-- spring-context依赖spring-beans-4.2.4 -->

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>

在pom文件定义依赖,先声明的依赖为准

测试:

如果将上边struts-spring-plugins和spring-context顺序颠倒,系统将导入spring-beans-4.2.4。

分析:

由于spring-context在前边以spring-context依赖的spring-beans-4.2.4为准,所以最终spring-beans-4.2.4添加到了工程中。

总之:发生冲突时,默认是以pom.xml文件由上到下的顺序优先,并且只导入第一个。

10、如何处理maven项目没有使用的jar包

直接在pom.xml文件的代码中注释掉maven项目中未使用的jar包依赖代码,如果是父子工程依赖,那么在父工程中注释掉依赖代码即可

11、导入maven工程

1.选择导入再选择导入maven工程

image-20200813201551168

2.导入的工程要选择父工程

image-20200813201605518

12、其他资料

https://www.jianshu.com/p/c27eacf92209 maven 配置阿里云服务器 报错问题

解决Failure to transfer org.apache.maven.plugins:maven-surefire-plugin:pom:2.12.4问题

有时候我们的maven工程的pom.xml文件第一行会报如下错误:

Failure to transfer org.apache.maven.plugins:maven-surefire-plugin:pom:2.12.4 from https://repo.maven.apache.org/maven2 was cached in the local repository,resolution will not be reattempted until the update interval of central has elapsed or updates are forced.Original error: Could not transfer artifact org.apache.maven.plugins:maven-surefire-plugin:pom:2.12.4 from/to central (https://repo.maven.apache.org/maven2):

The operation was cancelled.

可以尝试以下方法解决

  • 1.找到maven库目录,进入:\org\apache\maven\plugins\maven-surefire-plugin\2.12.4

  • 2.若2.12.4目录下有maven-surefire-plugin-2.12.4.pom.lastUpdated文件,将其删除

  • 3.项目右键–>maven–>Update Dependencies

https://www.jianshu.com/p/83400460d1a0 =》 maven 和ssm使用