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

cku*_*ker 7 mysql jndi jetty maven-jetty-plugin

我有一个非常简单的webapp项目,maven和jetty一直运作良好,直到现在.但现在我需要使用JNDI设置MySQL连接池,因为数据库连接总是超时.

首先,这是我的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>
Run Code Online (Sandbox Code Playgroud)

现在我在/ 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>
Run Code Online (Sandbox Code Playgroud)

但问题是,我甚至无法测试此连接是否有效,因为jetty-maven-plugin无法启动目标

mvn jetty:run
Run Code Online (Sandbox Code Playgroud)

出现以下错误:

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.
Run Code Online (Sandbox Code Playgroud)

那我怎么能让它运作起来呢?我被迫使用Jetty版本8.x,因为我需要WebSocket支持,因为远程高效服务器将运行Jetty 8.

编辑 在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.
Run Code Online (Sandbox Code Playgroud)

所以似乎有多个类加载器冲突.

EDIT2 根据Pavel的要求,我使用简化的webapp重新创建了错误,您可以在Dropbox上找到它.这是一个压缩的eclipse maven项目.

Kkk*_*kev 7

尝试删除依赖项jetty-maven-plugin- 此依赖项将插件添加到WAR,您不需要.

如果您需要使用Jetty本身的任何类,请为特定版本的Jetty(而不是插件)添加一个依赖项,范围为provided.