Copying data files upon build in Java/Eclipse
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?
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.
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.
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
精彩评论