简体   繁体   English

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

[英]Change working directory in ant junit task

I have a ant file that runs JUnits tests. 我有一个运行JUnits测试的ant文件。 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. 我已经尝试为批处理测试设置工作目录,但是失败了。

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". 我用谷歌搜索和搜索,但我找到的解决方法是设置“dir”,“sysproperty”或“jvmarg”。 As you can see I've tried them all :) 如你所见,我已经尝试过所有:)

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. 这个等式中的一个通配符是,这是在Hudson中运行的,它启动了一个启动antrunner的eclipse进程。 This is so we can run both junit and eclipse plugin junit tests. 这样我们就可以运行junit和eclipse插件junit测试。 Shouldn't have anything to do with the problem I think. 我认为不应该与这个问题有任何关系。

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. 我有相同的场景,在我的情况下,我看到如果在同一个jvm中运行,则忽略dir="...."因此我只是添加了fork='true'并且它有效。

Quote from Apache's documentation site " dir - The directory in which to invoke the VM. Ignored if fork is disabled. ". 引用Apache的文档站点“ dir - 调用VM的目录。如果禁用fork,则忽略。 ”。 More here . 更多这里

I think you are right with setting the basedir property (see projects attributes ). 我认为你设置basedir属性是正确的(参见项目属性 )。 However, since it is a property of ANT (and not of the JVM) it is READ ONLY! 但是,由于它是ANT(而不是JVM)的属性,因此它只是READ!

Does it effect other target if you set the basedir when calling your ant task? 如果在调用ant任务时设置basedir,它会影响其他目标吗? See Command Line reference . 请参阅命令行参考

ant -Dbasedir=somedir

Alternatively, span a new ant process to call your junit target. 或者,跨越一个新的ant进程来调用你的junit目标。 See the AntCall task or Ant task . 请参阅AntCall任务Ant任务 Following examples assumes that the junit target contains your junit task. 以下示例假定junit目标包含您的junit任务。 I used this task for other properties (never needed the basedir property so far. 我将此任务用于其他属性(到目前为止从未需要basedir属性)。

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

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

I'm using NetBeans. 我正在使用NetBeans。 When I add to Ant properties (from Tools, Options, Java, Ant) work.dir=C:/MyWorkingDir/ it executes ant with the following command and changes the working dir to C:\\MyWorkingDir : 当我添加到Ant属性(来自Tools,Options,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

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

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