Run debug Maven project locally using Jetty

pom.xml

 4.0.0< /modelVersion> com.zzq.main zzq-main 0.0.1.RELEASE war zzq- main  4.4 0.0.1.RELEASE 4.1.8.RELEASE  4.3.11.Final 1.1.0.Final 1.8.2 .RELEASE 1.6.0.RELEASE 1.0.16 5.1.18 2.4.2 2.3.23 1.8.7 2.2.1 1.2.4  3.1.0 2.0 1.2 3.4 < commons-io.version>2.4 3.2.1 1.3.1 1.9 1.9.2 3.1 18.0 2.6.3 2.8.2 5.2.2.Final 3.13 0.0.9 1.7.12 1.1.3 1.2 4.12 1.7.1 1.10.19 1.6.3 2.7.3  7.6.18.v20150929< /jetty.version> 7.0.65  1.4.190 0.4.7 1.0 1.2.3 UTF-8 1.7       com.alibaba fastjson 1.2.7      commons-lang commons-lang 2.6< /version>     org.mybatis mybatis 3.3 .0   org.mybatis mybatis-spring 1.2.2    org.mybatis.generator mybatis-generator-core 1.3.2      com.alibaba druid ${druid.version}    mysql< /groupId> mysql-connector-java ${mysql.version} runtime     com.github.pagehelper pagehelper 4.0.1      org.apache.commons commons-compress 1.4.1  < dependency> org.apache.commons commons-lang3 ${commons-lang3.version}   commons -io commons-io ${commons-io.version}   commons-collections commons-collections  ${commons-io.version}   commons-beanutils commons-beanutils $ {commons-beanutils.version}      log4j log4j 1.2.16    ch.qos.logback logback-core ${logback.version}   ch.qos.logback logback-classic< /artifactId> ${logback.version}   org.slf4j slf4j-api     ch.qos.logback logback-access ${logback.version}     ch.qos.logback < artifactId>logback-core ${logback.version}   ch.qos.logback logback-classic ${logback.version}   org.slf4j slf4j-api     ch.qos.logback logback-access ${logback.version}    org.slf4j slf4j-api ${slf4j.version}    org.slf4j slf4j-simple ${slf4j.version}    org.slf4j log4j-over-slf4j ${slf4j.version}    org.slf4j jcl-over-slf4j ${ slf4j.version}    org.slf4j jul-to-slf4j< /artifactId> ${slf4j.version}    com.googlecode.log4jdbc log4jdbc ${log4jdbc.version} runtime    commons-logging commons-logging 1.2   dom4j dom4j 1.6.1< /version>   commons-collections commons-collections ${commons-collections.version}  < dependency> org.apache.commons commons-lang3 ${commons-lang3.version}   com .google.guava guava ${guava.version}   javax.servlet jstl  ${jstl.version}     < groupId>org.eclipse.jetty.aggregate jetty-webapp ${jetty.version}   org.eclipse. jetty jetty-jsp ${jetty.version}     org.springframework spring-web ${spring.version}   org.springframework spring-webmvc ${spring.version}   org.springframework spring-webmvc-portlet ${spring.version}   < dependency> org.springframework spring-beans ${spring.version}     commons-fileupload commons-fileupload ${commons-fileupload.version}     zzq-main   org.apache.maven.plugins  maven-war-plugin  false    

jetty encapsulated util

JettyBootstrap.java

package com.zzq. main.jetty;import java.io.FileInputStream;import java.io.InputStream;import java.nio.charset.Charset;import java.util.Iterator;import org.dom4j.Document;import org.dom4j.DocumentHelper;import org .dom4j.Element;import org.eclipse.jetty.server.Server;import com.zzq.main.spring.Profiles;/** * Jetty startup class. * * @author zhouzhongqing * @date May 2, 2017 14 :55:30 */public class JettyBootstrap {public static void start( int port, String profile) throws Exception {String projectPath =System.getProperty("user.dir")+"/pom.xml"; StringBuilder strBuffer = new StringBuilder (); InputStream _input = new FileInputStream(projectPath); int buffer = 1024; byte[] bys = new byte[buffer]; int byteReade = 0; while ((byteReade = _input.read(bys, 0, buffer))! = -1) {strBuffer.append(new String(bys, 0, byteReade, Charset .forName("UTF-8"))); } _input.close(); String xmlString = strBuffer.toString(); Document doc = null; doc = DocumentHelper.parseText(xmlString); Element root = doc.getRootElement(); Iterator artifactId = root.elementIterator("artifactId") ; Element entry = (Element) artifactId.next(); String projectname = entry.getText(); start(port,profile,"/"+projectname);} /** * Start. */ public static void start( int port, String profile, String contextPath) throws Exception {long beginTime = System.currentTimeMillis(); // Set Spring's profile. Profiles.setProfileAsSystemProperty( profile ); System.setProperty( "org.apache.jasper.compiler.disablejsr199" , "true" ); Server server = JettyFactory.createServerInSource( port, contextPath ); try {server.start(); long endTime = System.currentTimeMillis(); System.err.println( "[INFO] Program running service address http ://localhost:" + port + contextPath); System.err.println( "[INFO] Program running time: "+ (endTime-beginTime) + "ms" ); System.err.println( "[HINT] Press Enter to restart quickly Use the program" ); // Wait for the user to input and press Enter to reload the application. while( true) {char c = (char) System.in.read(); if( c =='
') {beginTime = System. currentTimeMillis(); // Restart. JettyFactory.reloadContext( server ); endTime = System.currentTimeMillis(); System.err.println( "[INFO] Program running service address/t http://localhost:" + port + contextPath ); System.err.println( "[INFO] Program running time: "+ (endTime-beginTime) + "ms" ); System.err.println( "[HINT] Press Enter to quickly restart the application" ); }}} catch( Exception e) {e.printStackTrace(); System.exit( -1 );} })

JettyFactory.java

package com.zzq.main.jetty;import org.apache.commons.collections.MapUtils; import org.eclipse.jetty.server.Connector;import org.eclipse.jetty.server.Server;import org.eclipse.jetty.server.nio.SelectChannelConnector;import org.eclipse.jetty.webapp.WebAppClassLoader;import org.eclipse .jetty.webapp.WebAppC ontext;import java.util.Map;/** * Create a factory class for Jetty Server. * * @author zhouzhongqing * @date May 3, 2017 11:31:49 */public class JettyFactory {private static final String DEFAULT_WEBAPP_PATH = "src/main/webapp"; private static final String WINDOWS_WEBDEFAULT_PATH = "jetty/webdefault-windows.xml"; /** * Create Jetty Server for development, operation and debugging, with src/main/webapp as the web application directory. */ public static Server createServerInSource( int port, String contextPath) {return createServerInSource( port, contextPath, null );} /** * Create a Jetty Server for development, operation and debugging, with src/main/webapp as the web application directory. */ public static Server createServerInSource( int port, String contextPath, Map initParameter) {Server server = new Server(); // Set the hook to close Jetty when the JVM exits. server.setStopAtShutdown( true ); SelectChannelConnector connector = new SelectChannelConnector(); connector.setPort( port ); // Solve the problem of repeatedly starting Jetty under Windows without reporting port conflicts. connector.setReuseAddress( false ); server.setConnectors( new Connector[]{ connector} ); WebAppContext webContext = new WebAppContext( DEFAULT_WEBAPP_PATH, contextPath ); // Modify webdefault.xml to solve the problem of Jetty Lock living static files under Windows. webContext.setDefaultsDescriptor( WINDOWS_WEBDEFAULT_PATH ); // Struts2 Annotation needs to be modified, otherwise it cannot be mapped to Action. webContext. setClassLoader( Thread.currentThread().getContextClassLoader() ); //webContext.setResourceBase("D:/dev/wxspace/tzyun/application/application-shop/src/main/webapp"); // Set startup parameters. if (MapUtils.isNotEmpty( initParameter)) {for( Map.Entry entry: initParameter.entrySet()) {webContext.setInitParameter( entry.getKey(), entry.getValue() );}} server.setHandler (webContext ); return server; }// /**// * Set the name of the jar package containing tld files except jstl-*.jar. The jar name does not require a version number, such as sitemesh, shiro-web. // */// public static void setTldJarNames( Server server, String... jarNames) {// WebAppContext context = (WebAppContext) server.getHandler();// List jarNameExprssions = Lists.newArrayList( ".*/jstl -[^/]*\.jar$", ".*/.*taglibs[^/]*\.jar$" );// for( String jar Name: jarNames) {// jarNameExprssions.add( ".*/" + jarName + "-[^/]*\.jar$" );// }//// context.setAttribute( "org.eclipse. jetty.server.webapp.ContainerIncludeJarPattern",// StringUtils.join( jarNameExprssions,'|') );////} /** * Quickly restart the application, reload target/classes and target/test-classes. * / public static void reloadContext( Server server) throws Exception {WebAppContext context = (WebAppContext) server.getHandler(); // System.out.println( "[INFO] Application reloading" ); context.stop(); WebAppClassLoader classLoader = new WebAppClassLoader( context ); classLoader.addClassPath( "target/classes" ); classLoader.addClassPath( "target/test-classes" ); context.setClassLoader( classLoader ); context.start(); // System.out.println ("[INFO] Application reloaded" ); }}

Profiles.java

package com.zzq.main.spring;/** * Common methods of Spring profile and profile name. * * @author zhouzhongqing * @date May 2, 2017 14:55:56 */public class Profiles {public static final String ACTIVE_PROFILE = "spring.profiles.active"; public static final String DEFAULT_PROFILE = "spring.profiles.default "; public static final String LOCAL = "local"; public static final String PRODUCTION = "production"; public static final String DEVELOPMENT = "development"; public static final String TEST = "test"; public static final String UNIT_TEST = "test "; public static final String FUNCTIONAL_TEST = "functional"; /** * Before Spring starts, set the profile environment variables. */ public static void setProfileAsSystemProperty( String profile) {System.setProperty( ACTIVE_PROFILE, profile ); }}< /pre> 

web.xml配置

   Archetype Created Web Application  index.html index.htm  index.jsp default.html default.htm default.jsp    log4jConfigLocation < param-value>classpath:config/log4j/log4j.properties   webAppRootKey webApp.root    log4jRefreshInterval 6000000   org.springframework.web.util.Log4jConfigListener     contextConfigLocation classpath*:config/spring/ApplicationContext.xml   org.springframework.web.context.ContextLoaderListener   springMVC org.springframework.web.servlet.DispatcherServlet  contextConfigLocation classpath*:config/spring/ApplicationContext-MVC.xml  1   springMVC *.tz    60   java.lang.Throwable /WEB-INF/views/error/500.jsp   500 /WEB -INF/views/error/500.jsp   404 /WEB-INF/views/error/404 .jsp   org.springframework.web.util.IntrospectorCleanupListener 

< br> Start the project StartServer.java

package com.zit.main ;import com.zzq.main.jetty.JettyBootstrap;import com.zzq.main.spring.Profiles;/** * @author zhouzhongqing * 2017-5-2 15:00:45 */public class StartServer {public static void main( String[] args) throws Exception {JettyBootstrap.start( 6113, Profiles.LOCAL );} }

目录结构图

Attach Source code

http://download.csdn.net/detail/baidu_19473529/9831996

Leave a Comment

Your email address will not be published.