在 Java/Eclipse 中构建时复制数据文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5607723/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 16:07:59  来源:igfitidea点击:

Copying data files upon build in Java/Eclipse

javaeclipse

提问by someName

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?

与我的 java 源代码一起,我有一些数据文件,我想在构建源代码时将它们复制到构建目录。目前我没有使用任何构建工具(例如 maven 或 ant),而是仅在 Eclipse 中开发和运行单元测试。我可以以某种方式要求 Eclipse 在构建我的 Java 代码时复制这些数据文件吗?

回答by Gorkem Ercan

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 Pathsection of the project properties (from the context menu of the project). Select the Sourcetab. 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.

首先创建一个新的源文件夹,将其命名为 res,您可以使用此文件夹来存储您的数据文件。接下来打开项目属性的Java Build Path部分(从项目的上下文菜单中)。选择选项卡。在此选项卡中,您可以控制每个源文件夹的输出文件夹。默认情况下,eclipse 会在构建期间将源文件夹中的所有非 java 文件复制到输出目录。

回答by Boro

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.

我不经常使用 Eclipse。但我敢打赌,必须有一个构建文件 (build.xml),您可以在其中添加类似以下代码的内容,并在构建应用程序时始终执行它。

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.

这就是我在我的一个项目的构建文件中执行此操作的方式。我在这里将两个文件夹及其所有内容复制到一个 dist(在 NetBeans 中用作创建应用程序 jar 的分发文件夹),这样我就可以轻松地在 IDE 外部运行我的应用程序。

Be aware that the target name in Eclipse might differ. But you should find a correct one described in the build file probably.

请注意,Eclipse 中的目标名称可能不同。但是您可能应该在构建文件中找到正确的描述。

 

<!-- 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.

一切顺利,博罗。

回答by Andrew White

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...

Eclipse 并不是真的要复制东西(它可能有这个功能,我不知道)。就像您提到的,这是 Maven 或 Ant 的工作。但是,对于您的 JUnit 测试,您有几个选择...