JavaWeb_03 Maven

3、Maven

我们为什么要学这个技术?

  1. 在javaweb开发中,需要导入大量jar包,需要手动导入
  2. 如何能够让一个东西帮我导入和配置这个jar包。

3.1、Maven项目架构管理工具

目前,我们就是方便导入jar包的!

Maven的核心思想:约定大于配置

  • 有约束,不要去违反

Maven会规定好你该如何去编写Java代码,必须按这个规范!!

3.2、下载安装Maven

http://maven.apache.org/

3.3、配置环境变量

M2_HOME maven目录下的bin目录

MAVEN_HOME maven的目录

系统环境变量path中配置 %MAVEN_HOME%\bin

测试maven是否安装成功

在这里插入图片描述

3.4、阿里云镜像

  • mirrors
    • 作用:加速我们下载
  • 国内建议使用阿里云镜像
<mirror>
        <id>alimaven</id>
		<name>aliyun maven</name> 
		<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
		<mirrorOf>*</mirrorOf> 
</mirror>

在这里插入图片描述

3.5、本地仓库

本地仓库

远程仓库

建立一个本地仓库:localRepository

<localRepository>E:\environment\apache-maven-3.6.3\maven-repository
</localRepository>
  用自己的路径

在这里插入图片描述

3.6、在idea中使用maven

创建一个maven项目(使用模板建的)
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

自动导入包
在这里插入图片描述
这个只有在web应用下才会有
在这里插入图片描述

一个干净的maven项目,不用模板创建的!!!

在这里插入图片描述

3.7、在idea中配置tomcat

在这里插入图片描述
解决警告问题:

为什么会有这个问题?我们访问一个网站,需要指定一个文件夹

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

3.8、pom文件

pom.xml文件是maven的核心配置文件

在这里插入图片描述

<?xml version="1.0" encoding="UTF-8"?>
<!--maven版本和头文件-->
<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>
<!--刚才配置的GAV-->
  <groupId>com.hao</groupId>
  <artifactId>Javaweb-01-maven</artifactId>
  <version>1.0-SNAPSHOT</version>
  <!--项目打包方式
  jar:java应用
  war:javaweb应用
  -->
  <packaging>war</packaging>

  <name>Javaweb-01-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>
  </dependencies>

  <build>
    <finalName>Javaweb-01-maven</finalName>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-war-plugin</artifactId>
          <version>3.2.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

由于maven约定>配置,之后可能遇到我们写的配置文件无法导出问题

解决方案:

在build中配置resources,来防止我们资源导出失败的问题

<!--在build中配置resources,来防止我们资源导出失败的问题-->
<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <excludes>
                <exclude>**/*.properties</exclude>
                <exclude>**/*.xml</exclude>
            </excludes>
            <filtering>false</filtering>
        </resource>
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
            </includes>
            <filtering>false</filtering>
        </resource>
    </resources>
</build>

猜你喜欢

转载自blog.csdn.net/weixin_45148145/article/details/115491491