就个人而言,我总是向 Jetty 添加一个测试依赖项并使用以下类启动它。因为代码是从 eclipse 的类路径运行的,所以无论 eclipse 看到什么,jetty web 应用程序也会看到。因此,资源目录永远不会有任何问题。
<dependency>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty</artifactId>
<version>6.1.26</version>
</dependency>
package jetty;
import org.apache.tapestry5.SymbolConstants;
import org.mortbay.jetty.Connector;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.handler.ContextHandlerCollection;
import org.mortbay.jetty.nio.SelectChannelConnector;
import org.mortbay.jetty.webapp.WebAppContext;
/**
* For jetty 6
* @see http://docs.codehaus.org/display/JETTY/Embedding+Jetty#comment-4391085
*/
public class JettyWebServer {
public static String context = "/";
public static int port = 8069;
private Server jettyServer;
public void start() throws Exception {
System.setProperty(SymbolConstants.PRODUCTION_MODE, "false");
jettyServer = new Server();
Connector connector = new SelectChannelConnector();
connector.setPort(port);
connector.setHost("127.0.0.1");
jettyServer.addConnector(connector);
WebAppContext context = new WebAppContext("src/web/", JettyWebServer.context);
context.setLogUrlOnStart(true);
context.setParentLoaderPriority(true);
jettyServer.setStopAtShutdown(true);
ContextHandlerCollection contexts = new ContextHandlerCollection();
contexts.addHandler(context);
jettyServer.setHandler(contexts);
jettyServer.start();
}
public void join() throws Exception {
jettyServer.join();
}
public void stop() throws Exception {
if (jettyServer != null)
jettyServer.stop();
}
public static void main(String[] args) throws Exception {
JettyWebServer server = new JettyWebServer();
try {
server.start();
server.join();
} finally {
server.stop();
}
}
}