2.再新建一个gradle项目
a.build.gradle
apply plugin: 'java' sourceCompatibility = 1.6 version = '1.0' repositories { mavenCentral() } dependencies { compile("org.apache.tomcat.embed:tomcat-embed-jasper:8.0.9") compile("org.apache.tomcat.embed:tomcat-embed-core:8.0.9") compile("org.apache.tomcat.embed:tomcat-embed-logging-juli:8.0.9") compile("org.apache.tomcat.embed:tomcat-embed-logging-log4j:8.0.9") compile("org.apache.tomcat.embed:tomcat-embed-el:8.0.9") compile("org.apache.tomcat.embed:tomcat-embed-websocket:8.0.9") compile("org.eclipse.jdt.core.compiler:ecj:4.4") }compile("org.eclipse.jdt.core.compiler:ecj:4.4")相当重要,没有此jar包,类加载器就没有加载部分类的字节码,我多次调试才找到问题.
import org.apache.catalina.LifecycleException; import org.apache.catalina.core.AprLifecycleListener; import org.apache.catalina.core.StandardServer; import org.apache.catalina.startup.ClassLoaderFactory; import org.apache.catalina.startup.Tomcat; import javax.servlet.ServletException; /** * Created by nil on 2014/8/1. */ public class EmbeddedTomcat { private Tomcat tomcat; private void startTomcat(int port,String contextPath,String baseDir) throws ServletException, LifecycleException { tomcat = new Tomcat(); tomcat.setPort(port); tomcat.setBaseDir("."); StandardServer server = (StandardServer) tomcat.getServer(); AprLifecycleListener listener = new AprLifecycleListener(); server.addLifecycleListener(listener); tomcat.addWebapp(contextPath, baseDir); tomcat.start(); } private void stopTomcat() throws LifecycleException { tomcat.stop(); } public static void main(String[] args) { try { int port=8080; String contextPath = "/test"; String baseDir = "C:/Users/nil/AppData/Local/Temp/jetty-0.0.0.0-8080-test-1.0.war-_test-any-8966644989981380511.dir/webapp"; EmbeddedTomcat tomcat = new EmbeddedTomcat(); tomcat.startTomcat(port, contextPath, baseDir); //由于Tomcat的start方法为非阻塞方法,加一个线程睡眠模拟线程阻塞. Thread.sleep(10000000); } catch (Exception e) { e.printStackTrace(); } } }其中baseDir为前面war解压的路径,另两个参数port和contextPath是容易理解的.
二.war包配置的嵌入式jetty例子.
1.运行一下war任务,对这个web应用打成war包.
2.再新建一个gradle项目.
a.build.gradle
apply plugin: 'java' sourceCompatibility = 1.6 version = '1.0' repositories { mavenCentral() } dependencies { compile("org.eclipse.jetty:example-jetty-embedded:9.2.1.v20140609") compile("org.eclipse.jetty:jetty-jsp:9.2.1.v20140609") }b.新建一个含有main方法的可执行类(此类的编写主要参考jetty官方文档:http://www.eclipse.org/jetty/documentation/current/using-annotations-embedded.html).
import org.eclipse.jetty.server.Server; import org.eclipse.jetty.webapp.*; /** * Created by nil on 2014/8/2. */ public class Main { //Create a WebApp private static WebAppContext getWebAppContext(){ WebAppContext webapp = new WebAppContext(); webapp.setContextPath("/"); webapp.setWar("E:/idea/test/build/libs/test-1.0.war"); return webapp; } public static void appStart() throws Exception { Server server = new Server(8080); //Enable parsing of jndi-related parts of web.xml and jetty-env.xml org.eclipse.jetty.webapp.Configuration.ClassList classlist = org.eclipse.jetty.webapp.Configuration.ClassList.setServerDefault(server); classlist.addAfter("org.eclipse.jetty.webapp.FragmentConfiguration", "org.eclipse.jetty.plus.webapp.EnvConfiguration", "org.eclipse.jetty.plus.webapp.PlusConfiguration"); classlist.addBefore("org.eclipse.jetty.webapp.JettyWebXmlConfiguration", "org.eclipse.jetty.annotations.AnnotationConfiguration"); //get a WebAppContext server.setHandler(getWebAppContext()); server.start(); server.join(); } public static void main(String[] args) throws Exception { appStart(); } }appStart方法内第3-5行是为了支持servlet3特性.其中上面的setWar这里就是上面打包的war包路径.