Question

I have an Eclipse project that is organized as follows:

MyProj/
    src/
        main/
            java/
                <All main Java source here>
            config/
                spring/
                    spring-config.xml
                views/
                    <All JSPs here>
                h2/
                    my-h2-db
        test/
            java/
            config/   } etc. for test sources

When I bundle it into a WAR, it is packaged like so:

MyProj.war
    META-INF/
    WEB-INF/
        web.xml
        lib/
        classes/
            spring-config.xml
            my-h2-db
            views/
                Home.jsp
                About.jsp
                etc.
            com/ --> root dir for all compiled Java binaries

I need to write code that is capable of CRUDing an H2 database (and all the tables, users, etc. that go with it) that for this example I have called my-h2-db. This way the app can work with the same DB file no matter if I'm testing from inside Eclipse, or if I'm actually using the deployed WAR.

So I ask:

  • How do I access the my-h2-db file both from the Eclipse and runtime (WAR) classpath, since it is packaged in a different location than where it lives in the Eclipse project?; and
Was it helpful?

Solution

I don't think you'll want to package the H2 files within your WAR application. It's not going to be able to use them in the WAR.

What you want in your version control is the DDL used to build the database entities (and pre populate if necessary). Then, either in your development or production environments, you use these scripts to create/update the database.

What you have in your web application is simply the JDBC url (specifying the file location of the database) used to locate the embedded database. As you suggested, this will be different based on development or production environments.

I've been using LiquiBase as a way of managing database creation/upgrades, and it's pretty easy to use.

However, the idea of creating the database as part of development and then embedding those files within the WAR in order to use them directly won't work. There's no concept of saving those files, so none of the data would persist.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top