1

My project uses the Maven build manager along with JPA and EclipseLink for interaction with a SQLite databse. The IDE of choice is NetBeans. This question however should not be anchored to the IDE but to the application that is developed in it.

I would like to make my application generate the SQLite database file that it will use in a path relative to the application's location. Is there a way to specify this inside the persistence.xml or do I have to manually code the procedure of creating the file?

Currently inside the <properties/> section of my persistence.xml I have (among other things):

<property name="javax.persistence.jdbc.url" value="jdbc:sqlite:/home/user/NetBeansProjects/Cookbook/cookbook_db.sqlite"/>

I have tried for example to replace it with

<property name="javax.persistence.jdbc.url" value="jdbc:sqlite:cookbook_db.sqlite"/>

This creates a database file inside my /home directory however I get permission error when I try to interact with it with my Java application. Entering an absolute path (as in the first example) resolves the problem. Is an absolute path really mandatory?

The behaviour I'm looking for is similar to - let's say - cmake's CMAKE_BINARY_DIR. Upon execution cmake extracts the path from this variable thus allowing the user to alter the binary directory at will. Maven itself supports relative paths in its pom.xml using the <relativePath/> tag.


Update: I've added:

<build>

  ...

  <resources>
    <resource>
      <directory>${basedir}/src/main/resources/META-INF/</directory>
      <filtering>true</filtering>
      <includes>
        <include>persistence.xml</include>
      </includes>
    </resource>
  </resources>
</build>

to my pom.xml and also changed the JDBC URL property for my SQLite databse file inside my persistence.xml as suggested by @WillShackleford to:

<property name="javax.persistence.jdbc.url" value="jdbc:sqlite:${basedir}/cookbook_db.sqlite"/>

However now I'm getting:

javax.persistence.PersistenceException: No Persistence provider for EntityManager named CookbookPU

which means that the persistence.xml is not found.

rbaleksandar
  • 8,713
  • 7
  • 76
  • 161

1 Answers1

2

Within maven ${basedir} should expand to the project directory.

<property name="javax.persistence.jdbc.url" value="jdbc:sqlite:${basedir}/cookbook_db.sqlite"/>

Set filtering on for your persistence.xml.

<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
    </resources>
</build>
WillShackleford
  • 6,918
  • 2
  • 17
  • 33
  • Hmm, after adding it to my `persistence.xml` I'm getting `java.sql.SQLException: path to '${basedir}/cookbook_db.sqlite': '/opt/apache-tomcat-8.0.15/bin/${basedir}' does not exist` when I try to access the database and also the DB file is not even created (because of the path mismatch). It seems that my `${basedir}` expands to the `bin` folder of my Tomcat installation. Any way to change that? – rbaleksandar Oct 10 '15 at 20:55
  • You may need to turn on resource filtering in maven. – WillShackleford Oct 10 '15 at 21:02
  • Please see my update. Since I'm new to Maven I would love if you can elaborate more on this. – rbaleksandar Oct 10 '15 at 21:29
  • I think your pom also moves the persistence.xml to the wrong directory. Try my newly edited one. – WillShackleford Oct 10 '15 at 22:17
  • Ah, beautiful. It's working now. Any idea why it's all so messed up? :D I'm using the Vaadin plugin for NetBeans, which uses Maven. – rbaleksandar Oct 10 '15 at 22:38