Jetty JNDI error within Maven Jetty Plugin
Asked Answered
C

2

3

I am trying to configure a JNDI data source that can be used from an invocation of the Maven Jetty Plugin. I am trying to do this external to the WAR file, so that anyone who might later deploy our webapp with Jetty will not have to edit a configuration file inside the WAR's WEB-INF directory. I created a jetty.xml file as follows:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://jetty.mortbay.org/configure.dtd">
<Configure class="org.mortbay.jetty.webapp.WebAppContext">
 <!-- Atomikos XA aware (but not XA capable) JDBC data source -->
 <New id="sbeDataSource" class="org.mortbay.jetty.plus.naming.Resource">
  <Arg>jdbc/myDataSource</Arg>
  <Arg>
   <New class="com.atomikos.jdbc.nonxa.AtomikosNonXADataSourceBean">
    .......
   </New>
  </Arg>
 </New> 
</Configure>

I then referenced this file from within the Maven plugin as follows:

 <plugin>
  <groupId>org.mortbay.jetty</groupId>
  <artifactId>maven-jetty-plugin</artifactId>
  <configuration>
   <jettyConfig>config/jetty.xml</jettyConfig>
  </configuration>
 </plugin>

However when I attempt to run the webapp via mvn jetty:run-war I get the following error:

Embedded error: 
Object is not of type class org.mortbay.jetty.webapp.WebAppContext

If I leave out the top level <Configure> element and just try to create a new JNDI resource directly via:

<New id="sbeDataSource" class="org.mortbay.jetty.plus.naming.Resource">

Then I get a similar error:

Embedded error:
Object is not of type class org.mortbay.jetty.plus.naming.Resource

What gives?

Cresida answered 13/10, 2010 at 16:17 Comment(0)
T
2

In addition to Pascal Thivent's answer, your jetty.xml actually looks like jetty-env.xml, so you can configure maven-jetty-plugin to use it with <jettyEnvXml>:

<plugin> 
  <groupId>org.mortbay.jetty</groupId> 
  <artifactId>maven-jetty-plugin</artifactId> 
  <configuration> 
   <jettyEnvXml>config/jetty.xml</jettyEnvXml> 
  </configuration> 
</plugin>
Timmi answered 13/10, 2010 at 18:0 Comment(1)
I swear I read the maven-jetty-plugin documentation 50 times and never saw that configuration option. I guess part of the reason was because the documentation for the option is under the "run" goal only. But in black and white it clearly stated: jettyEnvXml Optional. it is the location of a jetty-env.xml file, which allows you to make JNDI bindings that will satisfy <env-entry>, <resource-env-ref> and <resource-ref> linkages in the web.xml that are scoped only to the webapp and not shared with other webapps that you may be deploying at the same time (eg by using a jettyConfig file).Cresida
I
3

According to the documentation, naming entries declared in the jetty.xml are supposed to be jvm or Server scoped:

As you can see, the most natural config files in which to declare naming entries of each scope are:

  • jetty.xml - jvm or Server scope
  • WEB-INF/jetty-env.xml or a context xml file - webapp scope

So your jetty.xml should contain something like this:

<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://jetty.mortbay.org/configure.dtd">
<Configure id="Server" class="org.mortbay.jetty.Server">
 <!-- Atomikos XA aware (but not XA capable) JDBC data source -->
 <New id="sbeDataSource" class="org.mortbay.jetty.plus.naming.Resource">
  <Arg>jdbc/myDataSource</Arg>
  <Arg>
   <New class="com.atomikos.jdbc.nonxa.AtomikosNonXADataSourceBean">
    .......
   </New>
  </Arg>
 </New> 
</Configure>
Icelandic answered 13/10, 2010 at 17:55 Comment(1)
hi Pascal, what is the purpose of id="Server"? Is it only an IoC bean object ID (for org.mortbay.jetty.Server) or should it be linked in any other specific Jetty configuration (similar like e.g. id="sbeDataSource" in deployment descriptor resource-ref)?Feldt
T
2

In addition to Pascal Thivent's answer, your jetty.xml actually looks like jetty-env.xml, so you can configure maven-jetty-plugin to use it with <jettyEnvXml>:

<plugin> 
  <groupId>org.mortbay.jetty</groupId> 
  <artifactId>maven-jetty-plugin</artifactId> 
  <configuration> 
   <jettyEnvXml>config/jetty.xml</jettyEnvXml> 
  </configuration> 
</plugin>
Timmi answered 13/10, 2010 at 18:0 Comment(1)
I swear I read the maven-jetty-plugin documentation 50 times and never saw that configuration option. I guess part of the reason was because the documentation for the option is under the "run" goal only. But in black and white it clearly stated: jettyEnvXml Optional. it is the location of a jetty-env.xml file, which allows you to make JNDI bindings that will satisfy <env-entry>, <resource-env-ref> and <resource-ref> linkages in the web.xml that are scoped only to the webapp and not shared with other webapps that you may be deploying at the same time (eg by using a jettyConfig file).Cresida

© 2022 - 2024 — McMap. All rights reserved.