ANT junit任务中的相对路径问题
我已经设置了一个 ant 脚本作为 eclipse 构建器来自动运行我的所有测试,如下所示:
<project name="auto-test" default="test">
<property name="tst-dir" location="C:\STAF\services\custom\TopCoder\bin" />
<path id="classpath.base" />
<path id="classpath.test">
<pathelement location="D:\eclipse\eclipse\plugins\org.junit4_4.3.1\junit.jar" />
<pathelement location="${tst-dir}" />
<path refid="classpath.base" />
</path>
<target name="test" description="Run the tests">
<junit>
<classpath refid="classpath.test" />
<formatter type="brief" usefile="false" />
<test name="testDataGenerator.test.AllTests" />
</junit>
</target>
</project>
在我将测试夹具文件从绝对路径更改为相对路径之前一切都很好:
SAXReader reader = new SAXReader();
Document document = reader.read(new File(".").getCanonicalPath()+"\\conf\\TestData.xml");
ant 任务现在尝试打开 D:\ eclipse\eclipse\conf\TestData.xml,而不是 C:\STAF\services\custom\TopCoder\conf\TestData.xml,我还尝试从Eclipse 一切都很好。
以前有人遇到过类似的问题吗?
提前致谢。
附言。 ANT_HOME=D:\eclipse\eclipse\plugins\org.apache.ant_1.7.0.v200706080842
跟进: 我尝试从命令行运行 ant 脚本,并找到以下内容:
C:\STAF\services\custom\TopCoder>ant -fc:\STAF\services\custom\TopCoder\task\build.xml ,ant 脚本工作正常。
C:>ant -fc:\STAF\services\custom\TopCoder\task\build.xml,脚本将声明:[junit] C:\conf\TestData.xml (系统找不到指定的路径)
我还检查了 eclipse builder 设置,似乎没有什么可以更改 D:\eclipse\eclipse 的路径。
I have setup an ant script as eclipse builder to automatically run all my tests, like below:
<project name="auto-test" default="test">
<property name="tst-dir" location="C:\STAF\services\custom\TopCoder\bin" />
<path id="classpath.base" />
<path id="classpath.test">
<pathelement location="D:\eclipse\eclipse\plugins\org.junit4_4.3.1\junit.jar" />
<pathelement location="${tst-dir}" />
<path refid="classpath.base" />
</path>
<target name="test" description="Run the tests">
<junit>
<classpath refid="classpath.test" />
<formatter type="brief" usefile="false" />
<test name="testDataGenerator.test.AllTests" />
</junit>
</target>
</project>
It was all good before I changed a test fixture file from absolute path to relative path:
SAXReader reader = new SAXReader();
Document document = reader.read(new File(".").getCanonicalPath()+"\\conf\\TestData.xml");
The ant task now try to open D:\eclipse\eclipse\conf\TestData.xml, instead of C:\STAF\services\custom\TopCoder\conf\TestData.xml, I've also try to run AllTests manually from Eclipse and it's all good.
Has anyone met similar problem before?
Thanks in advance.
PS. ANT_HOME=D:\eclipse\eclipse\plugins\org.apache.ant_1.7.0.v200706080842
Follow up:
I tried to run the ant script from command line, and find below:
C:\STAF\services\custom\TopCoder>ant -f c:\STAF\services\custom\TopCoder\task\build.xml, the ant script works correctly.
C:>ant -f c:\STAF\services\custom\TopCoder\task\build.xml, the script will claim: [junit] C:\conf\TestData.xml (The system cannot find the path specified)
I've also checked eclipse builder setting, there seems nothing to change the path to D:\eclipse\eclipse.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Java 根据当前用户目录解析相对路径,该目录通常是调用 java 程序的目录。
解决此问题的一种方法是为基本路径定义环境变量。然后,您可以轻松使用“相对路径”(意思是,通过连接基本路径和相对路径来创建绝对路径)。
Java resolves relative paths against the current user directory, which is typically the directory from where the java program was invoked.
One way to overcome this issue is to define an environmental variable for your base path. Then, you could easily use "relative paths" (meaning, create absolute paths by concatenating the base path and the relative path).
这是我找到的解决方案:
正如 kgiannakakis 提到的,Ant 也从调用它的位置开始执行其任务,因此我们只需要更改自定义 Eclipse 构建器的工作目录设置。
在 JRE 选项卡中,选择“执行环境”。
将工作目录更改为当前工作空间。
Here is the solution I find:
Just as kgiannakakis mentioned, Ant also start executing its task from the location it was invoked, so we just need to change the working directory setting of our custom eclipse builder.
In the JRE tab, choose "Execution Environment".
Change the Working directory to your current workspace.
看起来我错过了因果报应,但无论如何......
我们这样做:-
Build.xml
build.properties
当然,您可以通过创建自己的 build.computername.properties 来覆盖它,以允许开发人员路径差异等
Looks like I've missed the karma but anyway...
We do this:-
Build.xml
build.properties
which of course you can override by creating your OWN build.computername.properties to allow for developer path differences etc