引入jetty依赖
<dependency> <groupId>org.eclipse.jetty.aggregate</groupId> <artifactId>jetty-webapp</artifactId> <version>${jetty.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-jsp</artifactId> <version>${jetty.version}</version> <scope>test</scope> </dependency>
配置maven jetty插件
<plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>${jetty.version}</version> <configuration> <scanIntervalSeconds>10</scanIntervalSeconds> <stopPort>9966</stopPort> <stopKey>foo</stopKey> <useTestClasspath>true</useTestClasspath> <webAppConfig> <contextPath>/${project.artifactId}</contextPath> </webAppConfig> <connectors> <connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector"> <port>8088</port> <maxIdleTime>60000</maxIdleTime> </connector> </connectors> <systemProperties> <systemProperty> <name>spring.profiles.active</name> <value>development</value> </systemProperty> </systemProperties> </configuration> </plugin>
( 1 )配置 Jetty 容器(支持所有 goals )
<connectors> :可选参数,配置org.eclipse.jetty.server.Connector( Jetty 端口监听器)列表,如果不指定该参数,将会连接默认的 8080 端口。
<jettyXml> :可选参数,指定 Jetty 配置文件的路径
<scanIntervalSeconds>:可选参数,配置 Web 应用扫描的暂停时间, Web 应用扫描如果发现修改了程序,会自动重新部署。该参数默认值为 0 ,即不启动热部署
<systemProperties>:可选参数,设置插件执行时的系统参数,比如上面的配置示例中指定了 spring 的 profile 为development,如果不设置该参数,就需要配置 maven 与 spring 的 profile 一致,同时在 mvn 命令中增加 -P development选项,或者直接在 spring 配置文件中设置spring.profiles.active为development
<systemPropertiesFile>:可选参数,设置系统参数配置文件的位置,将会批量执行其中的系统参数配置
<loginServices>:可选参数,配置org.eclipse.jetty.security.LoginService实现类的列表。
<requestLog>:可选参数,配置请求日志接口:org.eclipse.jetty.server.RequestLog的实现类,配置请求日志的处理格式
( 2 )配置 Web 应用程序(不支持run-forked、stop两个 goals )
<webApp>:从jetty6.1.6rc0起使用webAppConfig, web 应用程序配置根节点
<contextPath>:设置 web 应用程序的 context 路径,默认情况下,它被设置成该项目的pom.xml的<artifactId>
<descriptor>:设置 web 应用程序主配置文件 web.xml 的路径,默认该配置文件位于 WEB-INF 目录下
<defaultsDescriptor>:设置先于 web.xml 执行的webdefault.xml配置文件的路径
<overrideDescriptor>:设置在 web.xml 读取之后执行的配置文件,使用该参数可以覆盖或增加 web.xml 中的配置
<tempDirectory>: Web 应用的临时目录, Jetty 可以在此目录编译 jsp 文件或者复制 jar 包,默认路径为${project.build.outputDirectory}/tmp
<baseResource>:指定 Web 应用静态资源所在的路径,默认路径为src/main/webapp
<resourceBases>:指定多个 Web 应用静态资源所在的路径,使用逗号分隔
<baseAppFirst>:可选参数,默认值为 true ,控制是否可以在 Web 应用的原始资源之前或之后叠加多个 war 包
<jettyEnvXml>:可选参数,指定jetty-env.xml配置文件的路径
<containerIncludeJarPattern>:jetty-8.1.x之后的版本可以使用,可选参数,配置加载到 Jetty 容器 Classloader 中的 Jar 包的路径或匹配模式,符合条件的 jar 包将会被检测META-INF、资源、 tld 和类的继承关系
<webInfIncludeJarPattern>:jetty-8.1.x之后的版本可以使用,可选参数,配置加载到 Web 应用程序的 Classloader (WEB-INF classpath )中的 Jar 包的路径或匹配模式,符合条件的 jar 包将会被检测META-INF、资源、 tld 和类的继承关系
<contextXml>:可选参数,指定context xml配置文件的路径
嵌入式jetty
package org.springside.examples.quickstart;
import java.io.File;
import java.net.URL;
import java.security.ProtectionDomain;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.WebAppContext;
/**
* Main Class for standalone running.
*
* @author calvin
*/
public class Main {
public static void main(String[] args) throws Exception {
String contextPath = "/";
int port = Integer.getInteger("port", 8080);
Server server = createServer(contextPath, port);
try {
server.start();
server.join();
} catch (Exception e) {
e.printStackTrace();
System.exit(100);
}
}
private static Server createServer(String contextPath, int port) {
// use Eclipse JDT compiler
System.setProperty("org.apache.jasper.compiler.disablejsr199", "true");
Server server = new Server(port);
server.setStopAtShutdown(true);
ProtectionDomain protectionDomain = Main.class.getProtectionDomain();
URL location = protectionDomain.getCodeSource().getLocation();
String warFile = location.toExternalForm();
WebAppContext context = new WebAppContext(warFile, contextPath);
context.setServer(server);
// 设置work dir,war包将解压到该目录,jsp编译后的文件也将放入其中。
String currentDir = new File(location.getPath()).getParent();
File workDir = new File(currentDir, "work");
context.setTempDirectory(workDir);
server.setHandler(context);
return server;
}
}
this.class.getProtectionDomain().getCodeSource().getLocation()
如果直接执行.class文件那么会得到当前class的绝对路径。
如果封装在jar包里面执行jar包那么会得到当前jar包的绝对路径。
通过以上配置,已经可以在 Web 应用程序内嵌入 Jetty 容器了,但还需要注意以下几点
Maven Pom 中的 Jetty 依赖注意 scope 修改为provided,防止 Jetty 的 Jar 包被打到WEB-INF/lib中。
如果需要解析 jsp 页面,需要在依赖中加入jsp-2.1-glassfish包的引用,注意其 scope 不能设置为provided
<dependency> <groupId>org.mortbay.jetty</groupId> <artifactId>jsp-2.1-glassfish</artifactId> <version>2.1.v20100127</version> </dependency>
简单:
运行war:
package com.google.code.garbagecan.jettystudy.sample6;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.WebAppContext;
public class WebAppContextWithWarServer {
public static void main(String[] args) throws Exception {
Server server = new Server(8080);
WebAppContext context = new WebAppContext();
context.setContextPath("/myapp");
context.setWar("E:/share/test/struts2-blank.war");
server.setHandler(context);
server.start();
server.join();
}
}
运行webApp
package com.google.code.garbagecan.jettystudy.sample6;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.WebAppContext;
public class WebAppContextWithFolderServer {
public static void main(String[] args) throws Exception {
Server server = new Server(8080);
WebAppContext context = new WebAppContext();
context.setContextPath("/myapp");
context.setDescriptor("E:/share/test/struts2-blank/WEB-INF/web.xml");
context.setResourceBase("E:/share/test/struts2-blank");
context.setParentLoaderPriority(true);
server.setHandler(context);
server.start();
server.join();
}
}