3
votes

I can't seem to get my datasource added using jetty-maven plugin. There are no errors thrown but when i look in the context the datasource has not been added. I have verified that the path to the file is correct and that its is parsing it(putting in incorrect class names leads to error's). I have tried using the tag and configuring as a WebAppContext configuration but this file is being completely ignored.

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 Code

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

Any help would be appreciated.

2
I have similar problem and it seems that jetty is not configured by defaukt with jndi. I m looking for the configuration but it is tricky. docs.codehaus.org/display/JETTY/JNDI - Jonathan Lebrun

2 Answers

1
votes

For people still looking for a solution, see the accepted answer in this question.

The key here is to use jetty-env.xml and not jetty.xml.

References

0
votes

Or simply add to your 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>