Along with my java source, I have some data files that I would like to copy to the build dir when the source is build. Currently I am not using any build tools (e.g. maven or ant), but develop and run unit tests solely from within Eclipse. Can I somehow ask Eclipse to copy these data files when it builds my java code?
3 Answers
First Create a new source folder call it something like res, you can use this folder to store your data files. Next open the Java Build Path section of the project properties (from the context menu of the project). Select the Source tab. In this tab you can control the output folders of each of the source folders. By default eclipse copies all the non-java files int the source folder to output directory during the build.

- 3,220
- 1
- 18
- 26
-
by any chance, do you know how the same can be done in a C++ project? I created a file in a source folder, and it seems like it isnt copied by default. Also I cannot find the "Java Build Path" equivalent in the properties of the C++ project – 463035818_is_not_an_ai May 05 '17 at 13:28
Go with Ant. To be honest this is the best solution for automation of app building process.
I am not using Eclipse often. But I bet there must be a build file (build.xml) where you can add something like the code below to it and have it always executed when building your app.
This is how I am doing it in my build file for one of my projects. I am copying here two folders with all their content to a dist (which in NetBeans serves as a distribution folder where application jar is being created), so then I can easily run my application externally outside the IDE.
Be aware that the target name in Eclipse might differ. But you should find a correct one described in the build file probably.
<!-- to copy data & images folders' contents to dist folder on build. -->
<target name="-post-jar" description="Copying Images">
<property name="images.dir" value="${dist.dir}/images" />
<property name="data.dir" value="${dist.dir}/data" />
<copy todir="${images.dir}">
<fileset dir="./images" />
</copy>
<echo level="info" message="Images folder content was copied."/>
<copy todir="${data.dir}">
<fileset dir="./data" />
</copy>
<echo level="info" message="Data folder content was copied."/>
</target>
All the best, Boro.

- 7,913
- 4
- 43
- 85
-
Thanks. I think I will start using Ant in the near feature. Seems far more flexible than plain-Eclipse. Nice and informative answer! – someName Apr 10 '11 at 16:24
Eclipse isn't really meant to copy things around (it may have that feature and I don't know about it). Like you alluded to, that's a job for Maven or Ant. However, for your JUnit tests, you have a couple of options...
- Open the data files based on a project relative path.
- Use getResourceAsStream(filename) and add the data files to the build-path

- 52,720
- 19
- 113
- 137