尝试使用Jetty 9.1.4 + Gradle构建一个hello world应用程序。 我的项目结构
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<display-name>HelloWorldServlet</display-name>
<servlet-name>HelloWorldServlet</servlet-name>
<servlet-class>servlet.HelloWorldServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloWorldServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
我的jetty-env.xml
<New id="DSTest" class="org.eclipse.jetty.plus.jndi.Resource">
<Arg></Arg>
<Arg>jdbc/DSTest</Arg>
<Arg>
<New class="com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource">
<Set name="Url">jdbc:mysql://sql2.freemysqlhosting.net/sql238625</Set>
<Set name="User">sql238625</Set>
<Set name="Password">gD5%tN5!</Set>
</New>
</Arg>
</New>
我的build.gradle
apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'jetty'
apply plugin: 'eclipse-wtp'
repositories {
mavenCentral()
}
dependencies {
providedCompile 'javax.servlet:servlet-api:2.5'
compile 'mysql:mysql-connector-java:5.1.30'
runtime 'javax.servlet:jstl:1.1.2'
providedCompile fileTree(dir: 'jetty-9.1.4/lib', include: '*.jar')
}
httpPort = 8080
stopPort = 9451
stopKey = 'foo'
当我执行gradle jettyRunWar任务时,我正在使用stacktrace获取此异常:
Failed startup of context org.gradle.api.plugins.jetty.internal.JettyPluginWebAppContext@12cb504{/recipe-beagle,D:\Dev\projects\recipe-beagle\build\libs\recipe-beagle.war}
java.lang.IllegalArgumentException: Object is not of type class org.eclipse.jetty.plus.jndi.Resource
at org.mortbay.xml.XmlConfiguration.configure(XmlConfiguration.java:189)
at org.mortbay.jetty.plus.webapp.EnvConfiguration.configureWebApp(EnvConfiguration.java:130)
at org.mortbay.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1269)
at org.mortbay.jetty.handler.ContextHandler.doStart(ContextHandler.java:517)
at org.mortbay.jetty.webapp.WebAppContext.doStart(WebAppContext.java:489)
at org.gradle.api.plugins.jetty.internal.JettyPluginWebAppContext.doStart(JettyPluginWebAppContext.java:112)
at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:50)
at org.mortbay.jetty.handler.HandlerCollection.doStart(HandlerCollection.java:152)
at org.mortbay.jetty.handler.ContextHandlerCollection.doStart(ContextHandlerCollection.java:156)
at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:50)
at org.mortbay.jetty.handler.HandlerCollection.doStart(HandlerCollection.java:152)
at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:50)
at org.mortbay.jetty.handler.HandlerWrapper.doStart(HandlerWrapper.java:130)
at org.mortbay.jetty.Server.doStart(Server.java:224)
at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:50)
at org.gradle.api.plugins.jetty.internal.Jetty6PluginServer.start(Jetty6PluginServer.java:111)
at org.gradle.api.plugins.jetty.AbstractJettyRunTask.startJettyInternal(AbstractJettyRunTask.java:240)[...]
那么,可能是什么原因以及如何使其发挥作用?
答案 0 :(得分:1)
原因在于运行jettyRunWar gradle任务。 Gradle jetty插件使用6.xxx版本的jetty服务器,它的包装是org.mortbay。 *并且它们不适合new(jetty-9.1.4)org.eclipse.jetty。* 。 解决方案不是ise gradle插件任务,而是用gradle'war'任务进行战争,将此战争交给本地jetty_installation_directory / webapps,然后使用命令java -jar start.jar在jetty_installation_directory中运行jetty。
为了使事情正确,build.gradle必须如下:
apply plugin: 'java'
apply plugin: 'war'
repositories {
mavenCentral()
}
dependencies {
providedCompile 'javax.servlet:servlet-api:2.5'
compile 'mysql:mysql-connector-java:5.1.30'
runtime 'javax.servlet:jstl:1.1.2'
}
task copyFilesToJetty(type : Copy) {
from 'build/libs'
into 'jetty-9.1.4/webapps'
include '*.war'
}