Maven
为什么要学习这个技术?
-
在 Javaweb 开发中,需要使用大量的 jar 包,我们手动去导入;
-
如何能够让一个东西自动帮我们导入和配置这个 jar 包?
由此,Maven 诞生了!
1、Maven 项目架构管理工具
目前就是为了方便导入 jar 包的!
Maven 的核心思想:约定大于配置
- 有约束,不要去违反。
Maven 会规定好你该如何去编写我们的 Java 代码,必须要按照这个规范来。
2、下载安装Maven
官网:http://maven.apache.org/
下载完成后,解压即可;
3、配置环境变量
在我们的系统环境变量中,配置如下配置:
-
M2_HOME:Maven目录下的 bin 目录
-
MAVEN_HOME:Maven 的目录
-
在系统的 path 中配置 :%MAVEN_HOME%\bin
-
测试 Maven 是否安装成功:
- cmd进入控制台
- 输入mvn -version命令
C:\Users\guancheng>mvn -version Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f) Maven home: D:\java\apache-maven-3.6.3\bin\.. Java version: 11.0.6, vendor: Oracle Corporation, runtime: D:\java\jdk-11.0.6_windows-x64_bin\jdk-11.0.6 Default locale: zh_CN, platform encoding: GBK OS name: "windows 10", version: "10.0", arch: "amd64", family: "windows"
4、阿里云镜像
- 镜像:mirrors
- 作用:加速我们的下载
- 国内建议使用阿里云的镜像
<mirror>
<id>nexus-aliyun</id>
<mirrorOf>central</mirrorOf>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>
5、本地仓库
建立一个本地仓库:localRepository
<localRepository>D:\java\apache-maven-3.6.3\maven-repo</localRepository>
6、在IDEA中使用Maven
-
启动IDEA
-
创建一个 MavenWeb 项目
-
等待项目初始化完毕
-
观察 maven 仓库中多了什么东西?
-
IDEA中的Maven设置
注意:在IDEA项目创建成功后,看一眼配置,因为在IDEA中经常出现在项目创建成功后,它的MavenHome会使用默认的地址(C盘下),我们需要手动改为自己设置的地址
-
到这里,Maven 在 IDEA 中的配置和使用就 OK 了!
7、创建一个普通的Maven项目
创建时不勾选使用Maven的模板即可。
这个只有在web运用下才会有
8、标记文件夹功能
9、在 IDEA 中配置 Tomcat
1、
2、
3、
4
5、解决警告问题
为什么会有这个问题:我们访问一个网站,需要指定一个文件夹名字(必须);
6、虚拟路径映射
7、启动Tamcat
10、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.cheng</groupId>
<artifactId>javaweb-01-maven</artifactId>
<version>1.0-SNAPSHOT</version>
<!--
packaging:项目的打包方式
jar:Java应用
war:javaWeb 应用
-->
<packaging>war</packaging>
<!--配置-->
<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>
<!-- 具体依赖的jar包配置文件 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</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>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
11、补充
1、替换为webapp4.0版本和tomcat一致
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-app>
2、Maven仓库的使用
地址:https://mvnrepository.com/
1、搜索,使用人数最多的
2、选择版本,使用人数多的
3、jar包和依赖
代码示例:
pom.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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.cheng</groupId>
<artifactId>hello</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>hello 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>
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet.jsp/javax.servlet.jsp-api -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.3</version>
</dependency>
</dependencies>
<build>
<finalName>hello</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>
HelloServlet
package com.cheng.servlet;
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 resp) throws ServletException, IOException {
resp.setCharacterEncoding("utf-8");
// 相应的类型:html
resp.setContentType("text/html");
// 获取相应的输出流
PrintWriter out = resp.getWriter();
out.println("<html>" +
"<head>" +
"<title>Hello World!</title>" +
"</head>" +
"<body>" +
"<h1>你好!</h1>" +
"</body>" +
"</html>");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
out.println("<html>" +
"<head>" +
"<title>Hello World!</title>" +
"</head>" +
"<body>" +
"<h1>你好!</h1>" +
"</body>" +
"</html>");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}