maven集成jetty插件热部署

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了maven集成jetty插件热部署相关的知识,希望对你有一定的参考价值。

一、org.eclipse.jetty插件启动

1.maven依赖

<dependency>
   <groupId>org.eclipse.jetty</groupId>
   <artifactId>jetty-webapp</artifactId>
   <version>9.4.5.v20170502</version>
</dependency>

2.plugin

<plugin>
 <groupId>org.eclipse.jetty</groupId>
 <artifactId>jetty-maven-plugin</artifactId>
 <version>9.4.5.v20170502</version>
 <configuration>
 <httpConnector>
   <port>8080</port>
 </httpConnector>
 <stopKey>shutdown</stopKey>
   <stopPort>9966</stopPort>
   <scanIntervalSeconds>3</scanIntervalSeconds>
   <webApp>
    <contextPath>/ciyo</contextPath>
    <descriptor>src/main/webapp/WEB-INF/web.xml</descriptor>
    <resourceBases>
     <resourceBase>${project.basedir}/src/main/webapp</resourceBase>
    </resourceBases>
   </webApp>
   <requestLog implementation="org.eclipse.jetty.server.NCSARequestLog">
     <filename>target/access-yyyy_mm_dd.log</filename>
     <filenameDateFormat>yyyy_MM_dd</filenameDateFormat>
     <logDateFormat>yyyy-MM-dd HH:mm:ss</logDateFormat>
     <logTimeZone>GMT+8:00</logTimeZone>
     <append>true</append>
     <logServer>true</logServer>
     <retainDays>120</retainDays>
     <logCookies>true</logCookies>
     </requestLog>
  </configuration>
</plugin>

技术分享

二、Java Main 函数启动

1.maven依赖

<dependency>
   <groupId>org.eclipse.jetty</groupId>
   <artifactId>jetty-webapp</artifactId>
   <version>9.4.5.v20170502</version>
</dependency>
<dependency>
   <groupId>org.eclipse.jetty</groupId>
   <artifactId>jetty-jsp</artifactId>
   <version>9.2.22.v20170606</version>
</dependency>

2.server_config.properties

server.host=0.0.0.0
server.web.port=8080
server.web.warFile=src/main/webapp
server.web.contextPath=/ciyo

 3.JettyMainServer

/**   
 * Copyright  2017 公司名. All rights reserved.
 * 
 * @Description: TODO
 * @author: asus   
 * @date: 2017年6月13日 下午7:52:29 
 * @version: V1.0   
 */
package ciyo.peak.web.notify.receive.controller;

import java.io.File;
import java.net.URL;
import java.security.ProtectionDomain;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Validate;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.HttpConfiguration;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.util.thread.QueuedThreadPool;
import org.eclipse.jetty.webapp.WebAppContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ciyo.peak.common.utils.PropertiesUtil;

public class JettyMainServer {

   private static Logger logger = LoggerFactory.getLogger(JettyMainServer.class);

   public static void main(String[] args) {
      try {
         logger.info("start executor server");
         PropertiesUtil properties = PropertiesUtil.getInstance("server_config");
         Validate.notNull(properties, "properties not load[file=server_config.properties]");
         // 启动http服务
         String contextPath = properties.getPropertyAsString("server.web.contextPath");
         String hostName = StringUtils.defaultString(properties.getPropertyAsString("server.host"), "0.0.0.0");
         int port = properties.getPropertyAsInt("server.web.port");
         String warFile = properties.getPropertyAsString("server.web.warFile");
         Validate.isTrue(port > 0, "port < 0");
         Validate.notBlank(warFile);
         Server server = JettyMainServer.createServer(contextPath, hostName, port, warFile);
         server.start();
         logger.info("start executor server succ");
      } catch (Exception e) {
         logger.error("start executor server error", e);
         System.exit(1);
      }

   }

   /**
    * create server
    * 
    * @param contextPath
    *            context path
    * @param hostName
    *            host name
    * @param port
    *            port
    * @return
    */
   public static Server createServer(String contextPath, String hostName, int port, String warFile) {
      logger.info("createServer params[contextPath={},hostName={},port={},warFile={}]", contextPath, hostName, port,
            warFile);
      // use Eclipse JDT compiler
      System.setProperty("org.apache.jasper.compiler.disablejsr199", "true");
      // threadPool
      QueuedThreadPool threadPool = new QueuedThreadPool();
      threadPool.setMinThreads(10);// default 8
      threadPool.setMaxThreads(200);// default 200
      threadPool.setDetailedDump(false);// default false
      Server server = new Server(threadPool);
      server.setStopAtShutdown(true);
      ProtectionDomain protectionDomain = JettyMainServer.class.getProtectionDomain();
      URL location = protectionDomain.getCodeSource().getLocation();

      WebAppContext context = new WebAppContext(warFile, contextPath);
      context.setClassLoader(Thread.currentThread().getContextClassLoader());
      context.setConfigurationDiscovered(true);
      context.setParentLoaderPriority(true);
      context.setServer(server);
      context.setDescriptor(warFile + "/WEB-INF/web.xml");
      // 设置work dir,war包将解压到该目录,jsp编译后的文件也将放入其中。
      String currentDir = new File(location.getPath()).getParent();
      File workDir = new File(currentDir, "work");
      context.setTempDirectory(workDir);

      server.setHandler(context);

      // HttpConfiguration
      HttpConfiguration http_config = new HttpConfiguration();
      http_config.setSecureScheme("https");
      http_config.setSecurePort(port);
      http_config.setOutputBufferSize(32768);

      // no ssl
      ServerConnector selectChannelConnector = new ServerConnector(server);
      selectChannelConnector.setPort(port);
      if (StringUtils.isNotEmpty(hostName)) {
         selectChannelConnector.setHost(hostName);
      }
      server.setConnectors(new Connector[] { selectChannelConnector });

      return server;
   }
}

3.run启动

本文出自 “ciyo技术分享” 博客,请务必保留此出处http://ciyorecord.blog.51cto.com/6010867/1935030

以上是关于maven集成jetty插件热部署的主要内容,如果未能解决你的问题,请参考以下文章

IntelliJ + Maven + 内Jetty 实现热部署项目

jetty插件实现 热部署

jetty;tomcat;热部署

Jetty插件实现热部署(开发时修改文件自动重启Jetty)

maven tomcat7 远程热部署

Intellij-插件安装-JRebel热部署插件安装