I have my resources directory and properties file at the same level as src. To run the program from outside of the IDE, I end up having to copy the resources and properties file to the bin/ directory. For deploying, is it best to keep resources and properties at the same level as bin, and to create a batch file that goes into bin to run the program?
3 Answers
As Dave Newton stated, it is generally a good ideia to put editable configurations in a separate location from your source, for example in a "config" folder.
If you have a small project and you are looking for a simple solution that would allow you to automatically do this via a script in eclipse or to do it by manually invoking the same script from outside eclipse, you could add an additional builder to your project.
Here's an example of a basic "Ant Builder":
First, create an ant file (ex: build.xml) in your project, like this:
<project name="ResourceBuilder" basedir="." default="build">
<target name="build">
<copy file="config/test.properties" tofile="bin/test.properties" overwrite="true"/>
</target>
</project>
For simplicity sake I'm assuming that the ant file will be in your project's root dir. This ant file will take a properties file named "test.properties" from the folder "config" and copy it to the "bin" folder.
After that, go to your project's properties and select "Builders" > "New..." > "Ant Builder".
In the "Main" tab, insert the script location in the Buildfile input (you can use the "Browse Workspace" button to locate it).
In the "Targets" tab, select the default target, via the "Set Targets..." button, for "Auto Build" and "During a Clean".
Save the project properties and rebuild you project.
If you have a "test.properties" file in a "config" folder it should now be copied automatically to your "bin" folder whenever you build your project.
From outside eclipse, you can invoke the script manually and it should also work.
Hope it helps!
Comments
For simple projects, keeping resources in the src directory is fine.
If the files are not intended to be edited by users, keep the same structure and load them as resources (rather than files).
If they must be external and/or editable, it's best to allow a means of configuring their location, along with sensible defaults. A launcher script could set a default as well, if one hasn't been set by the user.
Comments
Have a look at Maven to manage your project's lifecycle.
You might want also look at the related eclipse plugin, m2eclipse.