无法以嵌入式模式启动 Jetty

Not able to start Jetty in embedded mode

我正在尝试以嵌入式模式启动 Jetty 以部署 war 文件。我正在使用版本为 9.4.6

的码头库

我在 Gradle 中创建了以下任务,用于启动 Jetty 和部署 Web 应用程序。

println 'Starting Jetty............'
project.ext.server = new Server();
ServerConnector connector = new ServerConnector(project.ext.server);
connector.setPort(jettyPort);
project.ext.server.addConnector(connector);

WebAppContext webapp = new WebAppContext()
webapp.setContextPath('/')
def warPath = 'build/libs/';
warPath += 'test-' + project.version + '.war';
println("Deploying WAR File : --> ${warPath}");
webapp.setWar(warPath)

project.ext.server.setHandler(webapp);
project.ext.server.start();
println 'Server started, waiting...'
new StopMonitor(jettyStopPort, project.ext.server).start();
println 'Jetty started.'

但上面的脚本失败并出现以下错误

Caused by: java.lang.NoClassDefFoundError: Could not initialize class org.eclipse.jetty.server.session.SessionHandler

脚本中失败的确切行是

WebAppContext webapp = new WebAppContext()

即使我将此行保留为脚本中的单行并删除所有内容,我也会遇到同样的错误。

有趣的是,它所抱怨的 class 存在于 jar 文件 jetty-server.jar 中。用于 jetty libs 8.1

的相同脚本

注意:为了使脚本与 jetty 9.4 一起工作,我不得不使用 ServerConnector class 而不是 BlockingConnectot,它在 jetty 9.4 中被删除,脚本的其余部分是相同的。

我不确定为什么会失败。

您可能缺少所需的 jar 文件。

强烈建议您使用合适的构建工具,您有多种选择

这里是示例项目的 jar 依赖列表(树形式)...

https://github.com/jetty-project/embedded-servlet-3.1

[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ embedded-servlet-3.1 ---
[INFO] org.eclipse.jetty.demo:embedded-servlet-3.1:war:1-SNAPSHOT
[INFO] +- javax.servlet:javax.servlet-api:jar:3.1.0:compile
[INFO] +- org.eclipse.jetty:jetty-webapp:jar:9.4.6.v20170531:compile
[INFO] |  +- org.eclipse.jetty:jetty-xml:jar:9.4.6.v20170531:compile
[INFO] |  |  \- org.eclipse.jetty:jetty-util:jar:9.4.6.v20170531:compile
[INFO] |  \- org.eclipse.jetty:jetty-servlet:jar:9.4.6.v20170531:compile
[INFO] |     \- org.eclipse.jetty:jetty-security:jar:9.4.6.v20170531:compile
[INFO] |        \- org.eclipse.jetty:jetty-server:jar:9.4.6.v20170531:compile
[INFO] |           +- org.eclipse.jetty:jetty-http:jar:9.4.6.v20170531:compile
[INFO] |           \- org.eclipse.jetty:jetty-io:jar:9.4.6.v20170531:compile
[INFO] \- org.eclipse.jetty:jetty-annotations:jar:9.4.6.v20170531:compile
[INFO]    +- org.eclipse.jetty:jetty-plus:jar:9.4.6.v20170531:compile
[INFO]    |  \- org.eclipse.jetty:jetty-jndi:jar:9.4.6.v20170531:compile
[INFO]    +- javax.annotation:javax.annotation-api:jar:1.2:compile
[INFO]    +- org.ow2.asm:asm:jar:5.1:compile
[INFO]    \- org.ow2.asm:asm-commons:jar:5.1:compile
[INFO]       \- org.ow2.asm:asm-tree:jar:5.1:compile