java 在 ant junit 任务中更改工作目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4366821/
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-10-30 06:03:32  来源:igfitidea点击:

Change working directory in ant junit task

javaantjunithudson

提问by Fredrik

I have a ant file that runs JUnits tests. These tests rely on a relative path to certain configuration files. I've tried to set the working directory for the batch test, but fail.

我有一个运行 JUnits 测试的 ant 文件。这些测试依赖于某些配置文件的相对路径。我试图为批处理测试设置工作目录,但失败了。

I want the working directory to be ${plugins.dir}/${name}

我希望工作目录是 ${plugins.dir}/${name}

The JUnit part of the ant script:

ant 脚本的 JUnit 部分:

<junit haltonfailure="no" printsummary="on" fork="true" showoutput="true" dir="${plugins.dir}/${name}">
    <jvmarg value="-Duser.dir=${plugins.dir}/${name}"/>
    <classpath> 
        <path refid="project.classpath"/>
        <pathelement location="${plugins.dir}/${dependency}/@dot/"/>
        <pathelement location="${plugins.dir}/${name}/" />
    </classpath>
    <formatter type="xml" />
    <sysproperty key="basedir" value="${plugins.dir}/${name}"/>
    <sysproperty key="dir" value="${plugins.dir}/${name}"/>
    <batchtest todir="${junit.output}">
        <fileset dir="${dir}"> 
            <include name="**\*AllTests.class" />
        </fileset>
    </batchtest>
</junit>

I've googled and searched but the workarounds I've found have been to set the "dir", "sysproperty" or "jvmarg". As you can see I've tried them all :)

我用谷歌搜索过,但我发现的解决方法是设置“dir”、“sysproperty”或“jvmarg”。正如你所看到的,我都试过了:)

Is there a way to print the current dir in the tag? It doesnt support . That would allow me to verify if the dir is actually changed and to what.

有没有办法在标签中打印当前目录?它不支持 . 这将允许我验证目录是否实际更改以及更改为什么。

One wildcard in this equation is that this is being run in Hudson that starts upp an eclipse process that starts antrunner. This is so we can run both junit and eclipse plugin junit tests. Shouldn't have anything to do with the problem I think.

这个等式中的一个通配符是,这是在 Hudson 中运行的,它启动了一个启动 antrunner 的 eclipse 进程。这样我们就可以同时运行 junit 和 eclipse 插件 junit 测试。应该与我认为的问题没有任何关系。

采纳答案by Peter Schuetze

I think you are right with setting the basedir property (see projects attributes). However, since it is a property of ANT (and not of the JVM) it is READ ONLY!

我认为您设置 basedir 属性是正确的(请参阅项目属性)。但是,由于它是 ANT(而不是 JVM)的属性,因此它是只读的!

Does it effect other target if you set the basedir when calling your ant task? See Command Line reference.

如果在调用 ant 任务时设置 basedir 是否会影响其他目标?请参阅命令行参考

ant -Dbasedir=somedir

Alternatively, span a new ant process to call your junit target. See the AntCall taskor Ant task. Following examples assumes that the junit target contains your junit task. I used this task for other properties (never needed the basedir property so far.

或者,跨越一个新的 ant 进程来调用您的 junit 目标。请参阅AntCall 任务Ant 任务。以下示例假定 junit 目标包含您的 junit 任务。我将此任务用于其他属性(到目前为止从未需要 basedir 属性。

<antcall target="junit">
  <param name="basedir" value="${plugins.dir}/${name}"/>
</antcall>

<ant dir="${plugins.dir}/${name}" target="junit" />

回答by Alex

I had the same scenario and in my case I saw that dir="...."is ignored if run in same jvm so I simply added fork='true'and it worked.

我有同样的场景,在我的情况下,我看到dir="...."如果在同一个 jvm 中运行,它会被忽略,所以我只是添加了fork='true'它并且它起作用了。

Quote from Apache's documentation site "dir - The directory in which to invoke the VM. Ignored if fork is disabled.". More here.

引自 Apache 的文档站点“ dir - 调用 VM 的目录。如果 fork 被禁用,则忽略。”。更多在这里

回答by Harun

I'm using NetBeans. When I add to Ant properties (from Tools, Options, Java, Ant) work.dir=C:/MyWorkingDir/it executes antwith the following command and changes the working dir to C:\MyWorkingDir:

我正在使用 NetBeans。当我添加到 Ant 属性(来自工具、选项、Java、Ant)时,work.dir=C:/MyWorkingDir/它会ant使用以下命令执行并将工作目录更改为C:\MyWorkingDir

ant -f D:\workspace\lib\project -Dfork=true -Djavac.includes=com/myapp/MyTest.java -Dtest.includes=com/myapp/MyTest.java "-Dwork.dir=C:/MyWorkingDir/" test-single