Search code examples
javaeclipsefilemavennio

Managing different resource directories


I'm using Eclipse with Maven m2e plugin. My project uses configuration xml files located under config. Maven project layout says you should put stuff like that in srv/main/resources and here the trouble starts...

In the appication the config directory is hard wired relative to the project root. Can I somehow, maybe even trough the POM, trick Eclipse and Maven to make the software runnable even if the the files are in src/main/resources?

EDIT: Maven layout says only src, target, pom.xml and some txt files are in the root. But my deployed software requires config in the root. This doesn't quite fit together if my appilcation calls something like new File("config/someXml.xml"). My software would have to look in config/someXml.xml and src/main/resoruces/config/someXml.xml depending on if the software is deployed or debugging.

How do other projects do this? Do I somehow have to change my code for supplying the directory path?


Solution

  • From my own experience, the most common and efficient way is making you application resolve the required config file from multiple place in a pre-defined order, pseudo code:

    Properties config = null;
    // try load config file from pre-defined location, first attempt
    config = read("config/config.xml");
    // if nothing is here, load config file from classpath, default.
    if (config == null)
      config = read("src/main/resources/config.xml");
    

    The config.xml under src/main/resources always come with the final executable jar (inside the jar), if nothing is presented in config folder, always use this as default. So in development, app can use src/main/resources/config.xml without concerning config/config.xml existence, when go to deployment, it will start using config/config.xml if presented.