什么是maven
是Apache下的一个java开发的一个项目,并且还是开源的
说白了,就是一个项目管理工具,包含了一个项目的对象模型
(pom,project Object Model) 一组标准的集合,
使用Maven 对项目中的jar的依赖处理特别的方便,只是需要导入主要的jar坐标,那么
其他相关的依赖的jar就会自动的导入
maven 可以处理的问题
jar包的冲突,
单元测试 打包 ...
我们编写的项目会经历
编译 -> 测试 -> 运行 - > 打包 - > 安装 -> 部署 一些列的工作

Maven 3.3+ 的版本 需要jdk1.7+来进行构建
那也就是说安装Maven的使用需要使用jdk的环境
*安装 *
必须要有jdk的环境
进行配置maven的环境变量
key :M2_HOME
values: D:\Maven\apache-maven-3.5.4-bin\maven\apache-maven-3.5.4
然后在path中进行配置指定maven安装目录中的bin
%M2_HOME%\bin
查询版本
C:\Users\admin>mvn -version
测试jar下载的命令
C:\Users\admin>mvn help:system
------------------------------------
*配置本地仓库 *
在\Maven\apache-maven-3.5.4-bin\maven\apache-maven-3.5.4\conf\settings.xml
<localrepository>E:\mlRepository</localrepository>
是否离线使用maven

<interactiveMode>true</interactiveMode>
<offline>false</offline>

是本地仓库
用来进行存储从远程仓库(中央仓库)中进行下载的jar包或者插件
在项目中使用这些jar包或者插件,优先使用本地仓库中的jar
如本仓库中没有从远程仓库中下载到本地使用
中央仓库
在maven 内置了一个中央仓库,服务于整格互联网的
该中央仓库是maven 团队进行维护的,存储了很多得jar和插件
一般情况下,我们不会使用自带的仓库jar下载地址,(会进行使用***:阿里的)
进行配置
jar下载的地址 - 使用哪里云的
<mirror>
<id>nexus-aliyun</id>
<mirrorof>central</mirrorof>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>

如在\Maven\apache-maven-3.5.4-bin\maven\apache-maven-3.5.4\conf\settings.xml 配置的是全局的 需要进行配置局部的(当前用户的) 就是将settings.xml 复制一份到C:\Users\admin\.m2目录下(重启电脑) 优先加载当前用户的,然后在是全局的 -------------------- 到CMD中进行创建maven工程 E:\123>mvn archetype:generate ................ .................. vy-2.4.16.jar (4.7 MB at 37 kB/s) [INFO] Generating project in Interactive mode [WARNING] No archetype found in remote catalog. Defaulting to internal catalog [INFO] No archetype defined. Using maven-archetype-quickstart (org.apache.maven.archetypes:maven-archetype-quickstart:1.0) Choose archetype: 1: internal -> org.apache.maven.archetypes:maven-archetype-archetype (An archetype which contains a sample archetype.) 2: internal -> org.apache.maven.archetypes:maven-archetype-j2ee-simple (An archetype which contains a simplifed sample J2EE application.) 3: internal -> org.apache.maven.archetypes:maven-archetype-plugin (An archetype which contains a sample Maven plugin.) 4: internal -> org.apache.maven.archetypes:maven-archetype-plugin-site (An archetype which contains a sample Maven plugin site. This archetype can be layered upon an existing Maven plugin project.) 5: internal -> org.apache.maven.archetypes:maven-archetype-portlet (An archetype which contains a sample JSR-268 Portlet.) 6: internal -> org.apache.maven.archetypes:maven-archetype-profiles () 7: internal -> org.apache.maven.archetypes:maven-archetype-quickstart (An archetype which contains a sample Maven project.) 8: internal -> org.apache.maven.archetypes:maven-archetype-site (An archetype which contains a sample Maven site which demonstrates some of the supported document types like APT, XDoc, and FML and demonstrates how to i18n your site. This archetype can be layered upon an existing Maven project.) 9: internal -> org.apache.maven.archetypes:maven-archetype-site-simple (An archetype which contains a sample Maven site.) 10: internal -> org.apache.maven.archetypes:maven-archetype-webapp (An archetype which contains a sample Maven Webapp project.) Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): 7 Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): 7: 10 ................. etype-webapp/1.0/maven-archetype-webapp-1.0.jar (3.9 kB at 9.0 kB/s) Define value for property 'groupId': com.maven.chen #公司名或者组织名称 Define value for property 'artifactId': daylan #项目名 Define value for property 'version' 1.0-SNAPSHOT: : #版本信息 Define value for property 'package' com.maven.chen: : #报名 Confirm properties configuration: groupId: com.maven.chen artifactId: daylan version: 1.0-SNAPSHOT package: com.maven.chen Y: : y #确定是否继续操作 [INFO] ---------------------------------------------------------------------------- [INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-webapp:1.0 [INFO] ---------------------------------------------------------------------------- [INFO] Parameter: basedir, Value: E:\123 [INFO] Parameter: package, Value: com.maven.chen [INFO] Parameter: groupId, Value: com.maven.chen [INFO] Parameter: artifactId, Value: daylan [INFO] Parameter: packageName, Value: com.maven.chen [INFO] Parameter: version, Value: 1.0-SNAPSHOT [INFO] project created from Old (1.x) Archetype in dir: E:\123\daylan [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 06:48 min [INFO] Finished at: 2021-01-07T09:40:59+08:00 [INFO] ------------------------------------------------------------------------

pom.xml 进行配置对应jar包的坐标的
resources 资源目录(进行配置数据源的连接信息,)
webapp web相关的代码
WEB-INF
index.jsp
如上创建的maven web工程的目录是不完整的
pom.xml
src
main
java 项目代码资源文件如bo dao service ...
resources
webapp
WEB-INF
index.jsp
test
java 测试相关的代码
resources 测试相关的资源文件
target 编译之后的代码


1,构建前的清理工作
2.初始化
3.编译
4.测试-单元
5.打包
6.集成测试
7.发布
8.生成站点


编译命令
E:\123\daylan>mvn compile
第一次出现异常
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent!
[INFO] Compiling 1 source file to E:\123\daylan\target\classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] 不再支持源选项 5。请使用 7 或更高版本。
[ERROR] 不再支持目标选项 5。请使用 7 或更高版本。
[INFO] 2 errors
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.843 s
[INFO] Finished at: 2021-01-07T10:40:36+08:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project daylan: Compilation failure: Compilation failure:
[ERROR] 不再支持源选项 5。请使用 7 或更高版本。
[ERROR] 不再支持目标选项 5。请使用 7 或更高版本。
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
进行修改编译jdk版本
<profile>
<id>jdk‐1.8</id>
<activation>
<activebydefault>true</activebydefault>
<jdk>1.8</jdk>
</activation>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>
</profile>
注意,全局和局部要一致
E:\123\daylan>mvn test
[INFO] Scanning for projects...
[INFO]
[INFO] -----------------------< com.maven.chen:daylan >------------------------
[INFO] Building daylan Maven Webapp 1.0-SNAPSHOT
[INFO] --------------------------------[ war ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ daylan ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ daylan ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ daylan ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ daylan ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ daylan ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.904 s
[INFO] Finished at: 2021-01-07T10:43:57+08:00
[INFO] ------------------------------------------------------------------------
清理 -删除之前操作的
E:\123\daylan>mvn clean
[INFO] Scanning for projects...
[INFO]
[INFO] -----------------------< com.maven.chen:daylan >------------------------
[INFO] Building daylan Maven Webapp 1.0-SNAPSHOT
[INFO] --------------------------------[ war ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ daylan ---
[INFO] Deleting E:\123\daylan\target
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.257 s
[INFO] Finished at: 2021-01-07T10:45:36+08:00
[INFO] ------------------------------------------------------------------------
打包
E:\123\daylan>mvn package
[INFO] Scanning for projects...
[INFO]
[INFO] -----------------------< com.maven.chen:daylan >------------------------
[INFO] Building daylan Maven Webapp 1.0-SNAPSHOT
[INFO] --------------------------------[ war ]---------------------------------
Downloading from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/
安装
E:\123\daylan>mvn install
[INFO] Scanning for projects...
[INFO]
发布
mvn deploy


** Maven 指令的生明周期**
有三套,并且是独立的
Clean
在进行真正的构建之前的一些清理工作
pre-clean 清理前
clean 清理阶段
post-clean 清理后
default
编译 测试 打包 部署
validate 验证
compile 编译
test 测试
package 打包
deploy 发布
Site
生成项目报告,站点
pre site 生成站点前
site 生成站点
post site 生成站点后
site deploy 发布站点


Maven的依赖管理
通过Maven的依赖的管理,对项目中的所有的jar包的版本、插件 进行统一的管理
描述坐标
<groupid>com.java.ch</groupid>
<artifactid>test_mv_02</artifactid>
<version>1.0-SNAPSHOT</version>
<scope>provided</scope>

<type>jar</type>
<!--
依赖的类型
jar 普通的jar包
pom 父级的一个公共的资源
war web运行的
javadoc 文档描述
java-source 源码
test-jar 测试

        -->
          <!--            <classifier>jdk12</classifier>  
                                  指定jdk的版本
        -->

依赖的调用
原则1.
最短的路径优先
如某个项目下依赖关系的
A-B-X(1.0) 如artifactId为 spring-spring-aop(1.0)
D-X(2.0)如artifactId为 spring-aop(2.0)
原则2
先声明者优先
A-B-X(1.0) 如artifactId为 spring-spring-aop(1.0)
C-D-X(2.0) 如artifactId为 mybatis-status-aop(2.0)
由于长度一样
此刻(1.0)在(2.0)之前的,那么就使用(1.0)
排除依赖
如项目A依赖于项目B ,项目B依赖于项目C但是A不想用C ,这个使用就需要排除了

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>5.2.9.RELEASE</version>
    <!--    <classifier>jdk8</classifier>-->
         <!--排除依赖-->
        <exclusions>
             <exclusion>
                 <groupId>org.springframework</groupId>
                 <artifactId>spring-core</artifactId>
             </exclusion>
         </exclusions>
    </dependency>

依赖的归类

<properties>
    <!-- <spring-version>5.2.9.RELEASE</spring-version>-->
     <spring-version>4.3.18.RELEASE</spring-version>
     <mybaties-version></mybaties-version>
</properties>
<dependencies>
       <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
        <version>${spring-version}</version>

    </dependency>

<properties>
   <spring-version>5.2.9.RELEASE</spring-version>
    <!--<spring-version>4.3.18.RELEASE</spring-version>-->
    <mybaties-version>3.4.6</mybaties-version>
</properties>

<!--依赖归类成POM-->
<dependencyManagement>
     <dependencies>
         <dependency>
             <groupId>org.springframework</groupId>
             <!--定义-->
             <artifactId>spring-framework-bom</artifactId>
             <version>${spring-version}</version>
             <type>pom</type>
             <scope>import</scope>
         </dependency>
         <dependency>
             <groupId>org.mybatis</groupId>
             <!--定义-->
             <artifactId>mybatis</artifactId>
             <version>${mybaties-version}</version>
         <!--    <type>pom</type>
             <scope>import</scope>-->
         </dependency>

     </dependencies>
</dependencyManagement>
<dependencies>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
        <!--   <version>${spring-version}</version>-->

    </dependency>
    <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
    </dependency>

<!--定义编译与字符集-->                                  
<properties>                                           
    <project.build.sourceEncoding>utf-8</project.build.
    <maven.comilper.source>1.8</maven.comilper.source> 
    <maven.comilper.target>1.8</maven.comilper.target> 
   <spring-version>5.2.9.RELEASE</spring-version>      
    <!--<spring-version>4.3.18.RELEASE</spring-version>
    <mybaties-version>3.4.6</mybaties-version>         
</properties>