1.再新建一个gradle项目,build.gradle如下:
apply plugin: 'java' sourceCompatibility = 1.7 version = '1.0' repositories { mavenCentral() } dependencies { compile("org.eclipse.jetty:jetty-util-ajax:9.2.1.v20140609") compile("org.eclipse.jetty:jetty-deploy:9.2.1.v20140609") compile("org.eclipse.jetty:jetty-jmx:9.2.1.v20140609") compile("org.eclipse.jetty.websocket:javax-websocket-server-impl:9.2.1.v20140609") compile("org.eclipse.jetty.spdy:spdy-http-server:9.2.1.v20140609") compile("org.eclipse.jetty:jetty-proxy:9.2.1.v20140609") compile("org.eclipse.jetty:jetty-jsp:9.2.1.v20140609") } task jarWithoutResources(type: Jar,dependsOn:compileJava) { manifest{ attributes 'Main-Class':'Start' } from("$buildDir/classes/main") archiveName("start.jar") }2.新建一个含有main方法的可执行类(此类的编写主要参考jetty官方文档:http://www.eclipse.org/jetty/documentation/current/using-annotations-embedded.html).
import org.eclipse.jetty.server.Server; import org.eclipse.jetty.webapp.WebAppContext; public class Start { public static void main(String[] args) throws Exception { //Get parameters String resourceBase=PropertyUtil.getProperty("resourceBase"); if (resourceBase==null) throw new Exception("resourceBase can not be null"); String portTmp=PropertyUtil.getProperty("port"); int port=portTmp==null?8080:Integer.valueOf(portTmp); String contextPath=PropertyUtil.getProperty("contextPath"); contextPath=contextPath==null?"/":contextPath; String extraClasspath=PropertyUtil.getProperty("extraClasspath"); extraClasspath=extraClasspath==null?"":extraClasspath; Server server = new Server(port); //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"); //Create a WebAppContext WebAppContext webapp = new WebAppContext(); webapp.setContextPath(contextPath); webapp.setResourceBase(resourceBase); webapp.setExtraClasspath(extraClasspath); server.setHandler(webapp); server.start(); server.join(); } }和上篇文章的war包配置的嵌入式jetty例子很相似,将setWar改成了setResourceBase和setExtraClasspath,另外上面利用PropertyUtil从.properties文件获取属性配置.
#默认为'8080' port=8080 #默认为'/' contextPath=/test #必须指定 resourceBase=E:/idea/test/src/main/webapp #默认为'' extraClasspath=E:/idea/test/build/classes/main/;E:/idea/test/build/resources/main/;E:/gradlerepo/caches/modules-2/files-2.1/org.springframework/spring-webmvc/3.2.8.RELEASE/b23357096216fee7fa658c3e0a28a4574d4d3a90/spring-webmvc-3.2.8.RELEASE.jar; E:/gradlerepo/caches/modules-2/files-2.1/org.springframework.data/spring-data-jpa/1.5.2.RELEASE/6e8608622fd11205238d8d1363f099c2a638a0bf/spring-data-jpa-1.5.2.RELEASE.jar; E:/gradlerepo/caches/modules-2/files-2.1/org.springframework/spring-orm/3.2.8.RELEASE/b1f3ef4edfd5344207ed03c361e17bbdf3251c49/spring-orm-3.2.8.RELEASE.jar; E:/gradlerepo/caches/modules-2/files-2.1/org.springframework/spring-tx/3.2.8.RELEASE/af44bdd6dd44557f6a03021a7d2721bbf1708ec1/spring-tx-3.2.8.RELEASE.jar; E:/gradlerepo/caches/modules-2/files-2.1/org.hibernate/hibernate-entitymanager/4.3.1.Final/c27f33216ed0d0351f1ecf6a5b6f6c490b4bcbb9/hibernate-entitymanager-4.3.1.Final.jar; E:/gradlerepo/caches/modules-2/files-2.1/c3p0/c3p0/0.9.1.2/4ea71601f844c4b90d22195bbdb58979281bb8b6/c3p0-0.9.1.2.jar; E:/gradlerepo/caches/modules-2/files-2.1/mysql/mysql-connector-java/5.1.26/f1697bef3639811e56173e70eb2471562e3ac8ef/mysql-connector-java-5.1.26.jar; E:/gradlerepo/caches/modules-2/files-2.1/com.fasterxml.jackson.core/jackson-databind/2.3.1/c4096a8323bbbcbeda072e3def123a9b66783361/jackson-databind-2.3.1.jar; E:/gradlerepo/caches/modules-2/files-2.1/commons-fileupload/commons-fileupload/1.3.1/c621b54583719ac0310404463d6d99db27e1052c/commons-fileupload-1.3.1.jar; E:/gradlerepo/caches/modules-2/files-2.1/org.slf4j/slf4j-nop/1.7.6/3d219ee4ed4965348a630ff6ef2a5418032b9466/slf4j-nop-1.7.6.jar; E:/gradlerepo/caches/modules-2/files-2.1/org.apache.taglibs/taglibs-standard-impl/1.2.1/707591cd2ac6cc1b6beff4c0439873e03fa3a6b0/taglibs-standard-impl-1.2.1.jar; E:/gradlerepo/caches/modules-2/files-2.1/javax.servlet/javax.servlet-api/3.1.0/3cd63d075497751784b2fa84be59432f4905bf7c/javax.servlet-api-3.1.0.jar; E:/gradlerepo/caches/modules-2/files-2.1/javax.servlet.jsp/jsp-api/2.2.1-b03/c7205b380e9ceb4b96745656755f31f76ae01b74/jsp-api-2.2.1-b03.jar; E:/gradlerepo/caches/modules-2/files-2.1/javax.servlet.jsp.jstl/javax.servlet.jsp.jstl-api/1.2.1/f072f63ab1689e885ac40c221df3e6bb3e64a84a/javax.servlet.jsp.jstl-api-1.2.1.jar; E:/gradlerepo/caches/modules-2/files-2.1/org.springframework/spring-beans/3.2.8.RELEASE/7aa7d1192ad7acc4b5ced07d44a1cdc4e3a377b9/spring-beans-3.2.8.RELEASE.jar; E:/gradlerepo/caches/modules-2/files-2.1/org.springframework/spring-context/3.2.8.RELEASE/7edfc6e4283b549504793682cab1f8c37d9f1890/spring-context-3.2.8.RELEASE.jar; E:/gradlerepo/caches/modules-2/files-2.1/org.springframework/spring-core/3.2.8.RELEASE/64b9b731decc74403eb85163278659d91c300584/spring-core-3.2.8.RELEASE.jar; E:/gradlerepo/caches/modules-2/files-2.1/org.springframework/spring-expression/3.2.8.RELEASE/becdd45da4986fb6d8936188f90dc869b6c237a1/spring-expression-3.2.8.RELEASE.jar; E:/gradlerepo/caches/modules-2/files-2.1/org.springframework/spring-web/3.2.8.RELEASE/799ae0503da5a9f980fecd38c9e6b23a71a406c8/spring-web-3.2.8.RELEASE.jar; E:/gradlerepo/caches/modules-2/files-2.1/org.springframework.data/spring-data-commons/1.7.2.RELEASE/ccaf8f785382bdef27a0c287d2d75af7e5c8dccf/spring-data-commons-1.7.2.RELEASE.jar; E:/gradlerepo/caches/modules-2/files-2.1/org.springframework/spring-aop/3.2.8.RELEASE/90e7924ef58621a4c07c81279e013d9a11d41789/spring-aop-3.2.8.RELEASE.jar; E:/gradlerepo/caches/modules-2/files-2.1/org.aspectj/aspectjrt/1.7.4/e49a5c0acee8fd66225dc1d031692d132323417f/aspectjrt-1.7.4.jar; E:/gradlerepo/caches/modules-2/files-2.1/org.slf4j/slf4j-api/1.7.6/562424e36df3d2327e8e9301a76027fca17d54ea/slf4j-api-1.7.6.jar; E:/gradlerepo/caches/modules-2/files-2.1/org.slf4j/jcl-over-slf4j/1.7.6/ab1648fe1dd6f1e5c2ec6d12f394672bb8c1036a/jcl-over-slf4j-1.7.6.jar; E:/gradlerepo/caches/modules-2/files-2.1/org.springframework/spring-jdbc/3.2.8.RELEASE/8fc09a51ca267726ffd284ab4de7c02f667da2ad/spring-jdbc-3.2.8.RELEASE.jar; E:/gradlerepo/caches/modules-2/files-2.1/org.jboss.logging/jboss-logging/3.1.3.GA/64499e907f19e5e1b3fdc02f81440c1832fe3545/jboss-logging-3.1.3.GA.jar; E:/gradlerepo/caches/modules-2/files-2.1/org.jboss.logging/jboss-logging-annotations/1.2.0.Beta1/2f437f37bb265d9f8f1392823dbca12d2bec06d6/jboss-logging-annotations-1.2.0.Beta1.jar; E:/gradlerepo/caches/modules-2/files-2.1/org.hibernate/hibernate-core/4.3.1.Final/73618cbf2dd1fa989a13d9444d4f025cfc51ec10/hibernate-core-4.3.1.Final.jar; E:/gradlerepo/caches/modules-2/files-2.1/dom4j/dom4j/1.6.1/5d3ccc056b6f056dbf0dddfdf43894b9065a8f94/dom4j-1.6.1.jar; E:/gradlerepo/caches/modules-2/files-2.1/org.hibernate.common/hibernate-commons-annotations/4.0.4.Final/f1af75eca4e13ac0578750a497159695feceebfc/hibernate-commons-annotations-4.0.4.Final.jar; E:/gradlerepo/caches/modules-2/files-2.1/org.hibernate.javax.persistence/hibernate-jpa-2.1-api/1.0.0.Final/5e731d961297e5a07290bfaf3db1fbc8bbbf405a/hibernate-jpa-2.1-api-1.0.0.Final.jar; E:/gradlerepo/caches/modules-2/files-2.1/org.jboss.spec.javax.transaction/jboss-transaction-api_1.2_spec/1.0.0.Final/1f9fef7a9fcbb41cc390fc370a291cf30729e094/jboss-transaction-api_1.2_spec-1.0.0.Final.jar; E:/gradlerepo/caches/modules-2/files-2.1/org.javassist/javassist/3.18.1-GA/d9a09f7732226af26bf99f19e2cffe0ae219db5b/javassist-3.18.1-GA.jar; E:/gradlerepo/caches/modules-2/files-2.1/com.fasterxml.jackson.core/jackson-annotations/2.3.0/f5e853a20b60758922453d56f9ae1e64af5cb3da/jackson-annotations-2.3.0.jar; E:/gradlerepo/caches/modules-2/files-2.1/com.fasterxml.jackson.core/jackson-core/2.3.1/f9f7185c92ca5fefe2fb3efdeb477a67c96ea2d0/jackson-core-2.3.1.jar; E:/gradlerepo/caches/modules-2/files-2.1/commons-io/commons-io/2.2/83b5b8a7ba1c08f9e8c8ff2373724e33d3c1e22a/commons-io-2.2.jar; E:/gradlerepo/caches/modules-2/files-2.1/commons-logging/commons-logging/1.1.3/f6f66e966c70a83ffbdb6f17a0919eaf7c8aca7f/commons-logging-1.1.3.jar; E:/gradlerepo/caches/modules-2/files-2.1/aopalliance/aopalliance/1.0/235ba8b489512805ac13a8f9ea77a1ca5ebe3e8/aopalliance-1.0.jar; E:/gradlerepo/caches/modules-2/files-2.1/antlr/antlr/2.7.7/83cd2cd674a217ade95a4bb83a8a14f351f48bd0/antlr-2.7.7.jar; E:/gradlerepo/caches/modules-2/files-2.1/org.jboss/jandex/1.1.0.Final/e84a2122e76f0b6503be78094ddf2108057ac15f/jandex-1.1.0.Final.jar; E:/gradlerepo/caches/modules-2/files-2.1/xml-apis/xml-apis/1.0.b2/3136ca936f64c9d68529f048c2618bd356bf85c9/xml-apis-1.0.b2.jarport,contextPath,resourceBase的设置都是容易理解的.额外的类路径extraClasspath指的意思:当启动jetty的时候,类加载器也从这些位置加载类文件.jar文件或目录要以分号或逗号分隔,并且是目录时,要以'/'结尾.上面例子的意思当启动这个嵌入式jetty,应先加载E:/idea/test/build/classes/main/,E:/idea/test/build/resources/main/两个目录下的文件和项目依赖的一些jar包.
小结:这基本可以前面所说的需求,修改类,自动reload功能没实现,感觉还是不太实用.做成gradle插件,就更好了!但不想花太多心思.