开发中使用maven插件运行Servlet容器
开发war
包形式部署的项目。一般都是下载一个Tomcat到本地,然后配置到IDE。极其麻烦,而且因为配置外部服务器,导致各种问题层出不穷。我很推荐开发过程中使用插件的形式启动服务器。不依赖外部环境,干净,快捷。
搭建一个Maven构建的Servlet工程,添加 jetty插件
jetty插件
<!-- https://mvnrepository.com/artifact/org.eclipse.jetty/jetty-maven-plugin -->
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.4.24.v20191120</version>
</dependency>
完整pom
<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>io.springboot</groupId>
<artifactId>springboot-server-plugin</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>app</finalName>
<plugins>
<!-- jetty插件 -->
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.4.24.v20191120</version>
<configuration>
<httpConnector>
<!-- 端口 -->
<port>80</port>
</httpConnector>
<webApp>
<!-- contextPath -->
<contextPath>/</contextPath>
</webApp>
</configuration>
</plugin>
</plugins>
</build>
</project>
写一个测试的Servlet
package io.springboot.pugin.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(urlPatterns = "/")
public class IndexServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = -5532805244312875694L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.getWriter().write("run in jetty plugin!!");
}
}
启动Jetty
maven 命令: jetty:run
如果不知道怎么在idea中执行maven命令,建议通过搜索引擎了解。第一次启动会比较慢,因为需要下载依赖。
看到如下日志,表示启动成功
[INFO] --- jetty-maven-plugin:9.4.24.v20191120:run (default-cli) @ springboot-server-plugin ---
[INFO] Logging initialized @3100ms to org.eclipse.jetty.util.log.Slf4jLog
[INFO] webAppSourceDirectory not set. Trying src\main\webapp
[INFO] Reload Mechanic: automatic
[INFO] nonBlocking:false
[INFO] Classes = D:\eclipse-project\springboot-server-plugin\target\classes
[INFO] Configuring Jetty for project: springboot-server-plugin
[INFO] Context path = /
[INFO] Tmp directory = D:\eclipse-project\springboot-server-plugin\target\tmp
[INFO] Web defaults = org/eclipse/jetty/webapp/webdefault.xml
[INFO] Web overrides = none
[INFO] web.xml file = null
[INFO] Webapp directory = D:\eclipse-project\springboot-server-plugin\src\main\webapp
[INFO] jetty-9.4.24.v20191120; built: 2019-11-20T21:37:49.771Z; git: 363d5f2df3a8a28de40604320230664b9c793c16; jvm 1.8.0_161-b12
[INFO] JVM Runtime does not support Modules
[INFO] Scanning elapsed time=29ms
[INFO] DefaultSessionIdManager workerName=node0
[INFO] No SessionScavenger set, using defaults
[INFO] node0 Scavenging every 600000ms
[INFO] Started o.e.j.m.p.JettyWebAppContext@2d2acd89{/,file:///D:/eclipse-project/springboot-server-plugin/src/main/webapp/,AVAILABLE}{file:///D:/eclipse-project/springboot-server-plugin/src/main/webapp/}
[INFO] Started ServerConnector@13da7ab0{HTTP/1.1,[http/1.1]}{0.0.0.0:80}
[INFO] Started @3739ms
[INFO] Started Jetty Server
浏览器访问
完美运行
Tomcat的插件
也许你已经习惯了使用Tomcat作为web服务器,本质上 Jetty/Undertow/Tomcat
都一样,都是实现了Sevlet规范的容器,对于开发者来说,不用过于关心。你如果非要用Tomcat,插件也是有一个,但是它的版本是:7,版本比较旧了,很多年不更新,不支持Servlet4。(我也不知道为啥Tomcat不在开发一个最新版本的插件出来),不建议使用了。
<!-- https://mvnrepository.com/artifact/org.apache.tomcat.maven/tomcat7-maven-plugin -->
<dependency>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
</dependency>