如何解决Maven-jetty插件每分钟重新启动Servlet的问题?

时间:2019-04-03 08:59:05

标签: java maven intellij-idea maven-jetty-plugin

我正在学习Java-Android课程,并且在基础级别的最后一章中,他们正在讨论使用maven-jetty插件设置servlet。 我确实按照他们说的做,但是我无法在localhost:8080上运行我的servlet。 该项目只是一个简单的HelloWorld风格,但是问题是我无法运行服务器。 我正在使用Intellij IDE

我在pom.xml上使用码头插件

<plugin>
                <groupId>org.eclipse.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>9.4.15.v20190215</version>
                <configuration>
                    <scanIntervalSeconds>1</scanIntervalSeconds>
                </configuration>
            </plugin>

那是我的web.xml文件

<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_3_1.xsd"
         version="3.1">

    <sevlet>
        <servlet-name>HelloWorld</servlet-name>
        <servlet-class>com.elie.webapp.project.web.HelloWorldServlet</servlet-class>
    </sevlet>

    <servlet-mapping>
        <servlet-name>HelloWorld</servlet-name>
        <url-pattern>/test</url-pattern>
    </servlet-mapping>
</web-app>

和我的java类显示字符串“ Hello World”

package com.elie.webapp.project.web;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class HelloWorldServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.getWriter().println("Hello World");
    }
}

当我执行jetty:run插件时,似乎一切顺利,并且在日志中收到了消息:

[INFO] Web overrides =  none
[INFO] web.xml file = null
[INFO] Webapp directory = C:\Users\Elie\IdeaProjects\webapp\target\webapp-tmp
[INFO] jetty-9.4.15.v20190215; built: 2019-02-15T16:53:49.381Z; git: eb70b240169fcf1abbd86af36482d1c49826fa0b; jvm 11.0.2+9-LTS
[INFO] Scanning elapsed time=65ms
[INFO] DefaultSessionIdManager workerName=node0
[INFO] No SessionScavenger set, using defaults
[INFO] node0 Scavenging every 660000ms
[INFO] Started o.e.j.m.p.JettyWebAppContext@52ae997b{/,file:///C:/Users/Elie/IdeaProjects/webapp/target/webapp-tmp/,AVAILABLE}{file:///C:/Users/Elie/IdeaProjects/webapp/target/webapp-tmp/}
[INFO] Started ServerConnector@65002c76{HTTP/1.1,[http/1.1]}{0.0.0.0:8080}
[INFO] Started @4744ms
[INFO] Started Jetty Server

该消息发出10秒钟后,服务器会反复显示以下消息

[INFO] restarting o.e.j.m.p.JettyWebAppContext@52ae997b{/,file:///C:/Users/Elie/IdeaProjects/webapp/target/webapp-tmp/,AVAILABLE}{file:///C:/Users/Elie/IdeaProjects/webapp/target/webapp-tmp/}
[INFO] Stopped o.e.j.m.p.JettyWebAppContext@52ae997b{/,file:///C:/Users/Elie/IdeaProjects/webapp/target/webapp-tmp/,UNAVAILABLE}{file:///C:/Users/Elie/IdeaProjects/webapp/target/webapp-tmp/}
[INFO] Webapp source directory = C:\Users\Elie\IdeaProjects\webapp\target\webapp-tmp
[INFO] Reload Mechanic: automatic
[INFO] nonBlocking:false
[INFO] Classes = C:\Users\Elie\IdeaProjects\webapp\target\classes
[INFO] Context path = /
[INFO] Tmp directory = C:\Users\Elie\IdeaProjects\webapp\target\tmp
[INFO] Web defaults = org/eclipse/jetty/webapp/webdefault.xml
[INFO] Web overrides =  none
[INFO] web.xml file = null
[INFO] Webapp directory = C:\Users\Elie\IdeaProjects\webapp\target\webapp-tmp
[INFO] Scanning elapsed time=42ms
[INFO] Started o.e.j.m.p.JettyWebAppContext@52ae997b{/,file:///C:/Users/Elie/IdeaProjects/webapp/target/webapp-tmp/,AVAILABLE}{file:///C:/Users/Elie/IdeaProjects/webapp/target/webapp-tmp/}
[INFO] Restart completed at Wed Apr 03 10:52:48 CEST 2019

1 个答案:

答案 0 :(得分:0)

您可以这样配置它:

<configuration>
   <reload>manual</reload>
</configuration>
  

重新加载   默认值为“自动”,与非零scanIntervalSeconds结合使用时,在检测到更改时会自动进行热重新部署。改为设置为“ manual”,以通过在运行插件的控制台中键入换行来触发扫描。如果您要进行一系列更改,直到完成,这些更改可能会很有用。在这种情况下,请使用reload参数。