Maven 的创建(重要)


在这里插入图片描述

Maven配置

环境变量:
在这里插入图片描述
path路径下的配置
在这里插入图片描述
测试:win+R 输入mvn -v
在这里插入图片描述
这样就显示成功了
阿里云镜像配置

 <mirror>
         <id>nexus-aliyun</id>
         <mirrorOf>central</mirrorOf>
         <name>Nexus aliyun</name>
         <url>https://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>

在150行左右把原来的替换掉就行

本地仓库

建立一个本地仓库
在这里插入图片描述

在IDEA中使用Maven

第一步
在这里插入图片描述
第二步
在这里插入图片描述
第三步
在这里插入图片描述
第四步
项目搭建成功
出现build success
在这里插入图片描述
第五步
IDEA中Maven的设置
注意创建好后,看一眼
在这里插入图片描述
第六步
在这里插入图片描述
第七步
在这里插入图片描述

在这里插入图片描述
这个只有在web应用下才会有
在这里插入图片描述
第八步
标记文件夹功能
在这里插入图片描述
或者这样
在这里插入图片描述
在这里插入图片描述
第九步
在这里插入图片描述
第十步
我们访问一个网站需要指定一个文件夹名字
在这里插入图片描述
第十一步

在这里插入图片描述

一定要按照照片步骤一个一个来

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.maven</groupId>
  <artifactId>maven</artifactId>
  <version>1.0-SNAPSHOT</version>
<!--  项目的打包方式
      jar:java打包方式
      war:javaweb打包方式
      -->
  <packaging>war</packaging>

  <name>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.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>
<!--项目依赖-->
  <dependencies>
<!--    具体依赖的jar包配置文件-->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
<!--项目构建用的东西-->
  <build>
    <finalName>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的高级之处在于,他会帮你导入这个JAR包所依赖的其他jar包
maven由于他的约定大于配置,我们之后可以能遇到我们写的配置文件,无法被导出或者生效的问题,解决方案:

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

在这里插入图片描述

解决Maven中遇到的问题

1.Maven 3.6.2
解决方法:降级为 3.6.1
在这里插入图片描述
2.Tomcat 闪退

3.IDEA中每次都要重复配置Maven
在IDEA中的全局默认配置中去配置
在这里插入图片描述
在这里插入图片描述
4.Maven 默认 Web 项目中的 web.xml 版本问题
在这里插入图片描述
5.替换为 webapp4.0 版本和 Tomcat 一致
测试一下
创建一个java类

package com.test;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class HelloServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse response) throws ServletException, IOException {
        //响应的类型:html
        response.setContentType("text/html");
        //获取响应的输出流
        PrintWriter out = response.getWriter();
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Hello World!</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<h1>Hello World!</h1>");
        out.println("</body>");
        out.println("</html>");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}

在重写一遍web.xml


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                      http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0"
         metadata-complete="true">

<!--web.xml中是配置我们web的核心应用-->
<!--    注册servlet-->
    <servlet>
        <servlet-name>helloServlet</servlet-name>
        <servlet-class>com.test.HelloServlet</servlet-class>
    </servlet>
<!--一个servlet对应一个Mapping:映射-->
    <servlet-mapping>
        <servlet-name>helloServlet</servlet-name>
<!--        请求路径-->
        <url-pattern>/hello</url-pattern>
    </servlet-mapping>
</web-app>

运行一下
localhost:8080
或者
localhost:8080/hello
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值