简体   繁体   English

带有DataSource的jetty-env.xml导致mvn jetty:run上的WebAppContext失败

[英]jetty-env.xml with DataSource leads to failing WebAppContext on mvn jetty:run

I have a really simple webapp project with maven and jetty that has been working very well until now. 我有一个非常简单的webapp项目,maven和jetty一直运作良好,直到现在。 But now I need to setup MySQL connection pooling with JNDI as the database connections always time out. 但现在我需要使用JNDI设置MySQL连接池,因为数据库连接总是超时。

First of all here is the relevant content of my pom.xml: 首先,这是我的pom.xml的相关内容:

<modelVersion>4.0.0</modelVersion>
...
<packaging>war</packaging>
...
<properties> 
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <jetty-version>8.1.0.v20120127</jetty-version>
</properties> 
<dependencies>
    ...
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.20</version>
    </dependency>
    <dependency>
        <groupId>org.mortbay.jetty</groupId>
        <artifactId>jetty-maven-plugin</artifactId>
        <version>${jetty-version}</version>
        <type>maven-plugin</type>
    </dependency>
</dependencies>
...
<build>
    <plugins>
    <plugin>
        <groupId>org.mortbay.jetty</groupId>
        <artifactId>jetty-maven-plugin</artifactId>
        <version>${jetty-version}</version>
        <configuration>
                <scanIntervalSeconds>10</scanIntervalSeconds>
            </configuration>
        </plugin>
    </plugins>
    ...
</build>

Now I created a jetty-env.xml in the folder /src/main/webapp/WEB-INF with the following content: 现在我在/ src / main / webapp / WEB-INF文件夹中创建了一个jetty-env.xml,其中包含以下内容:

<Configure class="org.eclipse.jetty.webapp.WebAppContext">  
    <New id="project-db" class="org.eclipse.jetty.plus.jndi.Resource">
        <Arg>jdbc/db</Arg>
        <Arg>
            <New class="com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource">
                <Set name="url">jdbc:mysql://www.example.com:3306/mydb</Set>
                <Set name="username">dbuser</Set>
                <Set name="password">dbpass</Set>
            </New>
        </Arg>
    </New>
</Configure>

But the problem is that I can't even test if this connection works as the jetty-maven-plugin fails to start on the goal 但问题是,我甚至无法测试此连接是否有效,因为jetty-maven-plugin无法启动目标

mvn jetty:run

with the following error: 出现以下错误:

WARN:oejw.WebAppContext:Failed startup of context o.m.j.p.JettyWebAppContext
{/,file:/D:/documents/programmierung/workspace/battleships-trunk/src/main/webapp/}
,file:/D:/documents/programmierung/workspace/battleships-trunk/src/main/webapp/

java.lang.IllegalArgumentException: Object of class 
'org.mortbay.jetty.plugin.JettyWebAppContext' is not of type 
'org.eclipse.jetty.webapp.WebAppContext'. 
Object Class and type Class are from different loaders.

So how can I get this to work? 那我怎么能让它运作起来呢? I'm forced to use Jetty version 8.x as I need WebSocket support and as the remote productive server will be running Jetty 8. 我被迫使用Jetty版本8.x,因为我需要WebSocket支持,因为远程高效服务器将运行Jetty 8。

EDIT Before Pavel Veller's answer I tried the following: Deployed the assembled war to the remote jetty8 server and got the same error only that the previous error now reads as follows: 编辑在Pavel Veller的回答之前,我尝试了以下方法:将组装好的战争部署到远程jetty8服务器并获得相同的错误,只是前面的错误现在如下所示:

java.lang.IllegalArgumentException: Object of class 
'org.eclipse.jetty.webapp.WebAppContext' is not of type 
'org.eclipse.jetty.webapp.WebAppContext'. 
Object Class and type Class are from different loaders.

So it seems as if there are multiple class loaders conflicting. 所以似乎有多个类加载器冲突。

EDIT2 As requested by Pavel I recreated the error with a simplified webapp which you can find here on Dropbox . EDIT2根据Pavel的要求,我使用简化的webapp重新创建了错误,您可以在Dropbox上找到它。 It is a zipped eclipse maven project. 这是一个压缩的eclipse maven项目。

Try removing the dependency on jetty-maven-plugin - this dependency adds the plugin to the WAR, which you don't want. 尝试删除对jetty-maven-plugin依赖 - 这个依赖项将插件添加到WAR,这是你不想要的。

If you need to use any classes from Jetty itself, add a dependency for the specific version of Jetty (rather than the plugin) with a scope of provided . 如果您需要使用从码头本身的任何课程,增加对码头的使用的范围的特定版本(而不是插件)的依赖provided

It looks like it's pulling jetty 6 from somewhere. 看起来它正在从某个地方拉出码头6。 The exception you're seeing seems to be coming from the code that parses jetty-env.xml ( org.mortbay.jetty.plus.webapp.EnvConfiguration ). 您看到的异常似乎来自解析jetty-env.xmlorg.mortbay.jetty.plus.webapp.EnvConfiguration )的代码。 The XMLConfiguration class compares the class you declare on the Configure element with the actual class of what it gets from getWebAappContext() . XMLConfiguration类将您在Configure元素上声明的类与从getWebAappContext()获取的类进行比较。 The latter is instance of org.mortbay.jetty.plugin.JettyWebAppContext in your case and you expect it to be org.eclipse.jetty.webapp.WebAppContext (which would be the parent class for JettyWebAppContext had they both come from the same "namespace"). 后者是你的情况instance of org.mortbay.jetty.plugin.JettyWebAppContext的实例,你希望它是org.eclipse.jetty.webapp.WebAppContext (如果它们都来自相同的“命名空间”,它将是JettyWebAppContext的父类“)。

It's hard to tell where that would be happening from but maybe inspect your .m2 and confirm you have the proper binaries for your jetty dependencies? 很难说出会发生什么,但是可能会检查你的.m2并确认你有适合你的码头依赖的二进制文件? It has got to be running not the version you expect it to run. 它必须运行不是您期望它运行的版本。

UPDATE . 更新 Jetty does the following when it loads the classes defined in the configuration: Jetty在加载配置中定义的类时执行以下操作:

  1. first load with Thread.currentThread().getContextClassLoader() and loop through all getParent() until all options are exhausted. 首先使用Thread.currentThread().getContextClassLoader()加载并遍历所有getParent()直到所有选项都用完为止。
  2. if not successful, attempt to load with the class loader that loaded jetty core classes ( XmlConfiguration.class.getClassLoader() ) looping through all the parents as well. 如果不成功,尝试使用加载了jetty核心类的类加载器( XmlConfiguration.class.getClassLoader() )来加载循环遍历所有父项。
  3. If still not successful, do a Class.forName() 如果仍然不成功,请执行Class.forName()
  4. Report ClassNotFoundException if not successful. 如果不成功,则报告ClassNotFoundException

you can see it in the code of org.mortbay.util.Loader ( http://grepcode.com is a great resource for a quick look under the hood) 你可以在org.mortbay.util.Loader的代码中看到它( http://grepcode.com是一个很好的资源,可以快速浏览一下)

It does load the class in your case, but apparently not with the right class loader. 它确实在你的情况下加载了类,但显然没有使用正确的类加载器。

I would now assume you have an extra jetty JAR somewhere on your classpath that interferes with the order of things. 我现在假设你在类路径的某处有一个额外的码头JAR会干扰事物的顺序。

Had a same issue caused by : 有同样的问题引起的:

<useTestClasspath>true</useTestClasspath> (true instead of false)

That put a extra jetty jar in the classpath... 这在类路径中放了一个额外的码头......

Including the dependency scope solved the error for me. 包括依赖范围为我解决了错误。

<scope>provided</scope>

In the pom.xml it looks like this, pom.xml它看起来像这样,

<!-- JETTY DEPENDENCIES -->
<dependency>
  <groupId>org.eclipse.jetty</groupId>
  <artifactId>jetty-server</artifactId>
  <version>${jetty.version}</version>
  <scope>provided</scope>
</dependency>
<dependency>
  <groupId>org.eclipse.jetty</groupId>
  <artifactId>jetty-servlet</artifactId>
  <version>${jetty.version}</version>
  <scope>provided</scope>
</dependency>
<dependency>
  <groupId>org.eclipse.jetty</groupId>
  <artifactId>jetty-webapp</artifactId>
  <version>${jetty.version}</version>
  <scope>provided</scope>
</dependency>
<dependency>
    <groupId>org.eclipse.jetty</groupId>
    <artifactId>jetty-servlets</artifactId>
    <version>${jetty.version}</version>
    <scope>provided</scope>
</dependency>

in the jetty dependencies and the errors went off. 在码头依赖中,错误消失了。 And btw, the jetty version I'm using is 9.3.7.v20160115 . 顺便说一句,我使用的码头版本是9.3.7.v20160115

I had the same issue and fixed it but can't figure out why. 我有同样的问题并修复它但无法弄清楚原因。

By changing 通过改变

org.eclipse.jetty.webapp.WebAppContext

to

org.eclipse.jetty.maven.plugin.JettyWebAppContext 

it started to work for some reason, can't figure out exactly why. 它由于某种原因开始起作用,无法弄清楚究竟是为什么。 Clearly maven plugin has something to do with it? 显然,maven插件与它有关?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Jetty 7 + MySQL Config [java.lang.ClassNotFoundException:org.mortbay.jetty.webapp.WebAppContext] - Jetty 7 + MySQL Config [java.lang.ClassNotFoundException: org.mortbay.jetty.webapp.WebAppContext] Jetty 7 + MySQL Config [java.lang.IllegalArgumentException: Object is not of type class org.eclipse.jett y.webapp.WebAppContext] - Jetty 7 + MySQL Config [java.lang.IllegalArgumentException: Object is not of type class org.eclipse.jett y.webapp.WebAppContext] Jetty mysql连接池配置错误:javax.naming.NameNotFoundException; 剩余名称&#39;env / jdbc / ---(mysql 5.0 + jetty 7.0.1) - Jetty mysql connection-pool configuration error: javax.naming.NameNotFoundException; remaining name 'env/jdbc/---(mysql 5.0+jetty 7.0.1) 码头会话集群ClassNotFoundException - Jetty session clustering ClassNotFoundException 将JDBC引用保留在web.xml之外以在Jetty中部署,例如在tomcat中 - Keep JDBC references outside web.xml to deploy in Jetty, like in tomcat 码头MySQL数据库连接池 - jetty mysql database connection pooling 码头配置:MySQL JNDI问题 - Jetty configuration: problems with MySQL JNDI Solr Jetty Bitnami MySQL驱动程序配置 - Solr Jetty Bitnami MySQL Driver Configuration Jetty JDBCLoginService在mysql请求中使用null - Jetty JDBCLoginService using null in mysql request 码头无法加载mysql jdbc驱动程序 - jetty can't load mysql jdbc driver
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM