Thursday, May 5, 2011

Java export .properties file to build folder?

Hello. I have just created a .properties file in Java and got it to work. The problem is where/how to store it. I'm currently developing a "Dynamic Web project" in Eclipse, and have stored the properties file under build/classes/myfile.properties, and I'm using this code to load it:

properties.load(this.getClass().getResourceAsStream("/myfile.properties"));

But won't this folder get truncated when building the project, or not included when exporting as a WAR file? How can I add this file to the build path, so it will be added to /build/classes on every export (in eclipse)?

From stackoverflow
  • If you add your .properties file into the source folder, it will get copied into the classes folder at build time. I usually create a separate "Source Folder" in my projects to hold .properties files and other non-Java source files.

  • Putting it anywhere in classpath will do. Just make sure it is included in WEB-INF/classes after deployment/build.

    In maven projects the standard place for property files is /src/main/resources. You can put it there ant add this folder as eclipse "source folder".

  • You can put the properties into the "Web App Libraries/classes" folder, which corresponds to "src/main/webapp/WEB-INF/class" folder on the disk (eclipse automatically creates the shorthand version above):

  • See my question on copying property files using an ant task. Eclipse will do this automatically, as @highlycaffeinated has suggested, but you have to make sure your list-of-files is up-to-date (refresh files on your project before you debug/run/deploy).

    I use ant for more formal control over this.

0 comments:

Post a Comment