【问题标题】:Jetty JNDI error within Maven Jetty PluginMaven Jetty 插件中的 Jetty JNDI 错误
【发布时间】:2011-04-24 22:57:20
【问题描述】:

我正在尝试配置可通过调用 Maven Jetty 插件使用的 JNDI 数据源。我正在尝试在 WAR 文件外部执行此操作,以便以后可能使用 Jetty 部署我们的 webapp 的任何人都不必编辑 WAR 的 WEB-INF 目录中的配置文件。我创建了一个jetty.xml文件如下:

<?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>

然后我从 Maven 插件中引用了这个文件,如下所示:

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

但是,当我尝试通过 mvn jetty:run-war 运行 webapp 时,出现以下错误:

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

如果我遗漏了顶级 &lt;Configure&gt; 元素并尝试直接通过以下方式创建新的 JNDI 资源:

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

然后我得到一个类似的错误:

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

什么给了?

【问题讨论】:

    标签: java maven-2 jetty jndi maven-jetty-plugin


    【解决方案1】:

    根据documentationjetty.xml 中声明的命名条目应该是 jvmServer 作用域的:

    如您所见,最自然的 在其中声明的配置文件 每个范围的命名条目是:

    • jetty.xml - jvm 或服务器范围
    • WEB-INF/jetty-env.xml 或上下文 xml 文件 - webapp 范围

    所以你的jetty.xml 应该包含这样的内容:

    <?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>
    

    【讨论】:

    • 嗨 Pascal,id="Server" 的用途是什么?它只是一个 IoC bean 对象 ID(用于 org.mortbay.jetty.Server)还是应该在任何其他特定的 Jetty 配置中链接(类似于部署描述符资源引用中的 id="sbeDataSource")?
    【解决方案2】:

    除了 Pascal Thivent 的回答之外,您的 jetty.xml 实际上看起来像 jetty-env.xml,因此您可以配置 maven-jetty-plugin 以将其与 &lt;jettyEnvXml&gt; 一起使用:

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

    【讨论】:

    • 我发誓我已阅读 maven-jetty-plugin 文档 50 次,但从未见过该配置选项。我想部分原因是因为该选项的文档仅在“运行”目标下。但在黑白中它清楚地表明:jettyEnvXml Optional. it is the location of a jetty-env.xml file, which allows you to make JNDI bindings that will satisfy &lt;env-entry&gt;, &lt;resource-env-ref&gt; and &lt;resource-ref&gt; 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).
    猜你喜欢
    • 2014-03-28
    • 1970-01-01
    • 1970-01-01
    • 2010-12-21
    • 2011-05-01
    • 1970-01-01
    • 2016-08-20
    • 2012-01-05
    • 2019-04-26
    相关资源
    最近更新 更多