Maven
Maven的本质是项目管理工具 将项目开发和管理过程抽象成一个项目对象模型(POM)
Maven的作用:
- 项目构建:提供标准的、跨平台的自动化项目构建方式
- 依赖管理:方便快捷的管理项目依赖的资源(jar包) 避免资源间的版本冲突问题
- 统一开发结构:提供标准的 统一的项目结构
基础概念
仓库:用于存储资源,包含各种jar包
仓库分类:
- 本地仓库:自己电脑上存储资源的仓库 连接远程仓库获取资源
- 远程仓库:非本机电脑上的仓库 为本地仓库提供资源
- 中央仓库:Maven团队维护 存储所有资源的仓库
- 私服:部门、公司范围内存储资源的仓库 从中央仓库获取资源
私服的作用:
- 保存具有版权的资源 包含购买或自主研发的jar
- 中央仓库中的jar都是开源的 不能存储具有版权的资源
- 一定范围内共享资源 仅对内部开放 不对外共享
坐标
什么是坐标?Maven中的坐标用于描述仓库中资源的位置
作用:使用唯一标识 唯一性定位资源位置 通过该标识可以将资源的识别与下载工作交由机器完成
主要组成:
- groupId:定义当前Maven项目隶属组织名称
- artifactId:定义当前Maven项目名称
- version:定义当前项目版本号
- packaging:定义该项目的打包方式
配置本地仓库位置
选择合适的地方 创建文件夹maven
在其内部创建文件夹maven-repo
之后修改conf
目录下的setting.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>C:\environment\maven\maven-repo</localRepository>
镜像仓库的配置
lib
目录下maven-model-builder-3.8.1
有一个pom-4.0.0.xml
内部是远程仓库的配置
<repositories>
<repository>
<id>central</id>
<name>Central Repository</name>
<url>https://repo.maven.apache.org/maven2</url>
<layout>default</layout>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
我们需要在conf
目录下的setting.xml
为远程仓库做一个镜像仓库
<mirrors>
<!-- mirror
| Specifies a repository mirror site to use instead of a given repository. The repository that
| this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
| for inheritance and direct lookup purposes, and must be unique across the set of mirrors.
|
<mirror>
<id>mirrorId</id>
<mirrorOf>repositoryId</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>http://my.repository.com/repo/path</url>
</mirror>
-->
<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>
</mirrors>
Maven指令
- mvn compile 编译
- mvn clean 清理
- mvn test 测试
- mvn package 打包
- mvn install 安装到本地仓库
IDEA创建Maven工程
首先在IDEA中配置Maven
接下来创建Maven工程
在pom.xml
中添加依赖
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
点击刷新 查看是否添加成功
可以通过配置maven的运行配置
也可以通过右侧的命令来运行
使用IDEA骨架
创建Java工程
其余和之前一样 只需要在初始时选择骨架
以下配置默认即可
创建web工程
选择默认配置
修改目录结构
tomcat插件安装
在pom.xml中添加插件
<build>
<!--设置插件-->
<plugins>
<!--具体插件-->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.1</version>
</plugin>
</plugins>
</build>
右侧出现插件即安装成功
此时点击插件下的tomcat7:run
即可启动
也可以对这个插件进行配置
如果需要修改端口或者路径
<build>
<!--设置插件-->
<plugins>
<!--具体插件-->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.1</version>
<configuration>
<port>80</port>
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>
xml文件解析
<?xml version="1.0" encoding="UTF-8"?>
<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/maven-v4_0_0.xsd">
<!--指定pom的模型版本-->
<modelVersion>4.0.0</modelVersion>
<!--打包方式-->
<packaging>war</packaging>
<name>web</name>
<!--组织id-->
<groupId>com.chenxiii</groupId>
<!--项目id-->
<artifactId>web</artifactId>
<!--版本号-->
<version>1.0-SNAPSHOT</version>
<!--设置当前工程的所有依赖-->
<dependencies>
</dependencies>
<build>
<!--设置插件-->
<plugins>
<!--具体插件-->
<plugin>
<!-- https://mvnrepository.com/artifact/org.apache.tomcat.maven/tomcat7-maven-plugin -->
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.1</version>
<configuration>
<port>80</port>
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>
</project>
依赖管理
配置自己写的依赖
<!--设置当前工程的所有依赖-->
<dependencies>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!--配置自己写的工程-->
<dependency>
<groupId>com.chenxiii</groupId>
<artifactId>mvn</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
依赖具有传递性
- 直接依赖:在当前项目中通过依赖配置的建立的依赖关系
- 间接依赖:被调用的资源如果依赖其它资源 当前项目间接依赖其它资源
依赖冲突:
- 路径优先:当依赖中出现相同的资源时 层级越深 优先级越低 层级越浅 优先级越高
- 声明优先:当资源在相同层级被依赖时 配置顺序靠前的覆盖配置顺序靠后的
- 特殊优先:当同级配置了相同资源的不同版本 后配置的覆盖先配置的
可选依赖:可选依赖是值对外隐藏当前所依赖的资源
添加<optional>true</optional>
外部项目引用此项目时便无法看到该依赖
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<optional>true</optional>
</dependency>
排除依赖:指主动断开依赖的资源 被排除的资源无需指定版本
<!--设置当前工程的所有依赖-->
<dependencies>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
</dependency>
<!--配置自己写的工程-->
<dependency>
<groupId>com.chenxiii</groupId>
<artifactId>mvn</artifactId>
<version>1.0-SNAPSHOT</version>
<!--排除依赖-->
<exclusions>
<exclusion>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
依赖范围:依赖的jar默认可以在任何的地方使用 可以通过scope
标签设定其作用范围
作用范围:
- 主程序范围有效(main文件夹范围内)
- 测试程序范围有效(test文件夹范围内)
- 是否参与打包(package指令范围内)
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.3</version>
<scope>runtime</scope>
</dependency>
可以看出我们配置为runtime
时 它的后方会显示它的作用范围
依赖范围的传递性:带有依赖范围的资源在进行传递的时候 作用范围会受到影响
生命周期与插件
clean:清理工作
default:核心工作 例如编译、测试、打包、部署等
site:产生报告 发布站点等
插件:
- 插件与生命周期内的阶段绑定 在执行到对应的生命周期时执行对应的插件功能
- 默认maven在各个生命周期上绑定有预设的功能
- 通过插件可以自定义其它功能
<plugin>
<groupId>xxx</groupId>
<artifactId>xxx</artifactId>
<version>xxx</version>
<configuration>
<port>80</port>
<path>/</path>
</configuration>
<executions>
<execution>
<!--不是所有插件都要这个标签-->
<goals>
<goal>jar</goal>
</goals>
<!--在什么阶段执行这个插件-->
<phase>generate-test-resources</phase>
</execution>
</executions>
</plugin>