views:

35

answers:

2

I have a webapp that uses JNDI to locate a datasource and a transaction manager. I see from the Jetty documentation how to do this via the jetty-env.xml file. However it mentions that this file should be put into the WEB-INF directory.

Why would they suggest that JNDI resources be configured in a configuration file that is located inside my WAR? It makes no sense...I always thought of JNDI as a way to externalize configuration. Is there another place I can put this file on a Jetty server machine?

A follow up question: How about within my Maven WAR module...how should I deal with this file so that I can use the Maven jetty plugin for development, but not have the file end up in the WAR?

+1  A: 

Is there another place I can put this file on a Jetty server machine?

The official JNDI page puts it like this:

There are 3 places in which you can define naming entries:

  1. jetty.xml
  2. WEB-INF/jetty-env.xml
  3. context xml file

Naming entries defined in a jetty.xml file will generally be scoped at either the jvm level or the Server level. Naming entries in a jetty-env.xml file will generally be scoped to the webapp in which the file resides, although you are able to enter jvm or Server scopes if you wish, that is not really recommended. In most cases you will define all naming entries that you want visible to a particular Server instance, or to the jvm as a whole in a jetty.xml file. Entries in a context xml file will generally be scoped at the level of the webapp to which it applies, although once again, you can supply a less strict scoping level of Server or jvm if you want.

Use jetty.xml to configure things "outside" the webapp.

A follow up question: How about within my Maven WAR module...how should I deal with this file so that I can use the Maven jetty plugin for development, but not have the file end up in the WAR?

Use the jettyConfig parameter of the Maven Jetty Plugin:

jettyConfig Optional. The location of a jetty.xml file that will be applied in addition to any plugin configuration parameters. You might use it if you have other webapps, handlers etc to be deployed, or you have other jetty objects that cannot be configured from the plugin.

Pascal Thivent
I accept this as the answer, but the Jetty documentation (http://docs.codehaus.org/display/JETTY/jetty.xml) seemed to indicate that jetty.xml is used to configure the server rather than specific web applications. Indeed it mandates that configurations in multiple XML files must use the same server ID (example here http://docs.codehaus.org/display/JETTY/Walkthrough+jetty.xml). But what server ID should I use so that it functions properly within the Maven context?
HDave
I think I understand that I don't need to put the new JNDI resource inside a "Configure" element for the server, but am having difficulty with it anyway. I created a new question: http://stackoverflow.com/questions/3925771/jetty-jndi-error-within-maven-jetty-plugin
HDave
@HDave `the Jetty documentation (...) seemed to indicate that jetty.xml is used to configure the server` Yes, this is correct.
Pascal Thivent
@Pascal -- so to summarize, the jetty.xml file is for configuring the server and jetty-env.xml is for configuring the web-app. The jetty-env.xml in fact can go anywhere and for the purposes of Maven just needs to be referenced via the `JettyEnvXML` configuration property.
HDave
@HDave `so to summarize, the jetty.xml file is for configuring the server and jetty-env.xml is for configuring the web-app. d ` I think it's a good summary. `The jetty-env.xml in fact can go anywhere and for the purposes of Maven just needs to be referenced via the JettyEnvXML configuration property` Can it go anywhere when not using Maven?
Pascal Thivent
@Pascal - I did some research on this in the off chance that a customer may use Jetty in production. I believe (but haven't tested it) that you can supply the jetty-env.xml file as a command line argument if you start the Jetty webapp via either `start.jar` or `jetty-runner.jar`.
HDave
@HDave Interesting. Actually, the fact that the maven jetty plugin can do it is an hint that this must be possible.
Pascal Thivent
+1  A: 

The jetty-env.xml is only an additional option to set JNDI parameters. To set JNDI parameters outside of the WAR you can place them inside jetty.xml.

See a related post here and the Jetty docs for JNDI.

vanje