【问题标题】:jetty-maven-plugin not adding JNDI datasource resource from Jetty.xmljetty-maven-plugin 未从 Jetty.xml 添加 JNDI 数据源资源
【发布时间】:2026-02-05 13:30:01
【问题描述】:

我似乎无法使用 jetty-maven 插件添加我的数据源。没有抛出任何错误,但是当我查看上下文时,尚未添加数据源。我已经验证文件的路径是正确的并且它正在解析它(输入不正确的类名会导致错误)。我曾尝试使用标签并将其配置为 WebAppContext 配置,但该文件被完全忽略。

pom.xml

<plugin>
  <groupId>org.mortbay.jetty</groupId>
  <artifactId>jetty-maven-plugin</artifactId>
  <version>8.1.3.v20120416</version>
  <configuration>
    <jettyXml>somePath/jetty.xml</jettyXml>
  </configuration>
  <dependencies>        
    <dependency>
    <groupId>postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <version>9.0-801.jdbc4</version>
  </dependency>
 </dependencies>
</plugin>

jetty.xml

<Configure class="org.eclipse.jetty.server.Server">

<New id="jdbc/postgres" class="org.eclipse.jetty.plus.jndi.Resource">
    <Arg>jdbc/postgres</Arg>
    <Arg>
        <New class="org.postgresql.ds.PGSimpleDataSource">
            <Set name="User">postgres</Set>
            <Set name="Password">postgres</Set>
            <Set name="DatabaseName">myDB</Set>
            <Set name="ServerName">localhost</Set>
            <Set name="PortNumber">5432</Set>
        </New>
    </Arg>
</New>

Java 代码

Context ctx = new InitialContext();
Context context = (Context) ctx.lookup("java:comp/env");
ds = (DataSource) context.lookup("jdbc/postgres");

任何帮助将不胜感激。

【问题讨论】:

标签: jetty jndi maven-jetty-plugin


【解决方案1】:

对于仍在寻找解决方案的人,请参阅the accepted answer in this question

这里的关键是使用jetty-env.xml 而不是jetty.xml

参考文献

【讨论】:

    【解决方案2】:

    或者简单地添加到您的 web.xml:

    <resource-ref>
        <res-ref-name>jdbc/postgres</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
    </resource-ref>
    

    【讨论】: