尝试使用Jetty 9.1.4 + Gradle构建hello world应用程序。我的项目结构
- [project-root-dir]
- [它]
- [jetty-9.1.4]
- (自由)
- jetty-jndi-9.1.4.v(…). jar
- jetty-plus-9.1.4.v(…)/jar
- (src)
- [主要]
- (java)
- (servlet)
- HelloWorldServlet.java
- (应用)
- [- inf]
- jetty-env.xml
- web . xml
- build.gralde
<?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:Devprojectsrecipe-beaglebuildlibsrecipe-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)[...]
那么,可能的原因是什么,以及如何使它起作用?
原因是在运行jettyRunWar gradle任务。Gradle jetty插件使用6。jetty服务器的XXX版本,它的包是org.按揭。*和它们不适合new (jetty-9.1.4) org.eclipse.jetty.*。解决方案不是使用gradle插件任务,而是使用gradle 'war'任务,将这个war手工放到本地的jetty_installation_directory/webapps中,然后在jetty_installation_directory中使用命令java -jar start.jar运行jetty。
要使事情顺利进行,就要构建。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'
}