Search code examples
springspring-jdbcapplicationcontextembedded-databasefilereference

Spring Relative Path File Resource In Application Context


I have a quick question about getting spring to read a file from the application context that is external to the project. I have two projects; 1) Project A 2) Project Database. Project A has the following structure;

ProjectA
|-src
   |-main
      |-webapp
      |-WEB-INF
         |-config
         |-spring

Project Database has the following structure:

Project Database
  |-db
     |-scripts
        |-deltas

In the spring directory of Project A I have a dao-context file. In this I have configured an embedded database, however I want to initialize the database with scripts from project database. Specifically in from directory deltas.

I know the embedded database config should like the following:

<jdbc:embedded-database id="dataSource" type="H2">
    <jdbc:script location="someSchema.sql"/>
</jdbc:embedded-database>

However I am trying to determine the best way to reference the scripts in Project Database. In other words would should be the value of location if I want to use scripts in locations. A few details:

  • Both projects would be checked out together on the same hierarchy.
  • Furthermore this database would only be used to run the tests, not the actual production code. So what ever the solution it should be compatible with my build script when I run tests tasks.
  • Lastly I want to avoid an absolute file path, because this will be run on multiple machines.

Any ideas?

Cheers

EDIT: Both projects are just folders and not jars. Neither projects are in each others classpath. Other than the fact that they are in the same directory there is no relation.

EDIT 2: Another scenario (and likely to happen) is that the db scripts could be in the same project as ProjectA , but not in the classpath. For example the directory structure could look like this:

ProjectA
|-db
   |-scripts
      |-deltas
|-src
   |-main
      |-webapp
      |-WEB-INF
         |-config
         |-spring

If this was the case how would I access the scripts in the 'deltas' directory from the 'spring' directory?


Solution

  • I thing I have (more or less) understood what your asked, but there are many caveats.

    First, when developping apps, there are 3 categories of paths :

    • source paths containing java sources (will be compiled), and other configurations or data files (will be copied)
    • target paths generated from the previous ones that are assembled in jars, or wars (or ...)
    • data paths that may contain data files, or file databases outside of a jar or war.

    A web application is normally executed independantly of its source path, eventually on another machine : you build the war and deploy in on a servlet or JEE container.

    What you can do is use an environment variable, a system property or a property value to set the root of a data path in the web application. Spring is kind enough to allow environment variable to override property values when using a PropertySourcesPlaceholderConfigurer to set ${...} values in an application context. Then you use that value to get access to your datas outside of the project.

    In your example, you define such a property for the db location, and access all your database configuration relatively to that location.