Maven的配置与应用

1.maven介绍

Maven 翻译为"专家" , " 内行" , 是 Apache 下的一个纯 java 开发的开源项目. 基于项目对象模型(缩写: POM) 概念, Maven 利用一个中央信息片段能管理一个项目的构建,报告和文档等步骤.
Maven 是一个项目管理工具, 可以对 java 项目进行构建, 依赖管理
主要作用

  • 项目构建
  • 依赖管理
  • 项目信息管理(因为每家公司都有不用的ck要求这里就不叙述了)

Maven主要掌握的内容是:

一.Maven依赖管理:1.以来范围 . 2.继承性 3.传递性 . 4.依赖调节(包括两大部分:a.第一声明者优先原则. b.路径近者优先原则 3.排除原则)
二.版本的管理 : 1.使用properties声明版本(解决版本混乱问题)
2.锁定依赖
1.在父pom中声明对应的版本,只是声明,不会引入依赖
2.子项目使用父pom声明的依赖时,不需要使用版本

2.maven 的生命周期

Maven 有以下三个标准的生命周期:

  • clean : 项目清理的处理
  • default(或build) : 项目部署的处理
  • site : 项目站点文档创建的处理

图示:

claen : 由自己单独的生命周期
default: 生命周期描述: 执行任意一个字段(序列)的时候,都要其加载之前的字段.
就像自己上大学,那么必须要经历小学–>初中–>高中–>大学这个过程,这也是default 生命周期的特点

生命周期图示
一个典型的Maven构建(build)生命周期是由一下几个阶段的序列组成的:
在这里插入图片描述

3.maven仓库

在Maven 的术语中, 仓库是一个位置(palce)
Maven 仓库是项目中依赖的第三方库 , 这个库所在的位置叫做仓库.
在Maven中, 任何一个依赖, 插件或者项目构建的输出, 都可以称之为构件.
Maven 仓库能帮助我们管理构件.
Maven 仓库能帮助我们管理构件 (主要是JAR), 它就是房主所有JAR文件(WAR , ZIP , POM等等) 的地方
Maven 仓库有三种类型:

  • 本地(local)
  • 中央(center)
  • 远程(remote)
    私服也相当于一个远程仓库本地仓库: C:\User\Admin.m2\repository

配置maven 阿里镜像 settings,xml 修改

<mirrors>
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>
http://maven.aliyun.com/nexus/content/groups/public/
</url>
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>

配置私服 settings.xml修改

<servers>
<server>
<id>docker-hub</id>
<username>wangongzhuo</username>
<password>wangongzhuo0</password>
<configuration>
<email>wangongzhuo@163.com</email>
</configuration>
</server>
</servers>

4.maven 坐标

 **在Maven 世界中, 没一个工程都有他唯一的组织名, 模块名 , 版本 ,  这三个就是maven项目的坐标**
 一个maven 工程可以打包成jar . war . pom 等形式, 但是它们都拥有以上三个坐标的,  我们在项目过程中导入的jar 包就是通过上述坐标值唯一确定的, 因此  ,  我们在创建一个Maven 工程时会要求指定具体的 组织名, 模块名 , 版本  ,  打包方式. 
  groupid  公司或组织的倒叙,项目名
  artifactid  项目模块名
  version 版本号
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>

5.maven插件

1.创建java-maven工程

在这里插入图片描述

在配置maven工程时,可能无法在.m2仓库中无法找到了 idea自带的settings.xml . 那么就需要用自己在本地下载的maven了,需要配置镜像文件,然后在路径设置中,设置自己E:\app\Maven\apache-maven-3.6.0\conf的settings.xml,否则maven工程无法自动加载依赖

配置java-maven工程 的pom.xml

org.example.App 根据目录中的main函数

<?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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>org.example</groupId>
  <artifactId>untitled</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>untitled</name>
  <packaging>jar</packaging>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>

    <!-- 引入mybatis 依赖  -->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.4.5</version>
    </dependency>

    <!--引入jdbc 依赖-->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.6</version>

    </dependency>
  </dependencies>

  <build>
    <plugins>
    <plugin>
      <artifactId>maven-assembly-plugin</artifactId>
      <configuration>
        <archive>
          <manifest>
            <!--配置main 入口函数-->
            <mainClass>org.example.App</mainClass>
          </manifest>
        </archive>
        <descriptorRefs>
          <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
      </configuration>
      <executions>
        <execution>
          <id>make-assembly</id>
          <phase>package</phase>
          <goals>
            <goal>assembly</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
    </plugins>
  </build>
</project>

执行命令
maven clean 清理
mvn package 打出Jar包
mvn install 把jar 复制到本地仓库

创建servlet工程

在这里插入图片描述

配置可执行的java工程 pom.xml

com.aaa.qy114.servlet.HelloServlet

<?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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.aaa</groupId>
  <artifactId>untitled-Maven</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>pom</packaging>

  <name>untitled-Maven Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.0.1</version>
      <!--<scope>provided</scope>-->
    </dependency>
    <dependency>
      <groupId>javax.servlet.jsp</groupId>
      <artifactId>jsp-api</artifactId>
      <version>2.1</version>
     <!-- <scope>provided</scope>-->
    </dependency>

  </dependencies>

  <build>
    <plugins>
    <plugin>
      <artifactId>maven-assembly-plugin</artifactId>
      <configuration>
        <archive>
          <manifest>
            <!--配置main 入口函数-->
            <mainClass>com.aaa.qy114.servlet.HelloServlet</mainClass>
          </manifest>
        </archive>
        <descriptorRefs>
          <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
      </configuration>
      <executions>
        <execution>
          <id>make-assembly</id>
          <phase>package</phase>
          <goals>
            <goal>assembly</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
    </plugins>
  </build>
</project>

创建java和resources文件夹

在这里插入图片描述

执行mvn package 将war包拷贝到tomcat/webapp下运行

2.执行

mvn assembly:assembly

3.测试

在target目录下测试

java -jar mybatis04-1.0-SNAPSHOT-jar-with-dependencies.jar com.wgz.Test

解决中文乱码问题

在pom文件中增加

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

中间涉及到包封装依赖的jar包的和整个工程的应用,脱离IDEA封装到Tomcat容器中步骤就不写; 记得去找自己生产的jar,放到Tomcat的conf 中运行.

打包类型

项目的打包类型:pom、jar、war
packing默认是jar类型,
pom ---------> 父类型都为pom类型 父子工程需要
jar ---------> 内部调用或者是作服务使用
war ---------> 需要部署的项目

7.Maven 依赖管理(特别重要)

<dependencies>
<!--
  将依赖打入到可执行的jar
       <scope>compile</scope>
       <scope>runtime</scope>
       不会打入到jar
       <scope>test</scope>
       <scope>provided</scope>
       compile 编译时需要,运行时也需要
       junit  编译测试时需要,运行时不需要  打入到运行的jar
       provided 编译时需要,运行时不需要,因为累死tomcat 环境已经拥有了servlet-api 此时不需要了    runtime  编译时不需要,运行时需要

-->   
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- 添加 servlet 依赖 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
<scope>runtime</scope>
</dependency>


<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.1.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransfor
mer">
<mainClass>com.wgz.TestDemo.Test</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>


</dependencies>

继承性

当父类工程有依赖是子类则会继承父类的依赖

重新建一个maven工程不选择

在这里插入图片描述

<modules>
<module>projecta</module>
<module>projectb</module>
</modules>
<packaging>pom</packaging>
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
</dependencies>

在这里插入图片描述
在这里插入图片描述

传递性

A依赖B,B依赖C A依赖C称为传递性
当C是test或者provided时,C直接被丢弃,A不依赖C;(排除传递依赖)
否则A依赖C,C的scope继承与B的scope。maven会解析各个依赖的pom,将那些必要的间接依赖,
一传递性依赖的形式引入到当前的项目

在这里插入图片描述
在这里插入图片描述

依赖调解

1.第一声明者优先原则

<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.0.16.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.18.RELEASE</version>
</dependency>
</dependencies>

依赖挂关系(RELEASE 版本问题)

projecta->spring-webmvc-5.0.16.RELEASE->spring-beans-5.0.16.RELEASE
projecta->spring-context-4.3.18.RELEASE->spring-beans-4.3.18.RELEASE

projecta到底使用哪个一个版本的spring-beans?

根据第一声明原则
spring-webmvc-5.0.16.RELEASE 声明在spring-context-4.3.18.RELEASE之前
使用spring-beans-5.0.16.RELEASE

2.路径近者优先原则

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.18.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.0.16.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>

选择最短的路径作为依赖

总结

先使用路径优先原则判断,同等路径使用第一声明原则

3.排除原则 当发生冲突,希望使用自己特殊的版本时还可以使用 标签排除

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.18.RELEASE</version>
<exclusions>
<!-- 虽然满足条件,但是使用exclusions 剔除-->
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.0.16.RELEASE</version>
</dependency>

8.版本的管理

1.使用properties 声明版本 (解决版本混乱问题)

<properties>
<!--在当前pom 或者父类pom 中声明属性 -->
<spirng.version>5.0.16.RELEASE</spirng.version>
</properties>
<dependencies>
<dependency>
<groupId>com.wgz</groupId>
<artifactId>projectb</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<!--调用属性-->
<version>${spirng.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spirng.version}</version>
</dependency>
</dependencies>

2.锁定依赖

1.在父pom中声明对应的版本,只是声明,不会引入依赖

<?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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.wgz</groupId>
<artifactId>maven-parent</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<!--在当前pom 或者父类pom 中声明属性 -->
<spirng.version>5.0.16.RELEASE</spirng.version>
</properties>
<modules>
<module>projecta</module>
<module>projectb</module>
</modules>
<packaging>pom</packaging>
<!--
声明 子项目中使用一下版本
-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<!--调用属性-->
<version>${spirng.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spirng.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>

2.子项目使用父pom声明的依赖时,不需要使用版本

<?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/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>maven-parent</artifactId>
<groupId>com.wgz</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>projecta</artifactId>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>com.wgz</groupId>
<artifactId>projectb</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--因在 父类中有 锁定此时不需要版本号-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
</dependencies>
</project>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值