使用 tomcat7-maven-plugin 插件在 IDEA 中运行 JavaWeb 项目

我们知道 IDEA Community 版本是不带 JavaEE 功能的,所以不能像 Eclipse 一样,通过内嵌的 Tomcat 直接运行 JavaWeb 项目。但并不是说 Community 版本就没办法开发 Web 项目了。因为 Tomcat 提供了一个 Maven 插件,让使用 Maven 构建的 Web 项目可以通过 mvn run 方式在 IDEA 中运行。

下面以一个最简单的项目讲述配置步骤:

创建一个 Maven 项目

项目结构如图(基于 springmvc5):

1583304276804

各文件内容

SpringMVCInitializer.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.zhangzw.springmvc5;

import javax.servlet.ServletContext;
import javax.servlet.ServletRegistration.Dynamic;

import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

import com.zhangzw.springmvc5.config.AppConfig;

public class SpringMVCInitializer implements WebApplicationInitializer {

@Override
public void onStartup(ServletContext servletContext) {
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(AppConfig.class);
ctx.setServletContext(servletContext);

Dynamic dispatcherServlet = servletContext.addServlet(DispatcherServlet.class.getSimpleName(), new DispatcherServlet(ctx));
dispatcherServlet.addMapping("/");
dispatcherServlet.setLoadOnStartup(1);
}
}

AppConfig.java

1
2
3
4
5
6
7
8
9
10
11
package com.zhangzw.springmvc5.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
@ComponentScan("com.zhangzw.springmvc5")
public class AppConfig implements WebMvcConfigurer {

}

DemoController.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.zhangzw.springmvc5.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class DemoController {

@RequestMapping("/demo")
@ResponseBody
public String demo(HttpServletRequest request, HttpServletResponse response) {
return "demo";
}

}

log4j.properties

1
2
3
4
5
6
log4j.rootLogger=info,console

log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.Target=System.out
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=[%d{yyyy-MM-dd HH:mm:ss SSS}] [%-5p] %x %c{1}.%M(%L): %m%n

下面是关键的 pom.xml 文件,里面需要添加:maven-compiler-plugin、maven-war-plugin、tomcat7-maven-plugin 三个插件:

pom.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?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.zhangzw</groupId>
<artifactId>springmvc5</artifactId>
<version>1.0.0</version>
<!-- Web 项目打成 war 包 -->
<packaging>war</packaging>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<!-- 打包不用打进去 -->
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
</dependency>
</dependencies>

<build>
<finalName>springmvc5</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<!-- 对丢失 web.xml 检测机制进行忽略, Dynamic Web Module 3.0 时代不需要 web.xml 文件注册相关内容的。 -->
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<port>9090</port><!-- 端口号 -->
<path>/springmvc5</path><!-- 应用路径 -->
<uriEncoding>UTF-8</uriEncoding>
</configuration>
</plugin>
</plugins>
</build>

</project>

运行

项目建好后,在 IDEA 的右侧 Maven 栏中的 Plugins 可以看到 tomcat 相关命令:

1583304698925

然后双击执行tomcat7:run,看到打印如下日志,就说明应用启起来了。

1583304868166