有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Maven Jetty插件中的java Jetty JNDI错误

我试图配置一个JNDI数据源,该数据源可以通过调用Maven Jetty插件来使用。我尝试在WAR文件外部执行此操作,以便以后可能使用Jetty部署我们的webapp的任何人都不必在WAR的WEB-INF目录中编辑配置文件。我建造了一个码头。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

如果我省略了顶层<Configure>元素,而只是尝试通过以下方式直接创建一个新的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

有什么好处


共 (2) 个答案

  1. # 1 楼答案

    根据documentation,在jetty.xml中声明的命名条目应该是jvm服务器作用域:

    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

    所以你的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>
    
  2. # 2 楼答案

    除了Pascal Thivent的答案之外,你的jetty.xml实际上看起来像jetty-env.xml,所以你可以配置maven jetty插件来与<jettyEnvXml>一起使用:

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