您所遇到的问题中的链接提供了您所需的大多数内容。但是,除此之外,还需要做一些事情。
打包后,Jetty需要启动的所有类文件都必须位于war文件的根目录下。
<war>在归档之前,我们可以利用Ant为我们做到这一点。战争的清单文件还将需要一个
Main-Class属性来执行服务器。
这是一个循序渐进的步骤:
创建您的Jetty服务器类:
这是根据您提供的链接改编而成的。
package com.mycompany.myapp;import java.io.File;import java.net.URL;import java.security.ProtectionDomain;import org.mortbay.jetty.Server;import org.mortbay.jetty.webapp.WebAppContext;public final class EmbeddedJettyServer{ public static void main(String[] args) throws Exception { int port = Integer.parseInt(System.getProperty("port", "8080")); Server server = new Server(port); ProtectionDomain domain = EmbeddedJettyServer.class.getProtectionDomain(); URL location = domain.getCodeSource().getLocation(); WebAppContext webapp = new WebAppContext(); webapp.setContextPath("/"); webapp.setDescriptor(location.toExternalForm() + "/WEB-INF/web.xml"); webapp.setServer(server); webapp.setWar(location.toExternalForm()); // (Optional) Set the directory the war will extract to. // If not set, java.io.tmpdir will be used, which can cause problems // if the temp directory gets cleaned periodically. // Your build scripts should remove this directory between deployments webapp.setTempDirectory(new File("/path/to/webapp-directory")); server.setHandler(webapp); server.start(); server.join(); }}要查看您可以在此处配置的所有内容,请查看Jetty
API文档。
与Ant建立战争:
它使用暂存目录将必要的类文件解压缩到战争的根目录中,以便在执行战争时可以访问它们。
<target name="war" description="--> Creates self-executing war"> <property name="staging.dir" location="${basedir}/staging"/> <property name="webapp.dir" location="${basedir}/src/webapp"/> <mkdir dir="${staging.dir}"/> <!-- assumes you have all of your war content (excluding classes and libraries) already structured in a directory called src/webapp --> <!-- e.g. --> <!-- src/webapp/index.html --> <!-- src/webapp/WEB-INF/web.xml --> <!-- src/webapp/WEB-INF/classes/my.properties --> <!-- etc ... --> <copy todir="${staging.dir}"> <fileset dir="${webapp.dir}" includes="***"/> <exclude name="images*"/> <exclude name=".options"/> <exclude name="about.html"/> <exclude name="jdtCompilerAdapter.jar"/> <exclude name="plugin*"/> </patternset> </unjar> <!-- copy in the class file built from the above EmbeddedJettyServer.java --> <copy todir="${staging.dir}"> <fileset dir="path/to/classes/dir" includes="com/mycompany/myapp/EmbeddedJettyServer.class"/> </copy> <war destfile="myapp.war" webxml="${webapp.dir}/WEB-INF/web.xml"> <fileset dir="${staging.dir}" includes="**/*"/> <classes dir="path/to/classes/dir"/><!-- your application classes --> <lib dir="path/to/lib/dir"/><!-- application dependency jars --> <manifest> <!-- add the Main-Class attribute that will execute our server class --> <attribute name="Main-Class" value="com.mycompany.myapp.EmbeddedJettyServer"/> </manifest> </war> <delete dir="${staging.dir}"/></target>执行战争:
如果以上各项均已正确设置,则您应该能够:
java -jar myapp.war// or if you want to configure the port (since we are using the System property in the pre)java -Dport=8443 -jar myapp.war



