新建maven项目

image

不要勾选这个选项

image

选择web版本

image

填写项目信息

image

配置pom.xml

配置maven插件,指定java的版本

<build>
	<finalName>java-web</finalName>
	<plugins>
		<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-compiler-plugin</artifactId>
			<configuration>
				<source>1.8</source>
				<target>1.8</target>
			</configuration>
		</plugin>
	</plugins>
</build>

配置maven servlet

<!-- https://mvnrepository.com/artifact/javax.servlet/servlet-api -->
<dependency>
	<groupId>javax.servlet</groupId>
	<artifactId>servlet-api</artifactId>
	<version>2.5</version>
	<scope>provided</scope>
</dependency>

这是我配置好的pom.xml

<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>priv.sen</groupId>
<artifactId>test_maven_web</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>test_maven_web Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
	<dependency>
		<groupId>junit</groupId>
		<artifactId>junit</artifactId>
		<version>4.12</version>
		<scope>test</scope>
	</dependency>
	<!-- https://mvnrepository.com/artifact/javax.servlet/servlet-api -->
	<dependency>
		<groupId>javax.servlet</groupId>
		<artifactId>servlet-api</artifactId>
		<version>2.5</version>
		<scope>provided</scope>
	</dependency>
</dependencies>
<build>
	<finalName>java-web</finalName>
	<plugins>
		<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-compiler-plugin</artifactId>
			<configuration>
				<source>1.8</source>
				<target>1.8</target>
			</configuration>
		</plugin>
	</plugins>
</build>
</project>

Tomcat部署mavenWeb项目

右键项目-Run As-Run On Servlet-add 项目

设置本地保存路径

image

设置项目的访问路径

image

访问

image

原文链接:https://miansen.wang/2018/09/06/2/