一、Maven介绍
Jar就是一个java的项目。web的项目一般怎么部署在tomcat之上呢,打成war包。还会把这种后台的java项目打成jar包来运行。jar包里面资源会更多,jar包里面可能涵盖web服务器,比如说有tomcat。
pom是项目对象模型,pom.xml 会放置一些项目的描述信息。
Maven相关概念
-
Maven构建
定义:项目编译或运行需要依赖的各种Maven组件
类型:Jar包、描述文件、插件等
-
Maven仓库
保存Maven构建的本地或者远程存储区域
分类:本地仓库、远程仓库
-
Maven坐标
Maven构建的定位标识(GroupID、ArtifactID、Version)
作用:用来定义和寻找要使用的Maven构建
二、使用maven的步骤
1.
下载maven,https://maven.apache.org/download.cgi
2、配置
解压文件,高级系统设置-环境变量-新建变量 MAVEN_HOME 或M2_HOME,指向maven的解压路径
3、在path中配置
在path中配置 %MAVEN_HOME%\bin或%M2_HOME%\bin
4、查看版本
按win+r快捷键, 输入cmd 打开dos界面,输入mvn -version命令,就可以查看版本号
5、配置本地仓库
<localRepository>E:\repository</localRepository>
安装完idea以后,有个默认的maven仓库及配置C:\Users\Administrator \ .m2
6、镜像加速配置
<mirror>
<id>nexus-aliyun</id>
<mirrorOf>*</mirrorOf>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>
三、maven仓库分类
项目管理工具种的pom.xml文件配置maven坐标信息后,会从本地仓库去寻找maven构建,本地仓库如果没有maven构建,则会从远程仓库进行下载,然后给项
目提供maven构建。
四、maven使用模板构建web项目
4.1 创建项目
打开idea,创建项目
4.2 选择模板
4.3 填写GAV坐标信息
4.4 配置maven版本及配置文件
4.5 填写项目信息
点击Finish完成即可
五、Maven命令
clean:清空上一次项目运行生成的文件
compile:下载构建并编译项目
install:下载构建并打包项目,并安装项目到本地仓库
package:下载构建并打包项目
六、创建web项目打war包并部署
创建maven项目,添加web模块
编写pom.xml文件:
1、需要添加配置war节点:
<packaging>war</packaging>
2、添加servlet,jsp的依赖
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.1</version>
<scope>provided</scope>
</dependency>
3、maven打jsp项目的插件配置
<!--maven 打包jsp项目插件配置-->
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.3.1</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
<!--指定web.xml文件的位置-->
<webXml>web\WEB-INF\web.xml</webXml>
<!--指定jsp等文件所在位置-->
<warSourceDirectory>web</warSourceDirectory>
</configuration>
</plugin>
</plugins>
</build>
完整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.mvndemo</groupId>
<artifactId>mvn-01</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
<!--maven 打包jsp项目插件配置-->
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.3.1</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
<!--指定web.xml文件的位置-->
<webXml>web\WEB-INF\web.xml</webXml>
<!--指定jsp等文件所在位置-->
<warSourceDirectory>web</warSourceDirectory>
</configuration>
</plugin>
</plugins>
</build>
</project>
七、fastjson第三方的依赖文件使用
引入fastjson依赖:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.54</version>
</dependency>
编写servlet:
package servlet;
import com.alibaba.fastjson.JSON;
import entity.Person;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
/**
* @author:mengshujun
* @createTime: 2024-03-22 16:03:51 星期五
*/
@WebServlet("/hello")
public class HelloServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=utf-8");
Person p1 = new Person(1001,"张三",22);
String str = JSON.toJSONString(p1);
PrintWriter out = resp.getWriter();
out.print(str);
out.close();
out.flush();
}
}
maven依赖查找的官网地址:
https://mvnrepository.com