1. 下载jetty
注意: Jetty 9.x 需要 JDK 1.7 支持。下表是Jetty各版本状态一览。
版本 | JDK版本 | Servlet | JSP | 状态 |
9.0 | 1.7 | 3.0 (3.1 on branch) | 2.2 | 稳定 |
8.x | 1.6 | 3.0 | 2.2 | 成熟 |
7.x | 1.5, J2ME | 2.5 | 2.1 | 成熟 |
6.x | 1.4-1.5 | 2.5 | 2.0 | 过时 |
下载地址: http://download.eclipse.org/jetty/
2. 启动类
package net.codaview;
import java.io.File;
import java.net.URL;
import java.security.ProtectionDomain;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.WebAppContext;
public class EmbeddedServer {
public static void main(String[] args) throws Exception {
int port = Integer.parseInt(System.getProperty("port", "8080"));
Server server = new Server(port);
ProtectionDomain domain = EmbeddedServer.class.getProtectionDomain();
URL warLocation = domain.getCodeSource().getLocation();
WebAppContext webapp = new WebAppContext();
webapp.setContextPath("/");
webapp.setDescriptor(warLocation.toExternalForm() + "/WEB-INF/web.xml");
webapp.setServer(server);
webapp.setWar(warLocation.toExternalForm());
String tempDir = new File(warLocation.getPath()).getParent() + "/tmp";
webapp.setTempDirectory(new File(tempDir));
server.setHandler(webapp);
server.start();
server.join();
}
}
3. ant脚本
<project>
<target name="war">
<!-- web根路径 -->
<property name="webapp.dir" location="${basedir}/webapp" />
<!-- jetty -->
<property name="jetty.dir" location="${basedir}/webapp/WEB-INF/jetty" />
<!-- 临时目录 ,放置打包过程中产生的文件 -->
<property name="temp.dir" location="${basedir}/temp" />
<!-- 创建临时目录 -->
<mkdir dir="${temp.dir}" />
<!-- 拷贝web下所有文件到临时目录 -->
<copy todir="${temp.dir}">
<fileset dir="${webapp.dir}">
<exclude name="WEB-INF/jetty/*" />
<exclude name="WEB-INF/tmp/*" />
</fileset>
</copy>
<!-- 解压jetty -->
<unjar dest="${temp.dir}">
<fileset dir="${jetty.dir}">
<include name="jetty-continuation-7.6.9.v20130131.jar" />
<include name="jetty-http-7.6.9.v20130131.jar" />
<include name="jetty-io-7.6.9.v20130131.jar" />
<include name="jetty-jmx-7.6.9.v20130131.jar" />
<include name="jetty-security-7.6.9.v20130131.jar" />
<include name="jetty-server-7.6.9.v20130131.jar" />
<include name="jetty-servlet-7.6.9.v20130131.jar" />
<include name="jetty-util-7.6.9.v20130131.jar" />
<include name="jetty-webapp-7.6.9.v20130131.jar" />
<include name="jetty-xml-7.6.9.v20130131.jar" />
<include name="annotations-api-6.0.37.jar" />
<include name="catalina-6.0.37.jar" />
<include name="ecj-4.2.2.jar" />
<include name="el-api-6.0.37.jar" />
<include name="jasper-6.0.37.jar" />
<include name="jasper-el-6.0.37.jar" />
<include name="javax.servlet.jsp-api-2.2.1.jar" />
<include name="jsp-api-6.0.37.jar" />
<include name="juli-6.0.37.jar" />
<include name="servlet-api-6.0.37.jar" />
</fileset>
<patternset>
<exclude name="META-INF/**/*" />
<exclude name="images/**/*" />
<exclude name=".options" />
<exclude name="about.html" />
<exclude name="jetty-dir.css" />
<exclude name="jdtCompilerAdapter.jar" />
<exclude name="plugin*" />
</patternset>
</unjar>
<!-- 复制启动类 -->
<copy todir="${temp.dir}">
<fileset dir="${webapp.dir}/WEB-INF/classes" includes="net/codaview/EmbeddedServer.class" />
</copy>
<!-- 创建war包 -->
<war destfile="runnable.war" webxml="${webapp.dir}/web.xml">
<fileset dir="${temp.dir}" includes="**/*" />
<classes dir="${webapp.dir}/WEB-INF/classes" />
<lib dir="${webapp.dir}/WEB-INF/lib" />
<manifest>
<attribute name="Main-Class" value="net.codaview.EmbeddedServer" />
</manifest>
</war>
<!-- 删除临时目录 -->
<delete dir="${temp.dir}" />
</target>
</project>
4. 运行
java -jar runnable.war
5. 来源 stackoverflow