postgresqlopenjpaopenjpa-maven-plugin

Passing the JDBC URL to openjpa-maven-plugin to refresh data in PostgreSQL


In pom.xml I have (taken from http://struberg.wordpress.com/2012/05/10/using-jpa-in-real-projects-part-1/):

<properties>
    <openjpa.sql.action>refresh</openjpa.sql.action>
    <database.driver.name>org.postgresql.Driver</database.driver.name>
    <database.connection.url>jdbc:postgresql://myURL</database.connection.url>
    <database.user>user</database.user>
    <database.password>password</database.password>
</properties>

<plugin>
    <groupId>org.apache.openjpa</groupId>
    <artifactId>openjpa-maven-plugin</artifactId>
    <version>${openjpa-version}</version>
    <configuration>
        <includes>**/entity/*.class</includes>
        <excludes>**/entity/Q*.class</excludes>
        <addDefaultConstructor>true</addDefaultConstructor>
        <enforcePropertyRestrictions>true</enforcePropertyRestrictions>
        <sqlAction>${openjpa.sql.action}</sqlAction>
        <connectionDriverName>${database.driver.name}</connectionDriverName>
            <connectionProperties>
                jdbcUrl=${database.connection.url},
                user=${database.user},
                password=${database.password},
            </connectionProperties>
        </configuration>
        <executions>
            <execution>
                <id>enhancer</id>
                <phase>process-classes</phase>
                <goals>
                    <goal>enhance</goal>
                    <goal>sql</goal>
                </goals>
            </execution>
        </executions>
        <dependencies>
            <dependency>
                <groupId>org.apache.openjpa</groupId>
                <artifactId>openjpa-all</artifactId>
                <version>${openjpa-version}</version>
            </dependency>
        </dependencies>
    </plugin>

For some reason, the URL isn't passed correctly and I get

Execution enhancer of goal org.apache.openjpa:openjpa-maven-plugin:2.2.0:sql failed: A connection could not be obtained for driver class "org.postgresql.Driver" and URL "null". You may have specified an invalid URL. (org.apache.openjpa:openjpa-maven-plugin:2.2.0:sql:enhancer:process-classes)

I've tried replacing jdbcUrl in connectionProperties with url, URL, etc. Nothing helps. However, I can put the URL in persistence.xml and this does work:

<property name="javax.persistence.jdbc.url"
    value="jdbc:postgresql://myURL" />

Driver, user, and password are obtained from Maven properties. However, since this URL is only used to generate DDL (data source is created in a different way at the runtime), I'd prefer to have it in pom.xml as well without polluting persistence.xml. Is there a way to do this?


Solution

  • I used commons-dbcp BasicDataSource for getting the connection like this:

    <connectionDriverName>org.apache.commons.dbcp.BasicDataSource</connectionDriverName>
    <connectionProperties>
        driverClassName=${database.driver.name},
        url=${database.connection.url},
        username=${database.user},
        password=${database.password},
    </connectionProperties>