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

jetty, jndi, maven-jetty-plugin, mysql

Solution

Try removing the dependency on `jetty-maven-plugin`- this dependency adds the plugin to the WAR, which you don't want.

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`.

Problem

I have a really simple webapp project with maven and jetty that has been working very well until now. But now I need to setup MySQL connection pooling with JNDI as the database connections always time out. First of all here is the relevant content of my 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: ``` <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 ``` 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. 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: ``` 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. It is a zipped eclipse maven project.

Original source