Maven基础学习笔记
1. Maven简介
1.1 Maven是什么
- Maven的本质是一个项目管理工具,将项目开发过程抽象成一个项目的对象模型(POM)
- POM(Project Object Model):项目对象模型  - 1.2 Maven的作用- 项目构建:提供标准的、跨平台的自动化项目构建方式
- 依赖管理:方便快捷的管理项目依赖的资源(jar包),避免资源间版本冲突问题
- 统一开发结构:提供标准的、统一的项目结构
  
2.下载与安装
2.1 Maven下载
2.2 Maven安装
- Maven 属于绿色版软件,解压即安装
2.3 Maven环境变量配置
- 依赖java,需要配置JAVA_HOME
- 设置Maven自身的环境变量,需要配置MAVEN_HOME  - 并在Path下添加 - %MAVEN_HOME%\bin- 测试配置结果 - mvn
 
3. Maven基础概念
3.1 仓库
- 仓库:用于存储资源,包含各种jar包
- 仓库分类: - 本地仓库:自己电脑上存储资源的仓库,连接远程仓库获取资源
- 远程仓库:非本机电脑的仓库,为本地仓库提供资源 - 中央仓库:Maven团队维护,存储所有资源的仓库
- 私服: 部门/公司范围内存储资源的仓库,从中央仓库获取资源
 
- 私服的作用: - 保存具有版权的资源,包含购买或自主研发的jar包 - 中央仓库的jar都是开源的,不能存储具有版权的资源
 
- 一定范围内共享资源,仅对内部开放,不对外共享
 
 

3.2 坐标
- 什么是坐标? - maven中的坐标用于描述资源的位置
- https://repo1.maven.org/maven2/
 
- Maven坐标组成 - groupId:定义当前Maven项目隶属于组织名称(通常是域名反写,例如:org.mybatis)
- artifactld:定义当前Maven项目名称(通常是模块名称,例如CRM\SMS)
- version:定义当前项目版本号
- packing:定义项目的打包方式
- 示例:来自https://mvnrepository.com/
 - <!-- https://mvnrepository.com/artifact/junit/junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13.2</version> <scope>test</scope> </dependency>
- Maven坐标的作用 - 使用唯一标识,唯一性定位资源位置,通过该标识可以将资源的识别与下载工作交给机器(maven工具)来完成
 
4.仓库配置
4.1 本地仓库配置
- 修改本地仓库的位置 - 默认位置: - ${user.home}/.m2/repository
 
- 自定义位置 - 1.新建好本地文件夹
- 2.修改%MAVEN_HOME%/conf/bin/settings.xml
 - <!-- localRepository | The path to the local repository maven will use to store artifacts. | | Default: ${user.home}/.m2/repository <localRepository>/path/to/local/repo</localRepository> --> # 在下方添加如下内容 <localRepository>/path/to/local/repo</localRepository>
 
4.2 远程仓库配置镜像
- 在setting文件中配置阿里云镜像仓库 - 修改%MAVEN_HOME%/conf/bin/settings.xml
 - <mirrors> # 在这里面添加 <mirror> <id>alimaven</id> <name>aliyun maven</name> <url>https://maven.aliyun.com/repository/public</url> <mirrorOf>central</mirrorOf> </mirror> </mirrors>
- 修改
5. 第一个Maven项目(手工制作)
5.1 Maven工程目录结构

- 在src同层目录下创建 - pom.xml文件- <!-- 属性 --> <properties> <spring.version>4.2.4.RELEASE</spring.version> <hibernate.version>5.0.7.Final</hibernate.version> <struts.version>2.3.24</struts.version> </properties> <!-- 锁定版本,struts2-2.3.24、spring4.2.4、hibernate5.0.7 --> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>${hibernate.version}</version> </dependency> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-core</artifactId> <version>${struts.version}</version> </dependency> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-spring-plugin</artifactId> <version>${struts.version}</version> </dependency> </dependencies> </dependencyManagement> <!-- 依赖管理 --> <dependencies> <!-- spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> </dependency> <!-- hibernate --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> </dependency> <!-- 数据库驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.6</version> <scope>runtime</scope> </dependency> <!-- c3p0 --> <dependency> <groupId>c3p0</groupId> <artifactId>c3p0</artifactId> <version>0.9.1.2</version> </dependency> <!-- 导入 struts2 --> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-core</artifactId> </dependency> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-spring-plugin</artifactId> </dependency> <!-- servlet jsp --> <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> <!-- 日志 --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.2</version> </dependency> <!-- junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.9</version> <scope>test</scope> </dependency> <!-- jstl --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> </dependencies> <build> <plugins> <!-- 设置编译版本为1.7 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.7</source> <target>1.7</target> <encoding>UTF-8</encoding> </configuration> </plugin> <!-- maven内置 的tomcat6插件 --> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>tomcat-maven-plugin</artifactId> <version>1.1</version> <configuration> <!-- 可以灵活配置工程路径 --> <path>/Hello</path> <!-- 可以灵活配置端口号 --> <port>8080</port> </configuration> </plugin> </plugins> </build>
5.2 Maven项目构建命令
- Maven构建命令使用mvn开头,后面添加功能参数,可以一次执行多个命令,使用空格分隔 - mvn compile #编译 mvn clean #清理 mvn test #测试 mvn package #打包 mvn install #安装到本地仓库
- 手动执行的话在pom.xml文件目录下执行
6.第一个Maven项目(IDEA生成)
7.依赖管理
7.1 添加依赖
- 在pom.xml文件里的<dependencies></dependencies>中添加<dependency></dependency>
7.2 依赖传递
- 依赖具有传递性 - 直接依赖:在当前项目中通过依赖配置建立的依赖关系
- 间接依赖:被资源的资源如果依赖其他资源,当前项目间接依赖其他资源  
 
7.3 依赖传递冲突问题
- 路径优先:当依赖中出现相同资源时,层级越深,优先级越低,层级越浅,优先级越高
- 声明优先:当资源在相同层级被依赖时,配置顺序靠前的覆盖顺序靠后的
- 特殊优先:当同级配置了相同资源的不同版本,后配置的覆盖先配置的(在同一个pom.xml文件中进行配置)

7.4 可选依赖与排除依赖
- 可选依赖 - 控制资源不被调用者看到具体的依赖情况
 
- 排除依赖 - 指主动断开依赖的资源,被排除的资源无需要指定具体的版本--不需要
  
7.5 依赖范围
- 依赖的jar默认情况可以在任何地方使用,可以通过scope标签设定其作用范围
- 作用范围 - 主程序范围内有效(main文件夹范围内)
- 测试程序范围有效(test文件夹范围内)
- 是否参与打包(package指令范围内)
  
- 依赖范围传递性(了解即可) - 带有依赖范围的资源在进行传递时,作用范围将受到影响
  
8.生命周期与插件
8.1 项目构建生命周期
- maven构建生命周期描述的是一次构建过程经历了多少个事件

- maven对项目构建的生命周期划分为3套 - clean:清理工作
- default:核心工作,例如编译、测试、打包、部署等
- site:产生报告,发布站点等
 
8.2 插件
- 插件与生命周期内的阶段绑定,在执行到对应生命周期时执行对应的插件功能
- 默认maven在各个生命周期上绑定有预设的功能
- 通过插件可以自定义其他功能  
 
    
评论 (0)