简体   繁体   English

如何让Ant识别项目文件夹中的文件?

[英]How do I get Ant to recognize a file is in the project folder?

I am passing the file "testFile.txt" to my Java program as an argument using Eclipse and it works fine when I choose to run it normally. 我使用Eclipse将文件“ testFile.txt”作为参数传递给我的Java程序,当我选择正常运行它时,它运行良好。 If I run the Ant build.xml in Eclipse with "testFile.txt" set as an argument, I get the error: 如果在Eclipse中以“ testFile.txt”为参数运行Ant build.xml,则会收到错误消息:

BUILD FAILED Target "testFile.txt" does not exist in the project "MyProj1". 失败:目标“ testFile.txt”在项目“ MyProj1”中不存在。

If I use Ant and pass the file in a linux shell using the line: 如果我使用Ant并使用以下行在Linux shell中传递文件:

ant -Dargs="testFile.txt" run

it passes the String value "testFile.txt" but can not find the the corresponding file. 它传递字符串值“ testFile.txt”,但找不到相应的文件。 The file is in the MyProj1 folder which is where the src and build.xml are located. 该文件位于src和build.xml所在的MyProj1文件夹中。 Do I need to move the .txt file or modify the build file? 我是否需要移动.txt文件或修改构建文件? If I have to modify the build file, what do I need? 如果必须修改构建文件,我需要什么?

<target name="run" depends="compile" description="run the project">
    <java dir="${build.dir}" classname="${main.class}" fork="yes">
        <arg line="${args}"/>
    </java>
</target>

Add

<arg file="testFile.txt"/>

To the java task, ie: 要执行Java任务,即:

<target name="run" depends="compile" description="run the project">
    <java dir="${build.dir}" classname="${main.class}" fork="yes">
        <arg line="${args}"/>
        <arg file="testFile.txt"/>
    </java>
</target>

This will create an absolute path to the /path/to/build.xml/directory, which is where your file is. 这将创建文件/path/to/build.xml/目录的绝对路径。

If your file is in a subdirectory, prepend that to the arg file value, ../ also works the way you'd expect. 如果您的文件位于子目录中,请将该文件放在arg文件值的前面,.. /也可以按您期望的方式工作。

edit: 编辑:

Directory structure 目录结构

c:\project
|
+---PassFileViaAnt.class
+---build.xml
+---foo.txt

Class: 类:

public class PassFileViaAnt {
    public static void main( String[] args ) {
        for( String arg : args ) {
            System.out.println( arg );
        }
    }
}

task: 任务:

<target name="run" description="run the project">
    <java dir="${build.dir}" classname="PassFileViaAnt" fork="yes">
        <arg file="${args}" />
    </java>
</target>

Command: 命令:

c:\project> ant run -Dargs="foo.txt"

Output: 输出:

[java] C:\project\foo.txt

Command: 命令:

c:> ant -f c:\project\build.xml run -Dargs="foo.txt"

output 输出

[java] C:\project\foo.txt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM